wasmcloud_control_interface/types/
provider.rs1use std::collections::BTreeMap;
4
5use serde::{Deserialize, Serialize};
6
7use crate::Result;
8
9#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
11#[non_exhaustive]
12pub struct ProviderDescription {
13 #[serde(default)]
15 pub(crate) id: String,
16 #[serde(default, skip_serializing_if = "Option::is_none")]
18 pub(crate) image_ref: Option<String>,
19 #[serde(default, skip_serializing_if = "Option::is_none")]
21 pub(crate) name: Option<String>,
22 #[serde(default)]
24 pub(crate) revision: i32,
25 #[serde(default, skip_serializing_if = "Option::is_none")]
28 pub(crate) annotations: Option<BTreeMap<String, String>>,
29}
30
31impl ProviderDescription {
32 pub fn id(&self) -> &str {
34 &self.id
35 }
36
37 pub fn image_ref(&self) -> Option<&str> {
39 self.image_ref.as_deref()
40 }
41
42 pub fn name(&self) -> Option<&str> {
44 self.name.as_deref()
45 }
46
47 pub fn revision(&self) -> i32 {
49 self.revision
50 }
51
52 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#[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 #[must_use]
77 pub fn id(mut self, v: &str) -> Self {
78 self.id = Some(v.into());
79 self
80 }
81
82 #[must_use]
84 pub fn image_ref(mut self, v: &str) -> Self {
85 self.image_ref = Some(v.into());
86 self
87 }
88
89 #[must_use]
91 pub fn name(mut self, v: &str) -> Self {
92 self.name = Some(v.into());
93 self
94 }
95
96 #[must_use]
98 pub fn revision(mut self, v: i32) -> Self {
99 self.revision = Some(v);
100 self
101 }
102
103 #[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 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}