ns3_config/
config_lib_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_ns3config::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0xaa, 0x8a, 0x2e, 0x7b, 0xd2, 0x1d, 0x06, 0x66, 0x21, 0x34, 0xfa, 0xbf, 0x58, 0x65, 0xdc, 0xd4,
8    0x25, 0xaa, 0x71, 0x85, 0xb8, 0x0f, 0x69, 0x9e, 0x5a, 0x77, 0xb5, 0xa6, 0xc3, 0xc1, 0xcd, 0xaa,
9];
10#[derive(Debug)]
11pub struct Config {
12    pub debug_logs: bool,
13    pub num_threads: u8,
14    pub opaque_iids: bool,
15    pub sampled_stats_enabled: bool,
16    pub suspend_enabled: bool,
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            debug_logs: fidl_config.debug_logs,
57            num_threads: fidl_config.num_threads,
58            opaque_iids: fidl_config.opaque_iids,
59            sampled_stats_enabled: fidl_config.sampled_stats_enabled,
60            suspend_enabled: fidl_config.suspend_enabled,
61        })
62    }
63    fn record_inspect(&self, inspector_node: &Node) {
64        inspector_node.record_bool("debug_logs", self.debug_logs);
65        inspector_node.record_uint("num_threads", self.num_threads as u64);
66        inspector_node.record_bool("opaque_iids", self.opaque_iids);
67        inspector_node.record_bool("sampled_stats_enabled", self.sampled_stats_enabled);
68        inspector_node.record_bool("suspend_enabled", self.suspend_enabled);
69    }
70}