1use crate::Error;
2use std::collections::HashSet;
3
4#[derive(Debug, Clone)]
5pub struct Device {
6 path: String,
7 attrs: Option<HashSet<String>>,
8}
9
10impl Device {
11 #[inline]
12 pub fn new(path: &str) -> Self {
13 Self {
14 path: path.to_owned(),
15 attrs: None,
16 }
17 }
18 #[inline]
19 pub fn path(&self) -> &str {
20 &self.path
21 }
22 #[inline]
23 pub fn load(&mut self) -> Result<(), Error> {
27 let data = crate::get(&self.path)?;
28 let a: HashSet<String> = data
29 .split(',')
30 .filter(|v| !v.ends_with('/'))
31 .map(ToOwned::to_owned)
32 .collect();
33 self.attrs.replace(a);
34 Ok(())
35 }
36 #[inline]
37 pub fn attrs(&self) -> Vec<&str> {
38 if let Some(ref a) = self.attrs {
39 a.iter().map(String::as_str).collect()
40 } else {
41 Vec::new()
42 }
43 }
44 #[inline]
45 pub fn has(&self, attr: &str) -> bool {
46 if let Some(ref a) = self.attrs {
47 a.contains(attr)
48 } else {
49 false
50 }
51 }
52 #[inline]
53 pub fn get(&self, attr: &str) -> Result<String, Error> {
57 crate::get(&format!("{}/{}", self.path, attr))
58 }
59 #[inline]
60 pub fn set(&self, attr: &str, value: &str) -> Result<(), Error> {
64 crate::set(&format!("{}/{}", self.path, attr), value)
65 }
66 #[inline]
67 pub fn info(&self) -> Result<DeviceInfo, Error> {
71 Ok(DeviceInfo {
72 w1_type: self.get("type")?,
73 family: self.get("family")?.parse().ok(),
74 })
75 }
76}
77
78#[derive(Debug, Clone)]
79pub struct DeviceInfo {
80 w1_type: String,
81 family: Option<u32>,
82}
83
84impl DeviceInfo {
85 #[inline]
86 pub fn w1_type(&self) -> &str {
87 &self.w1_type
88 }
89 #[inline]
90 pub fn family(&self) -> Option<u32> {
91 self.family
92 }
93}