Skip to main content

virtcon_config/
virtcon_config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_virtconconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::{ArithmeticArrayProperty, Node};
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0x2e, 0xda, 0x15, 0x77, 0x6c, 0x99, 0xae, 0x80, 0xb7, 0xa2, 0xe0, 0x0d, 0xbe, 0xfd, 0x17, 0x72,
8    0xd3, 0x7a, 0x55, 0xd4, 0x23, 0xeb, 0xaa, 0x36, 0x00, 0xe0, 0x26, 0x51, 0x63, 0x02, 0xa3, 0xd2,
9];
10#[derive(Debug)]
11pub struct Config {
12    pub buffer_count: u32,
13    pub color_scheme: String,
14    pub disable: bool,
15    pub display_rotation: u32,
16    pub dpi: Vec<u32>,
17    pub font_size: String,
18    pub keep_log_visible: bool,
19    pub key_map: String,
20    pub keyrepeat: bool,
21    pub rounded_corners: bool,
22    pub scrollback_rows: u32,
23    pub show_logo: bool,
24}
25impl Config {
26    #[doc = r" Take the config startup handle and parse its contents."]
27    #[doc = r""]
28    #[doc = r" # Panics"]
29    #[doc = r""]
30    #[doc = r" If the config startup handle was already taken or if it is not valid."]
31    pub fn take_from_startup_handle() -> Self {
32        <Self as ComponentConfig>::take_from_startup_handle()
33    }
34    #[doc = r" Parse `Self` from `vmo`."]
35    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
36        <Self as ComponentConfig>::from_vmo(vmo)
37    }
38    #[doc = r" Parse `Self` from `bytes`."]
39    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
40        <Self as ComponentConfig>::from_bytes(bytes)
41    }
42    pub fn record_inspect(&self, inspector_node: &Node) {
43        <Self as ComponentConfig>::record_inspect(self, inspector_node)
44    }
45}
46impl ComponentConfig for Config {
47    #[doc = r" Parse `Self` from `bytes`."]
48    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
49        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
50        let checksum_len_bytes: [u8; 2] =
51            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
52        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
53        let (observed_checksum, bytes) =
54            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
55        if observed_checksum != EXPECTED_CHECKSUM {
56            return Err(Error::ChecksumMismatch {
57                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
58                observed_checksum: observed_checksum.to_vec(),
59            });
60        }
61        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
62        Ok(Self {
63            buffer_count: fidl_config.buffer_count,
64            color_scheme: fidl_config.color_scheme,
65            disable: fidl_config.disable,
66            display_rotation: fidl_config.display_rotation,
67            dpi: fidl_config.dpi,
68            font_size: fidl_config.font_size,
69            keep_log_visible: fidl_config.keep_log_visible,
70            key_map: fidl_config.key_map,
71            keyrepeat: fidl_config.keyrepeat,
72            rounded_corners: fidl_config.rounded_corners,
73            scrollback_rows: fidl_config.scrollback_rows,
74            show_logo: fidl_config.show_logo,
75        })
76    }
77    fn record_inspect(&self, inspector_node: &Node) {
78        inspector_node.record_uint("buffer_count", self.buffer_count as u64);
79        inspector_node.record_string("color_scheme", &self.color_scheme);
80        inspector_node.record_bool("disable", self.disable);
81        inspector_node.record_uint("display_rotation", self.display_rotation as u64);
82        let arr = inspector_node.create_uint_array("dpi", self.dpi.len());
83        for i in 0..self.dpi.len() {
84            arr.add(i, self.dpi[i] as u64);
85        }
86        inspector_node.record(arr);
87        inspector_node.record_string("font_size", &self.font_size);
88        inspector_node.record_bool("keep_log_visible", self.keep_log_visible);
89        inspector_node.record_string("key_map", &self.key_map);
90        inspector_node.record_bool("keyrepeat", self.keyrepeat);
91        inspector_node.record_bool("rounded_corners", self.rounded_corners);
92        inspector_node.record_uint("scrollback_rows", self.scrollback_rows as u64);
93        inspector_node.record_bool("show_logo", self.show_logo);
94    }
95}