lenso_capability_configuration_source/
generated_plugin.rs1pub const CAPABILITY_ID: &str = "lenso.configuration.source@1";
3pub const DESCRIPTOR_VERSION: &str = "1.0.0";
4pub const DESCRIPTOR_DIGEST: &str = "sha256:ff815d269114b4cbe4b268a68dee06979f292395deadcc2a52a561634b48ca21";
5pub const PORTABLE: bool = true;
6pub const SOURCE_CAPABILITY_ID: &str = CAPABILITY_ID;
7pub const SOURCE_DESCRIPTOR_VERSION: &str = DESCRIPTOR_VERSION;
8pub const SOURCE_DESCRIPTOR_DIGEST: &str = DESCRIPTOR_DIGEST;
9
10pub const FETCH_OPERATION: &str = "fetch";
11
12pub use lenso_contract_runtime::{Uint64, UnknownDomainError};
13use lenso_contract_runtime::{decode_portable_json, encode_portable_json};
14
15#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
16pub struct FetchRequest {
17
18}
19
20#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
21pub struct FetchResponse {
22 #[serde(rename = "configurations")]
23 #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
24 pub configurations: Vec<FetchResponseConfigurationsItem>,
25 #[serde(rename = "revision")]
26 #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
27 pub revision: Uint64,
28}
29
30#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
31pub struct FetchResponseConfigurationsItem {
32 #[serde(rename = "instance_key")]
33 #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
34 pub instance_key: String,
35 #[serde(rename = "plugin_id")]
36 #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
37 pub plugin_id: String,
38 #[serde(rename = "toml")]
39 #[serde(deserialize_with = "lenso_contract_runtime::serde::deserialize_required")]
40 pub toml: String,
41}
42
43#[derive(Clone, Debug, PartialEq)]
44pub enum FetchError {
45 InvalidSource,
46 Unknown(UnknownDomainError),
47}
48
49impl serde::Serialize for FetchError {
50 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
51 where
52 S: serde::Serializer,
53 {
54 use serde::ser::SerializeMap;
55 match self {
56 Self::InvalidSource => serializer.serialize_str("invalid_source"),
57 Self::Unknown(value) => {
58 let mut map = serializer.serialize_map(Some(1 + usize::from(value.payload.is_some()) + value.extra.len()))?;
59 map.serialize_entry("code", &value.code)?;
60 if let Some(payload) = &value.payload {
61 map.serialize_entry("payload", payload)?;
62 }
63 for (key, extra) in &value.extra {
64 map.serialize_entry(key, extra)?;
65 }
66 map.end()
67 },
68 }
69 }
70}
71
72impl<'de> serde::Deserialize<'de> for FetchError {
73 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
74 where
75 D: serde::Deserializer<'de>,
76 {
77 let value = <serde_json::Value as serde::Deserialize>::deserialize(deserializer)?;
78 match value {
79 serde_json::Value::String(code) => match code.as_str() {
80 "invalid_source" => Ok(Self::InvalidSource),
81 _ => Ok(Self::Unknown(UnknownDomainError { code, payload: None, extra: std::collections::BTreeMap::new() })),
82 },
83 serde_json::Value::Object(mut object) => {
84 let Some(code) = object.remove("code").and_then(|value| value.as_str().map(ToOwned::to_owned)) else {
85 return Err(serde::de::Error::custom("Domain Error object is missing a string code"));
86 };
87 let payload = object.remove("payload");
88 let extra = object.into_iter().collect::<std::collections::BTreeMap<_, _>>();
89 Ok(Self::Unknown(UnknownDomainError { code, payload, extra }))
90 }
91 other => Err(serde::de::Error::custom(format!("Domain Error must be a string or object, got {other}"))),
92 }
93 }
94}
95
96pub fn encode_fetch_request(value: &FetchRequest) -> Result<String, serde_json::Error> { encode_portable_json(value) }
97pub fn decode_fetch_request(wire: &str) -> Result<FetchRequest, serde_json::Error> { decode_portable_json(wire) }
98pub fn encode_fetch_response(value: &FetchResponse) -> Result<String, serde_json::Error> { encode_portable_json(value) }
99pub fn decode_fetch_response(wire: &str) -> Result<FetchResponse, serde_json::Error> { decode_portable_json(wire) }
100pub fn encode_fetch_error(value: &FetchError) -> Result<String, serde_json::Error> { encode_portable_json(value) }
101pub fn decode_fetch_error(wire: &str) -> Result<FetchError, serde_json::Error> { decode_portable_json(wire) }
102
103#[derive(Clone, Debug)]
104pub struct SourceClient {
105 dependency: lenso_plugin_sdk::Dependency,
106}
107
108impl lenso_plugin_sdk::DependencyClient for SourceClient {
109 const CAPABILITY_ID: &'static str = CAPABILITY_ID;
110 const DESCRIPTOR_VERSION: &'static str = DESCRIPTOR_VERSION;
111 const DESCRIPTOR_DIGEST: &'static str = DESCRIPTOR_DIGEST;
112
113 fn from_dependency(dependency: lenso_plugin_sdk::Dependency) -> Self {
114 Self { dependency }
115 }
116}
117
118impl SourceClient {
119 pub fn fetch(
120 &self,
121 context: &lenso_plugin_sdk::Ctx,
122 request: FetchRequest,
123 ) -> Result<FetchResponse, lenso_plugin_sdk::CallError<FetchError>> {
124 context.request(&self.dependency, FETCH_OPERATION, &request)
125 }
126}
127
128pub trait SourceProvider {
129 fn fetch(
130 &self,
131 context: lenso_plugin_sdk::Ctx,
132 request: FetchRequest,
133 ) -> Result<FetchResponse, FetchError>;
134}
135
136#[macro_export]
137macro_rules! export_source_plugin {
138 ($plugin:ty) => {
139 impl lenso_plugin_sdk::JsonRequestHandler for $plugin {
140 fn invoke_with_context(
141 &self,
142 context: lenso_plugin_sdk::Ctx,
143 capability: &str,
144 operation: &str,
145 request: lenso_plugin_sdk::__private::serde_json::Value,
146 ) -> lenso_plugin_sdk::InvocationOutcome {
147 if capability != $crate::CAPABILITY_ID {
148 return lenso_plugin_sdk::InvocationOutcome::Failure("unknown Capability".to_owned());
149 }
150 match operation {
151 $crate::FETCH_OPERATION => {
152 let request = match lenso_plugin_sdk::__private::serde_json::from_value::<$crate::FetchRequest>(request) {
153 Ok(request) => request,
154 Err(_) => return lenso_plugin_sdk::InvocationOutcome::Failure("invalid Capability request".to_owned()),
155 };
156 match <$plugin as $crate::SourceProvider>::fetch(self, context, request) {
157 Ok(value) => match lenso_plugin_sdk::__private::serde_json::to_value(value) {
158 Ok(value) => lenso_plugin_sdk::InvocationOutcome::Success(value),
159 Err(error) => lenso_plugin_sdk::InvocationOutcome::Failure(error.to_string()),
160 },
161 Err(error) => match lenso_plugin_sdk::__private::serde_json::to_value(error) {
162 Ok(error) => lenso_plugin_sdk::InvocationOutcome::DomainError(error),
163 Err(error) => lenso_plugin_sdk::InvocationOutcome::Failure(error.to_string()),
164 },
165 }
166 }
167 _ => lenso_plugin_sdk::InvocationOutcome::Failure("unknown Capability operation".to_owned()),
168 }
169 }
170 }
171
172 lenso_plugin_sdk::__export_json_request_handler! {
173 $plugin {
174 capability_id: "lenso.configuration.source@1",
175 descriptor_version: "1.0.0",
176 descriptor_digest: "sha256:ff815d269114b4cbe4b268a68dee06979f292395deadcc2a52a561634b48ca21",
177 requests: ["fetch"],
178 }
179 }
180 };
181}