Advertisement
Guest User

Untitled

a guest
May 20th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.15 KB | None | 0 0
  1. enum Type {
  2.     Network,
  3.     Temperature,
  4.     Load,
  5.     Time,
  6.     Fan,
  7.     Usage
  8. }
  9.  
  10. // A sensor has a specific "type" and an array of "Data"
  11. struct Sensor {
  12.     data: Vec<Data>,
  13.     sensor_type: Type
  14. }
  15.  
  16. // Data represents the raw value that a sensor can use. I.e. a Sensor can have multiple data inputs.
  17. struct Data {
  18.     value: i64,
  19.     path: PathBuf,
  20. }
  21.  
  22. // Different sensors are later stored in an array of trait objects
  23. trait Readable {
  24.     fn format(&self) -> &str;
  25. }
  26.  
  27. // Each sensor type has a unique method to format it's data.
  28. impl Readable for Sensor {
  29.     fn format(&self) ->&str {
  30.         match self.sensor_type {
  31.             Type::Network => "Network",
  32.             Type::Temperature => "Temp",
  33.             Type::Load => "Load",
  34.             Type::Time => "Time",
  35.             Type::Fan => "Fan",
  36.             Type::Usage => "Usage"
  37.  
  38.         }
  39.     }
  40. }
  41.  
  42. // "read_data" reads the raw value from the "path" variable specified in the Data struct.
  43. impl Data {
  44.     fn read_data(&mut self) {
  45.  
  46.         if let Ok(val) = fs::read(&self.path) {
  47.             self.raw = val;
  48.             println!("{:?}",self.raw);
  49.         }
  50.  
  51.  
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement