pdk-classy 1.9.1-alpha.2

PDK Classy
Documentation
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

//! Utils to read key/value data from a given the current context.

use crate::proxy_wasm::types::Bytes;
use crate::{
    extract::{Extract, FromContext},
    host::Host,
    reactor::root::RootReactor,
};
use std::{convert::Infallible, rc::Rc};

/// Abstraction that enables property manipulation
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]>);
}

/// A [`PropertyAccessor`] which reads properties using the underlying host calls.
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)
    }
}

/// Stream properties can be injected for any context.
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))
    }
}