pdk-classy 1.3.0-alpha.0

PDK Classy
Documentation
// Copyright 2023 Salesforce, Inc. All rights reserved.
use crate::proxy_wasm::types::Bytes;
use crate::{
    extract::{Extract, FromContext},
    host::Host,
    reactor::root::RootReactor,
};
use std::{convert::Infallible, rc::Rc};

pub trait PropertyAccessor {
    /// Returns a property, if not missing
    fn read_property(&self, path: &[&str]) -> Option<Bytes>;

    /// Overrides a given property with a given value
    fn set_property(&self, path: &[&str], value: Option<&[u8]>);
}
pub struct StreamProperties {
    host: Rc<dyn Host>,
}

impl StreamProperties {
    pub fn new(host: Rc<dyn Host>) -> Self {
        Self { host }
    }
}

impl PropertyAccessor for StreamProperties {
    fn read_property(&self, path: &[&str]) -> Option<Bytes> {
        self.host.get_property(path.to_vec())
    }

    fn set_property(&self, path: &[&str], value: Option<&[u8]>) {
        self.host.set_property(path.to_vec(), value)
    }
}

impl<C> FromContext<C> for StreamProperties
where
    Rc<dyn Host>: FromContext<C, Error = Infallible>,
    Rc<RootReactor>: FromContext<C, Error = Infallible>,
{
    type Error = Infallible;

    fn from_context(context: &C) -> Result<Self, Self::Error> {
        let host = context.extract()?;
        Ok(Self::new(host))
    }
}