lenso-capability-configuration-source 0.1.1

Portable bootstrap Configuration Source Capability for Lenso Hosts
Documentation
// @generated by lenso-contract-codegen; do not edit.
pub const CAPABILITY_ID: &str = "lenso.configuration.source@1";
pub const DESCRIPTOR_VERSION: &str = "1.0.0";
pub const DESCRIPTOR_DIGEST: &str = "sha256:ff815d269114b4cbe4b268a68dee06979f292395deadcc2a52a561634b48ca21";
pub const PORTABLE: bool = true;
pub const SOURCE_CAPABILITY_ID: &str = CAPABILITY_ID;
pub const SOURCE_DESCRIPTOR_VERSION: &str = DESCRIPTOR_VERSION;
pub const SOURCE_DESCRIPTOR_DIGEST: &str = DESCRIPTOR_DIGEST;

pub const FETCH_OPERATION: &str = "fetch";

pub use lenso_contract_runtime::{Uint64, UnknownDomainError};
use lenso_contract_runtime::{decode_portable_json, encode_portable_json};

#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct FetchRequest {

}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct FetchResponse {
    #[serde(rename = "configurations")]
    #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
    pub configurations: Vec<FetchResponseConfigurationsItem>,
    #[serde(rename = "revision")]
    #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
    pub revision: Uint64,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct FetchResponseConfigurationsItem {
    #[serde(rename = "instance_key")]
    #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
    pub instance_key: String,
    #[serde(rename = "plugin_id")]
    #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
    pub plugin_id: String,
    #[serde(rename = "toml")]
    #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
    pub toml: String,
}

#[derive(Clone, Debug, PartialEq)]
pub enum FetchError {
    InvalidSource,
    Unknown(UnknownDomainError),
}

impl serde::Serialize for FetchError {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeMap;
        match self {
            Self::InvalidSource => serializer.serialize_str("invalid_source"),
            Self::Unknown(value) => {
                let mut map = serializer.serialize_map(Some(1 + usize::from(value.payload.is_some()) + value.extra.len()))?;
                map.serialize_entry("code", &value.code)?;
                if let Some(payload) = &value.payload {
                    map.serialize_entry("payload", payload)?;
                }
                for (key, extra) in &value.extra {
                    map.serialize_entry(key, extra)?;
                }
                map.end()
            },
        }
    }
}

impl<'de> serde::Deserialize<'de> for FetchError {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = <serde_json::Value as serde::Deserialize>::deserialize(deserializer)?;
        match value {
            serde_json::Value::String(code) => match code.as_str() {
                "invalid_source" => Ok(Self::InvalidSource),
                _ => Ok(Self::Unknown(UnknownDomainError { code, payload: None, extra: std::collections::BTreeMap::new() })),
            },
            serde_json::Value::Object(mut object) => {
                let Some(code) = object.remove("code").and_then(|value| value.as_str().map(ToOwned::to_owned)) else {
                    return Err(serde::de::Error::custom("Domain Error object is missing a string code"));
                };
                let payload = object.remove("payload");
                let extra = object.into_iter().collect::<std::collections::BTreeMap<_, _>>();
                Ok(Self::Unknown(UnknownDomainError { code, payload, extra }))
            }
            other => Err(serde::de::Error::custom(format!("Domain Error must be a string or object, got {other}"))),
        }
    }
}

pub fn encode_fetch_request(value: &FetchRequest) -> Result<String, serde_json::Error> { encode_portable_json(value) }
pub fn decode_fetch_request(wire: &str) -> Result<FetchRequest, serde_json::Error> { decode_portable_json(wire) }
pub fn encode_fetch_response(value: &FetchResponse) -> Result<String, serde_json::Error> { encode_portable_json(value) }
pub fn decode_fetch_response(wire: &str) -> Result<FetchResponse, serde_json::Error> { decode_portable_json(wire) }
pub fn encode_fetch_error(value: &FetchError) -> Result<String, serde_json::Error> { encode_portable_json(value) }
pub fn decode_fetch_error(wire: &str) -> Result<FetchError, serde_json::Error> { decode_portable_json(wire) }

#[derive(Clone, Debug)]
pub struct SourceClient {
    dependency: lenso_plugin_sdk::Dependency,
}

impl lenso_plugin_sdk::DependencyClient for SourceClient {
    const CAPABILITY_ID: &'static str = CAPABILITY_ID;
    const DESCRIPTOR_VERSION: &'static str = DESCRIPTOR_VERSION;
    const DESCRIPTOR_DIGEST: &'static str = DESCRIPTOR_DIGEST;

    fn from_dependency(dependency: lenso_plugin_sdk::Dependency) -> Self {
        Self { dependency }
    }
}

impl SourceClient {
    pub fn fetch(
        &self,
        context: &lenso_plugin_sdk::Ctx,
        request: FetchRequest,
    ) -> Result<FetchResponse, lenso_plugin_sdk::CallError<FetchError>> {
        context.request(&self.dependency, FETCH_OPERATION, &request)
    }
}

pub trait SourceProvider {
    fn fetch(
        &self,
        context: lenso_plugin_sdk::Ctx,
        request: FetchRequest,
    ) -> Result<FetchResponse, FetchError>;
}

#[macro_export]
macro_rules! export_source_plugin {
    ($plugin:ty) => {
        impl lenso_plugin_sdk::JsonRequestHandler for $plugin {
            fn invoke_with_context(
                &self,
                context: lenso_plugin_sdk::Ctx,
                capability: &str,
                operation: &str,
                request: lenso_plugin_sdk::__private::serde_json::Value,
            ) -> lenso_plugin_sdk::InvocationOutcome {
                if capability != $crate::CAPABILITY_ID {
                    return lenso_plugin_sdk::InvocationOutcome::Failure("unknown Capability".to_owned());
                }
                match operation {
                    $crate::FETCH_OPERATION => {
                        let request = match lenso_plugin_sdk::__private::serde_json::from_value::<$crate::FetchRequest>(request) {
                            Ok(request) => request,
                            Err(_) => return lenso_plugin_sdk::InvocationOutcome::Failure("invalid Capability request".to_owned()),
                        };
                        match <$plugin as $crate::SourceProvider>::fetch(self, context, request) {
                            Ok(value) => match lenso_plugin_sdk::__private::serde_json::to_value(value) {
                                Ok(value) => lenso_plugin_sdk::InvocationOutcome::Success(value),
                                Err(error) => lenso_plugin_sdk::InvocationOutcome::Failure(error.to_string()),
                            },
                            Err(error) => match lenso_plugin_sdk::__private::serde_json::to_value(error) {
                                Ok(error) => lenso_plugin_sdk::InvocationOutcome::DomainError(error),
                                Err(error) => lenso_plugin_sdk::InvocationOutcome::Failure(error.to_string()),
                            },
                        }
                    }
                    _ => lenso_plugin_sdk::InvocationOutcome::Failure("unknown Capability operation".to_owned()),
                }
            }
        }

        lenso_plugin_sdk::__export_json_request_handler! {
            $plugin {
                capability_id: "lenso.configuration.source@1",
                descriptor_version: "1.0.0",
                descriptor_digest: "sha256:ff815d269114b4cbe4b268a68dee06979f292395deadcc2a52a561634b48ca21",
                requests: ["fetch"],
            }
        }
    };
}