omaha_client_structured_config/
omaha-client-structured-config-lib_rust_config_lib_source.rs
1use fidl::unpersist;
2use fidl_cf_sc_internal_omahaclientstructuredconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7 0x70, 0x33, 0xd6, 0x73, 0x02, 0x0a, 0x40, 0x60, 0x94, 0xfa, 0x2b, 0x27, 0x15, 0x17, 0x86, 0x3c,
8 0x72, 0x5b, 0x0c, 0xd1, 0x8d, 0x78, 0xcc, 0xec, 0xce, 0x1b, 0xe8, 0x41, 0xed, 0x16, 0x58, 0xd4,
9];
10#[derive(Debug)]
11pub struct Config {
12 pub allow_reboot_when_idle: bool,
13 pub fuzz_percentage_range: u8,
14 pub periodic_interval_minutes: u16,
15 pub retry_delay_seconds: u16,
16 pub startup_delay_seconds: u16,
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 allow_reboot_when_idle: fidl_config.allow_reboot_when_idle,
57 fuzz_percentage_range: fidl_config.fuzz_percentage_range,
58 periodic_interval_minutes: fidl_config.periodic_interval_minutes,
59 retry_delay_seconds: fidl_config.retry_delay_seconds,
60 startup_delay_seconds: fidl_config.startup_delay_seconds,
61 })
62 }
63 fn record_inspect(&self, inspector_node: &Node) {
64 inspector_node.record_bool("allow_reboot_when_idle", self.allow_reboot_when_idle);
65 inspector_node.record_uint("fuzz_percentage_range", self.fuzz_percentage_range as u64);
66 inspector_node
67 .record_uint("periodic_interval_minutes", self.periodic_interval_minutes as u64);
68 inspector_node.record_uint("retry_delay_seconds", self.retry_delay_seconds as u64);
69 inspector_node.record_uint("startup_delay_seconds", self.startup_delay_seconds as u64);
70 }
71}