ns3_config/
config_lib_rust_config_lib_source.rs1use 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 0x1e, 0x99, 0xd2, 0xed, 0xb9, 0xbd, 0x0c, 0xbe, 0xc6, 0x9a, 0x81, 0xc6, 0x3e, 0x32, 0x57, 0xcd,
8 0x2f, 0xd1, 0xa4, 0x12, 0x53, 0x20, 0xce, 0xfd, 0x78, 0x83, 0x9a, 0x37, 0x45, 0xe5, 0x6e, 0x5c,
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 max_rolling_capture_buffer_size: u32,
15 pub multi_vmo: bool,
16 pub num_threads: u8,
17 pub opaque_iids: bool,
18 pub sampled_stats_enabled: bool,
19 pub suspend_enabled: bool,
20}
21impl Config {
22 #[doc = r" Take the config startup handle and parse its contents."]
23 #[doc = r""]
24 #[doc = r" # Panics"]
25 #[doc = r""]
26 #[doc = r" If the config startup handle was already taken or if it is not valid."]
27 pub fn take_from_startup_handle() -> Self {
28 <Self as ComponentConfig>::take_from_startup_handle()
29 }
30 #[doc = r" Parse `Self` from `vmo`."]
31 pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
32 <Self as ComponentConfig>::from_vmo(vmo)
33 }
34 #[doc = r" Parse `Self` from `bytes`."]
35 pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
36 <Self as ComponentConfig>::from_bytes(bytes)
37 }
38 pub fn record_inspect(&self, inspector_node: &Node) {
39 <Self as ComponentConfig>::record_inspect(self, inspector_node)
40 }
41}
42impl ComponentConfig for Config {
43 #[doc = r" Parse `Self` from `bytes`."]
44 fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
45 let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
46 let checksum_len_bytes: [u8; 2] =
47 checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
48 let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
49 let (observed_checksum, bytes) =
50 bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
51 if observed_checksum != EXPECTED_CHECKSUM {
52 return Err(Error::ChecksumMismatch {
53 expected_checksum: EXPECTED_CHECKSUM.to_vec(),
54 observed_checksum: observed_checksum.to_vec(),
55 });
56 }
57 let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
58 Ok(Self {
59 debug_logs: fidl_config.debug_logs,
60 max_rolling_capture_buffer_size: fidl_config.max_rolling_capture_buffer_size,
61 multi_vmo: fidl_config.multi_vmo,
62 num_threads: fidl_config.num_threads,
63 opaque_iids: fidl_config.opaque_iids,
64 sampled_stats_enabled: fidl_config.sampled_stats_enabled,
65 suspend_enabled: fidl_config.suspend_enabled,
66 })
67 }
68 fn to_bytes(&self) -> Result<Vec<u8>, Error> {
69 let fidl_config = FidlConfig {
70 debug_logs: self.debug_logs.clone(),
71 max_rolling_capture_buffer_size: self.max_rolling_capture_buffer_size.clone(),
72 multi_vmo: self.multi_vmo.clone(),
73 num_threads: self.num_threads.clone(),
74 opaque_iids: self.opaque_iids.clone(),
75 sampled_stats_enabled: self.sampled_stats_enabled.clone(),
76 suspend_enabled: self.suspend_enabled.clone(),
77 };
78 let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
79 let mut bytes = Vec::with_capacity(
80 EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
81 );
82 bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
83 bytes.extend_from_slice(EXPECTED_CHECKSUM);
84 bytes.append(&mut fidl_bytes);
85 Ok(bytes)
86 }
87 fn record_inspect(&self, inspector_node: &Node) {
88 inspector_node.record_bool("debug_logs", self.debug_logs);
89 inspector_node.record_uint(
90 "max_rolling_capture_buffer_size",
91 self.max_rolling_capture_buffer_size as u64,
92 );
93 inspector_node.record_bool("multi_vmo", self.multi_vmo);
94 inspector_node.record_uint("num_threads", self.num_threads as u64);
95 inspector_node.record_bool("opaque_iids", self.opaque_iids);
96 inspector_node.record_bool("sampled_stats_enabled", self.sampled_stats_enabled);
97 inspector_node.record_bool("suspend_enabled", self.suspend_enabled);
98 }
99}