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 {
fn read_property(&self, path: &[&str]) -> Option<Bytes>;
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))
}
}