memory_monitor2_config/
memory-monitor2-config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_memorymonitor2config::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0xcc, 0x37, 0x00, 0x7a, 0x6c, 0xf4, 0xed, 0xbb, 0xc7, 0xf2, 0x0e, 0x93, 0x2b, 0x29, 0x52, 0xba,
8    0xf2, 0xb2, 0x89, 0x81, 0xb1, 0x05, 0x5f, 0x41, 0xc5, 0x13, 0xb4, 0x08, 0xec, 0x79, 0xb8, 0x8d,
9];
10#[derive(Debug)]
11pub struct Config {
12    pub capture_on_pressure_change: bool,
13    pub critical_capture_delay_s: u32,
14    pub imminent_oom_capture_delay_s: u32,
15    pub normal_capture_delay_s: u32,
16    pub warning_capture_delay_s: u32,
17}
18impl Config {
19    #[doc = r" Take the config startup handle and parse its contents."]
20    #[doc = r""]
21    #[doc = r" # Panics"]
22    #[doc = r""]
23    #[doc = r" If the config startup handle was already taken or if it is not valid."]
24    pub fn take_from_startup_handle() -> Self {
25        <Self as ComponentConfig>::take_from_startup_handle()
26    }
27    #[doc = r" Parse `Self` from `vmo`."]
28    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
29        <Self as ComponentConfig>::from_vmo(vmo)
30    }
31    #[doc = r" Parse `Self` from `bytes`."]
32    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
33        <Self as ComponentConfig>::from_bytes(bytes)
34    }
35    pub fn record_inspect(&self, inspector_node: &Node) {
36        <Self as ComponentConfig>::record_inspect(self, inspector_node)
37    }
38}
39impl ComponentConfig for Config {
40    #[doc = r" Parse `Self` from `bytes`."]
41    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
42        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
43        let checksum_len_bytes: [u8; 2] =
44            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
45        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
46        let (observed_checksum, bytes) =
47            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
48        if observed_checksum != EXPECTED_CHECKSUM {
49            return Err(Error::ChecksumMismatch {
50                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
51                observed_checksum: observed_checksum.to_vec(),
52            });
53        }
54        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
55        Ok(Self {
56            capture_on_pressure_change: fidl_config.capture_on_pressure_change,
57            critical_capture_delay_s: fidl_config.critical_capture_delay_s,
58            imminent_oom_capture_delay_s: fidl_config.imminent_oom_capture_delay_s,
59            normal_capture_delay_s: fidl_config.normal_capture_delay_s,
60            warning_capture_delay_s: fidl_config.warning_capture_delay_s,
61        })
62    }
63    fn record_inspect(&self, inspector_node: &Node) {
64        inspector_node.record_bool("capture_on_pressure_change", self.capture_on_pressure_change);
65        inspector_node
66            .record_uint("critical_capture_delay_s", self.critical_capture_delay_s as u64);
67        inspector_node
68            .record_uint("imminent_oom_capture_delay_s", self.imminent_oom_capture_delay_s as u64);
69        inspector_node.record_uint("normal_capture_delay_s", self.normal_capture_delay_s as u64);
70        inspector_node.record_uint("warning_capture_delay_s", self.warning_capture_delay_s as u64);
71    }
72}