wasmcloud_control_interface/types/
provider.rs

1//! Data types used when managing providers on a wasmCloud lattice
2
3use std::collections::BTreeMap;
4
5use serde::{Deserialize, Serialize};
6
7use crate::Result;
8
9/// A summary description of a capability provider within a host inventory
10#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
11#[non_exhaustive]
12pub struct ProviderDescription {
13    /// Provider's unique identifier
14    #[serde(default)]
15    pub(crate) id: String,
16    /// Image reference for this provider, if applicable
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub(crate) image_ref: Option<String>,
19    /// Name of the provider, if one exists
20    #[serde(default, skip_serializing_if = "Option::is_none")]
21    pub(crate) name: Option<String>,
22    /// The revision of the provider
23    #[serde(default)]
24    pub(crate) revision: i32,
25    /// The annotations that were used in the start request that produced
26    /// this provider instance
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub(crate) annotations: Option<BTreeMap<String, String>>,
29}
30
31impl ProviderDescription {
32    /// Get the ID of the provider
33    pub fn id(&self) -> &str {
34        &self.id
35    }
36
37    /// Get the image reference of the provider
38    pub fn image_ref(&self) -> Option<&str> {
39        self.image_ref.as_deref()
40    }
41
42    /// Get the name of the provider
43    pub fn name(&self) -> Option<&str> {
44        self.name.as_deref()
45    }
46
47    /// Get the revision of the provider
48    pub fn revision(&self) -> i32 {
49        self.revision
50    }
51
52    /// Get the annotations of the provider
53    pub fn annotations(&self) -> Option<&BTreeMap<String, String>> {
54        self.annotations.as_ref()
55    }
56
57    #[must_use]
58    pub fn builder() -> ProviderDescriptionBuilder {
59        ProviderDescriptionBuilder::default()
60    }
61}
62
63/// Builds [`ProviderDescription`]s
64#[derive(Clone, Debug, Default, Eq, PartialEq)]
65#[non_exhaustive]
66pub struct ProviderDescriptionBuilder {
67    id: Option<String>,
68    image_ref: Option<String>,
69    name: Option<String>,
70    revision: Option<i32>,
71    annotations: Option<BTreeMap<String, String>>,
72}
73
74impl ProviderDescriptionBuilder {
75    /// Provider's unique identifier
76    #[must_use]
77    pub fn id(mut self, v: &str) -> Self {
78        self.id = Some(v.into());
79        self
80    }
81
82    /// Image reference for this provider, if applicable
83    #[must_use]
84    pub fn image_ref(mut self, v: &str) -> Self {
85        self.image_ref = Some(v.into());
86        self
87    }
88
89    /// Name of the provider, if one exists
90    #[must_use]
91    pub fn name(mut self, v: &str) -> Self {
92        self.name = Some(v.into());
93        self
94    }
95
96    /// The revision of the provider
97    #[must_use]
98    pub fn revision(mut self, v: i32) -> Self {
99        self.revision = Some(v);
100        self
101    }
102
103    /// The annotations that were used in the start request that produced
104    /// this provider instance
105    #[must_use]
106    pub fn annotations(mut self, v: impl Into<BTreeMap<String, String>>) -> Self {
107        self.annotations = Some(v.into());
108        self
109    }
110
111    /// Build a [`ProviderDescription`]
112    pub fn build(self) -> Result<ProviderDescription> {
113        Ok(ProviderDescription {
114            id: self.id.ok_or_else(|| "id is required".to_string())?,
115            image_ref: self.image_ref,
116            name: self.name,
117            revision: self.revision.unwrap_or_default(),
118            annotations: self.annotations,
119        })
120    }
121}
122
123#[cfg(test)]
124mod tests {
125    use std::collections::BTreeMap;
126
127    use super::ProviderDescription;
128
129    #[test]
130    fn provider_description_builder() {
131        assert_eq!(
132            ProviderDescription {
133                id: "id".into(),
134                image_ref: Some("ref".into()),
135                name: Some("name".into()),
136                annotations: Some(BTreeMap::from([("a".into(), "b".into())])),
137                revision: 0,
138            },
139            ProviderDescription::builder()
140                .id("id")
141                .image_ref("ref")
142                .name("name")
143                .annotations(BTreeMap::from([("a".into(), "b".into())]))
144                .revision(0)
145                .build()
146                .unwrap()
147        )
148    }
149}