pdk_classy/
stream.rs

1// Copyright (c) 2025, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4
5//! Utils to read key/value data from a given the current context.
6
7use crate::proxy_wasm::types::Bytes;
8use crate::{
9    extract::{Extract, FromContext},
10    host::Host,
11    reactor::root::RootReactor,
12};
13use std::{convert::Infallible, rc::Rc};
14
15/// Abstraction that enables property manipulation
16pub trait PropertyAccessor {
17    /// Returns a property, if not missing
18    fn read_property(&self, path: &[&str]) -> Option<Bytes>;
19
20    /// Overrides a given property with a given value
21    fn set_property(&self, path: &[&str], value: Option<&[u8]>);
22}
23
24/// A [`PropertyAccessor`] which reads properties using the underlying host calls.
25pub struct StreamProperties {
26    host: Rc<dyn Host>,
27}
28
29impl StreamProperties {
30    pub fn new(host: Rc<dyn Host>) -> Self {
31        Self { host }
32    }
33}
34
35impl PropertyAccessor for StreamProperties {
36    fn read_property(&self, path: &[&str]) -> Option<Bytes> {
37        self.host.get_property(path.to_vec())
38    }
39
40    fn set_property(&self, path: &[&str], value: Option<&[u8]>) {
41        self.host.set_property(path.to_vec(), value)
42    }
43}
44
45/// Stream properties can be injected for any context.
46impl<C> FromContext<C> for StreamProperties
47where
48    Rc<dyn Host>: FromContext<C, Error = Infallible>,
49    Rc<RootReactor>: FromContext<C, Error = Infallible>,
50{
51    type Error = Infallible;
52
53    fn from_context(context: &C) -> Result<Self, Self::Error> {
54        let host = context.extract()?;
55        Ok(Self::new(host))
56    }
57}