Skip to main content

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    0x20, 0x89, 0x62, 0x9e, 0xa2, 0x2c, 0x59, 0xee, 0xcb, 0xbc, 0x91, 0xe4, 0xdc, 0xaa, 0x77, 0x14,
8    0x41, 0xe4, 0xf0, 0xae, 0xee, 0x1c, 0x3e, 0x32, 0xea, 0x11, 0xf3, 0x50, 0xa7, 0xcc, 0x12, 0x11,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub debug_logs: bool,
14    pub multi_vmo: bool,
15    pub num_threads: u8,
16    pub opaque_iids: bool,
17    pub sampled_stats_enabled: bool,
18    pub suspend_enabled: bool,
19}
20impl Config {
21    #[doc = r" Take the config startup handle and parse its contents."]
22    #[doc = r""]
23    #[doc = r" # Panics"]
24    #[doc = r""]
25    #[doc = r" If the config startup handle was already taken or if it is not valid."]
26    pub fn take_from_startup_handle() -> Self {
27        <Self as ComponentConfig>::take_from_startup_handle()
28    }
29    #[doc = r" Parse `Self` from `vmo`."]
30    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
31        <Self as ComponentConfig>::from_vmo(vmo)
32    }
33    #[doc = r" Parse `Self` from `bytes`."]
34    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
35        <Self as ComponentConfig>::from_bytes(bytes)
36    }
37    pub fn record_inspect(&self, inspector_node: &Node) {
38        <Self as ComponentConfig>::record_inspect(self, inspector_node)
39    }
40}
41impl ComponentConfig for Config {
42    #[doc = r" Parse `Self` from `bytes`."]
43    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
44        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
45        let checksum_len_bytes: [u8; 2] =
46            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
47        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
48        let (observed_checksum, bytes) =
49            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
50        if observed_checksum != EXPECTED_CHECKSUM {
51            return Err(Error::ChecksumMismatch {
52                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
53                observed_checksum: observed_checksum.to_vec(),
54            });
55        }
56        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
57        Ok(Self {
58            debug_logs: fidl_config.debug_logs,
59            multi_vmo: fidl_config.multi_vmo,
60            num_threads: fidl_config.num_threads,
61            opaque_iids: fidl_config.opaque_iids,
62            sampled_stats_enabled: fidl_config.sampled_stats_enabled,
63            suspend_enabled: fidl_config.suspend_enabled,
64        })
65    }
66    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
67        let fidl_config = FidlConfig {
68            debug_logs: self.debug_logs.clone(),
69            multi_vmo: self.multi_vmo.clone(),
70            num_threads: self.num_threads.clone(),
71            opaque_iids: self.opaque_iids.clone(),
72            sampled_stats_enabled: self.sampled_stats_enabled.clone(),
73            suspend_enabled: self.suspend_enabled.clone(),
74        };
75        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
76        let mut bytes = Vec::with_capacity(
77            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
78        );
79        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
80        bytes.extend_from_slice(EXPECTED_CHECKSUM);
81        bytes.append(&mut fidl_bytes);
82        Ok(bytes)
83    }
84    fn record_inspect(&self, inspector_node: &Node) {
85        inspector_node.record_bool("debug_logs", self.debug_logs);
86        inspector_node.record_bool("multi_vmo", self.multi_vmo);
87        inspector_node.record_uint("num_threads", self.num_threads as u64);
88        inspector_node.record_bool("opaque_iids", self.opaque_iids);
89        inspector_node.record_bool("sampled_stats_enabled", self.sampled_stats_enabled);
90        inspector_node.record_bool("suspend_enabled", self.suspend_enabled);
91    }
92}