Skip to main content

httpsdate_config/
httpsdate_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_httpsdateconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0x01, 0x81, 0x2a, 0x29, 0x52, 0x74, 0x64, 0xd4, 0xf7, 0x4c, 0x12, 0x72, 0xdb, 0x8e, 0xc4, 0xf4,
8    0x5f, 0x0b, 0x63, 0xb4, 0xbc, 0xd0, 0x23, 0xab, 0x36, 0xbb, 0x1b, 0x05, 0x77, 0x78, 0xad, 0x48,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub first_rtt_time_factor: u16,
14    pub https_timeout_sec: u8,
15    pub is_monitor_time_source: bool,
16    pub max_attempts_urgency_high: u32,
17    pub max_attempts_urgency_low: u32,
18    pub max_attempts_urgency_medium: u32,
19    pub num_polls_urgency_high: u32,
20    pub num_polls_urgency_low: u32,
21    pub num_polls_urgency_medium: u32,
22    pub standard_deviation_bound_percentage: u8,
23    pub time_source_endpoint_url: String,
24    pub use_pull_api: bool,
25}
26impl Config {
27    #[doc = r" Take the config startup handle and parse its contents."]
28    #[doc = r""]
29    #[doc = r" # Panics"]
30    #[doc = r""]
31    #[doc = r" If the config startup handle was already taken or if it is not valid."]
32    pub fn take_from_startup_handle() -> Self {
33        <Self as ComponentConfig>::take_from_startup_handle()
34    }
35    #[doc = r" Parse `Self` from `vmo`."]
36    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
37        <Self as ComponentConfig>::from_vmo(vmo)
38    }
39    #[doc = r" Parse `Self` from `bytes`."]
40    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
41        <Self as ComponentConfig>::from_bytes(bytes)
42    }
43    pub fn record_inspect(&self, inspector_node: &Node) {
44        <Self as ComponentConfig>::record_inspect(self, inspector_node)
45    }
46}
47impl ComponentConfig for Config {
48    #[doc = r" Parse `Self` from `bytes`."]
49    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
50        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
51        let checksum_len_bytes: [u8; 2] =
52            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
53        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
54        let (observed_checksum, bytes) =
55            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
56        if observed_checksum != EXPECTED_CHECKSUM {
57            return Err(Error::ChecksumMismatch {
58                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
59                observed_checksum: observed_checksum.to_vec(),
60            });
61        }
62        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
63        Ok(Self {
64            first_rtt_time_factor: fidl_config.first_rtt_time_factor,
65            https_timeout_sec: fidl_config.https_timeout_sec,
66            is_monitor_time_source: fidl_config.is_monitor_time_source,
67            max_attempts_urgency_high: fidl_config.max_attempts_urgency_high,
68            max_attempts_urgency_low: fidl_config.max_attempts_urgency_low,
69            max_attempts_urgency_medium: fidl_config.max_attempts_urgency_medium,
70            num_polls_urgency_high: fidl_config.num_polls_urgency_high,
71            num_polls_urgency_low: fidl_config.num_polls_urgency_low,
72            num_polls_urgency_medium: fidl_config.num_polls_urgency_medium,
73            standard_deviation_bound_percentage: fidl_config.standard_deviation_bound_percentage,
74            time_source_endpoint_url: fidl_config.time_source_endpoint_url,
75            use_pull_api: fidl_config.use_pull_api,
76        })
77    }
78    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
79        let fidl_config = FidlConfig {
80            first_rtt_time_factor: self.first_rtt_time_factor.clone(),
81            https_timeout_sec: self.https_timeout_sec.clone(),
82            is_monitor_time_source: self.is_monitor_time_source.clone(),
83            max_attempts_urgency_high: self.max_attempts_urgency_high.clone(),
84            max_attempts_urgency_low: self.max_attempts_urgency_low.clone(),
85            max_attempts_urgency_medium: self.max_attempts_urgency_medium.clone(),
86            num_polls_urgency_high: self.num_polls_urgency_high.clone(),
87            num_polls_urgency_low: self.num_polls_urgency_low.clone(),
88            num_polls_urgency_medium: self.num_polls_urgency_medium.clone(),
89            standard_deviation_bound_percentage: self.standard_deviation_bound_percentage.clone(),
90            time_source_endpoint_url: self.time_source_endpoint_url.clone(),
91            use_pull_api: self.use_pull_api.clone(),
92        };
93        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
94        let mut bytes = Vec::with_capacity(
95            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
96        );
97        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
98        bytes.extend_from_slice(EXPECTED_CHECKSUM);
99        bytes.append(&mut fidl_bytes);
100        Ok(bytes)
101    }
102    fn record_inspect(&self, inspector_node: &Node) {
103        inspector_node.record_uint("first_rtt_time_factor", self.first_rtt_time_factor as u64);
104        inspector_node.record_uint("https_timeout_sec", self.https_timeout_sec as u64);
105        inspector_node.record_bool("is_monitor_time_source", self.is_monitor_time_source);
106        inspector_node
107            .record_uint("max_attempts_urgency_high", self.max_attempts_urgency_high as u64);
108        inspector_node
109            .record_uint("max_attempts_urgency_low", self.max_attempts_urgency_low as u64);
110        inspector_node
111            .record_uint("max_attempts_urgency_medium", self.max_attempts_urgency_medium as u64);
112        inspector_node.record_uint("num_polls_urgency_high", self.num_polls_urgency_high as u64);
113        inspector_node.record_uint("num_polls_urgency_low", self.num_polls_urgency_low as u64);
114        inspector_node
115            .record_uint("num_polls_urgency_medium", self.num_polls_urgency_medium as u64);
116        inspector_node.record_uint(
117            "standard_deviation_bound_percentage",
118            self.standard_deviation_bound_percentage as u64,
119        );
120        inspector_node.record_string("time_source_endpoint_url", &self.time_source_endpoint_url);
121        inspector_node.record_bool("use_pull_api", self.use_pull_api);
122    }
123}