sysctl/ctl_info.rs
1// ctl_info.rs
2
3use ctl_flags::*;
4use ctl_type::*;
5
6#[derive(Debug, PartialEq)]
7/// A structure representing control metadata
8pub struct CtlInfo {
9 /// The control type.
10 pub ctl_type: CtlType,
11
12 /// A string which specifies the format of the OID in
13 /// a symbolic way.
14 ///
15 /// This format is used as a hint by sysctl(8) to
16 /// apply proper data formatting for display purposes.
17 ///
18 /// Formats defined in sysctl(9):
19 /// * `N` node
20 /// * `A` char *
21 /// * `I` int
22 /// * `IK[n]` temperature in Kelvin, multiplied by an optional single
23 /// digit power of ten scaling factor: 1 (default) gives deciKelvin,
24 /// 0 gives Kelvin, 3 gives milliKelvin
25 /// * `IU` unsigned int
26 /// * `L` long
27 /// * `LU` unsigned long
28 /// * `Q` quad_t
29 /// * `QU` u_quad_t
30 /// * `S,TYPE` struct TYPE structures
31 pub fmt: String,
32
33 pub flags: u32,
34}
35
36#[cfg(target_os = "freebsd")]
37impl CtlInfo {
38 /// Is this sysctl a temperature?
39 pub fn is_temperature(&self) -> bool {
40 self.fmt.starts_with("IK")
41 }
42}
43
44impl CtlInfo {
45 /// Return the flags for this sysctl.
46 pub fn flags(&self) -> CtlFlags {
47 CtlFlags::from_bits_truncate(self.flags)
48 }
49
50 /// If the sysctl is a structure, return the structure type string.
51 ///
52 /// Checks whether the format string starts with `S,` and returns the rest
53 /// of the format string or None if the format String does not have a struct
54 /// hint.
55 pub fn struct_type(&self) -> Option<String> {
56 if !self.fmt.starts_with("S,") {
57 return None;
58 }
59
60 Some(self.fmt[2..].into())
61 }
62}