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];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub buffer_count: u32,
14    pub color_scheme: String,
15    pub disable: bool,
16    pub display_rotation: u32,
17    pub dpi: Vec<u32>,
18    pub font_size: String,
19    pub keep_log_visible: bool,
20    pub key_map: String,
21    pub keyrepeat: bool,
22    pub rounded_corners: bool,
23    pub scrollback_rows: u32,
24    pub show_logo: 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            buffer_count: fidl_config.buffer_count,
65            color_scheme: fidl_config.color_scheme,
66            disable: fidl_config.disable,
67            display_rotation: fidl_config.display_rotation,
68            dpi: fidl_config.dpi,
69            font_size: fidl_config.font_size,
70            keep_log_visible: fidl_config.keep_log_visible,
71            key_map: fidl_config.key_map,
72            keyrepeat: fidl_config.keyrepeat,
73            rounded_corners: fidl_config.rounded_corners,
74            scrollback_rows: fidl_config.scrollback_rows,
75            show_logo: fidl_config.show_logo,
76        })
77    }
78    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
79        let fidl_config = FidlConfig {
80            buffer_count: self.buffer_count.clone(),
81            color_scheme: self.color_scheme.clone(),
82            disable: self.disable.clone(),
83            display_rotation: self.display_rotation.clone(),
84            dpi: self.dpi.clone(),
85            font_size: self.font_size.clone(),
86            keep_log_visible: self.keep_log_visible.clone(),
87            key_map: self.key_map.clone(),
88            keyrepeat: self.keyrepeat.clone(),
89            rounded_corners: self.rounded_corners.clone(),
90            scrollback_rows: self.scrollback_rows.clone(),
91            show_logo: self.show_logo.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("buffer_count", self.buffer_count as u64);
104        inspector_node.record_string("color_scheme", &self.color_scheme);
105        inspector_node.record_bool("disable", self.disable);
106        inspector_node.record_uint("display_rotation", self.display_rotation as u64);
107        let arr = inspector_node.create_uint_array("dpi", self.dpi.len());
108        for i in 0..self.dpi.len() {
109            arr.add(i, self.dpi[i] as u64);
110        }
111        inspector_node.record(arr);
112        inspector_node.record_string("font_size", &self.font_size);
113        inspector_node.record_bool("keep_log_visible", self.keep_log_visible);
114        inspector_node.record_string("key_map", &self.key_map);
115        inspector_node.record_bool("keyrepeat", self.keyrepeat);
116        inspector_node.record_bool("rounded_corners", self.rounded_corners);
117        inspector_node.record_uint("scrollback_rows", self.scrollback_rows as u64);
118        inspector_node.record_bool("show_logo", self.show_logo);
119    }
120}