hwmon abstraction wip

This commit is contained in:
Robin Appelman 2023-05-03 20:26:30 +02:00
commit 197f19d3a1
3 changed files with 124 additions and 33 deletions

View file

@ -1,6 +1,7 @@
pub mod disk;
pub mod docker;
pub mod gpu;
pub mod hwmon;
pub mod power;
pub mod sensors;
@ -10,10 +11,6 @@ use crate::disk::*;
use crate::sensors::*;
use color_eyre::Result;
use std::fmt::Write;
use std::fs::File;
use std::io::{ErrorKind, Read, Seek};
use std::path::Path;
use std::str::FromStr;
pub fn get_metrics() -> Result<String> {
let disk_usage = disk_usage()?;
@ -119,31 +116,3 @@ pub fn get_metrics() -> Result<String> {
}
Ok(result)
}
pub struct FileSource {
buff: String,
file: File,
}
impl FileSource {
pub fn open<P: AsRef<Path>>(path: P) -> std::io::Result<FileSource> {
Ok(FileSource {
buff: String::with_capacity(32),
file: File::open(path)?,
})
}
pub fn read<T>(&mut self) -> std::io::Result<T>
where
T: FromStr,
<T as FromStr>::Err: std::error::Error + Send + Sync + 'static,
{
self.buff.clear();
self.file.rewind()?;
self.file.read_to_string(&mut self.buff)?;
self.buff
.trim()
.parse()
.map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e))
}
}