proxy_sdk/property/
mod.rs

1use log::warn;
2
3use crate::{hostcalls, log_concern};
4
5pub mod all;
6pub mod envoy;
7
8pub fn get_property(name: impl AsRef<str>) -> Option<Vec<u8>> {
9    log_concern(
10        "get-property",
11        hostcalls::get_property(name.as_ref().split('.')),
12    )
13}
14
15pub fn get_property_string(name: impl AsRef<str>) -> Option<String> {
16    get_property(name).map(|x| String::from_utf8_lossy(&x).into_owned())
17}
18
19pub fn set_property(name: impl AsRef<str>, value: impl AsRef<[u8]>) {
20    log_concern(
21        "set-property",
22        hostcalls::set_property(name.as_ref().split('.'), Some(value.as_ref())),
23    );
24}
25
26pub fn get_property_int(name: &str) -> Option<i64> {
27    let raw = get_property(name)?;
28    if raw.len() != 8 {
29        return None;
30    }
31    Some(i64::from_le_bytes(raw.try_into().unwrap()))
32}
33
34pub fn get_property_bool(name: &str) -> Option<bool> {
35    let raw = get_property(name)?;
36    if raw.is_empty() || raw.len() != 1 {
37        return None;
38    }
39    Some(raw[0] != 0)
40}
41
42pub fn get_property_decode<P: prost::Message + Default>(name: &str) -> Option<P> {
43    let raw = get_property(name)?;
44    match P::decode(&raw[..]) {
45        Ok(x) => Some(x),
46        Err(e) => {
47            warn!("failed to decode property '{name}': {e:?}");
48            None
49        }
50    }
51}