1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! Data types used when dealing with components on a wasmCloud lattice

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::{ComponentId, Result};

/// A summary description of an component within a host inventory
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct ComponentDescription {
    /// The unique component identifier for this component
    #[serde(default)]
    pub(crate) id: ComponentId,

    /// Image reference for this component
    #[serde(default)]
    pub(crate) image_ref: String,

    /// Name of this component, if one exists
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) name: Option<String>,

    /// The annotations that were used in the start request that produced
    /// this component instance
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) annotations: Option<BTreeMap<String, String>>,

    /// The revision number for this component instance
    #[serde(default)]
    pub(crate) revision: i32,

    /// The maximum number of concurrent requests this instance can handle
    #[serde(default)]
    pub(crate) max_instances: u32,
}

#[derive(Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ComponentDescriptionBuilder {
    id: Option<ComponentId>,
    image_ref: Option<String>,
    name: Option<String>,
    annotations: Option<BTreeMap<String, String>>,
    revision: Option<i32>,
    max_instances: Option<u32>,
}

impl ComponentDescriptionBuilder {
    #[must_use]
    pub fn id(mut self, v: String) -> Self {
        self.id = Some(v);
        self
    }

    #[must_use]
    pub fn image_ref(mut self, v: String) -> Self {
        self.image_ref = Some(v);
        self
    }

    #[must_use]
    pub fn name(mut self, v: String) -> Self {
        self.name = Some(v);
        self
    }

    #[must_use]
    pub fn revision(mut self, v: i32) -> Self {
        self.revision = Some(v);
        self
    }

    #[must_use]
    pub fn max_instances(mut self, v: u32) -> Self {
        self.max_instances = Some(v);
        self
    }

    #[must_use]
    pub fn annotations(mut self, v: BTreeMap<String, String>) -> Self {
        self.annotations = Some(v);
        self
    }

    pub fn build(self) -> Result<ComponentDescription> {
        Ok(ComponentDescription {
            image_ref: self
                .image_ref
                .ok_or_else(|| "image_ref is required".to_string())?,
            id: self.id.ok_or_else(|| "id is required".to_string())?,
            name: self.name,
            revision: self.revision.unwrap_or_default(),
            max_instances: self.max_instances.unwrap_or_default(),
            annotations: self.annotations,
        })
    }
}

impl ComponentDescription {
    /// Get the ID of the component
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Get the image reference of the component
    pub fn image_ref(&self) -> &str {
        &self.image_ref
    }

    /// Get the name of the component
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// Get the annotations of the component
    pub fn annotations(&self) -> Option<&BTreeMap<String, String>> {
        self.annotations.as_ref()
    }

    /// Get the revision of the component
    pub fn revision(&self) -> i32 {
        self.revision
    }

    /// Get the revision of the component
    pub fn max_instances(&self) -> u32 {
        self.max_instances
    }

    #[must_use]
    pub fn builder() -> ComponentDescriptionBuilder {
        ComponentDescriptionBuilder::default()
    }
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct ComponentInstance {
    /// The annotations that were used in the start request that produced
    /// this component instance
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) annotations: Option<BTreeMap<String, String>>,

    /// Image reference for this component
    #[serde(default)]
    pub(crate) image_ref: String,

    /// This instance's unique ID (guid)
    #[serde(default)]
    pub(crate) instance_id: String,

    /// The revision number for this component instance
    #[serde(default)]
    pub(crate) revision: i32,

    /// The maximum number of concurrent requests this instance can handle
    #[serde(default)]
    pub(crate) max_instances: u32,
}

impl ComponentInstance {
    /// Get the image reference of the component instance
    pub fn image_ref(&self) -> &str {
        &self.image_ref
    }

    /// Get the image ID of the component instance
    pub fn instance_id(&self) -> &str {
        &self.instance_id
    }

    /// Get the annotations of the component
    pub fn annotations(&self) -> Option<&BTreeMap<String, String>> {
        self.annotations.as_ref()
    }

    /// Get the revision of the component
    pub fn revision(&self) -> i32 {
        self.revision
    }

    /// Get the revision of the component
    pub fn max_instances(&self) -> u32 {
        self.max_instances
    }

    #[must_use]
    pub fn builder() -> ComponentInstanceBuilder {
        ComponentInstanceBuilder::default()
    }
}

#[derive(Default, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ComponentInstanceBuilder {
    instance_id: Option<String>,
    image_ref: Option<String>,
    revision: Option<i32>,
    max_instances: Option<u32>,
    annotations: Option<BTreeMap<String, String>>,
}

impl ComponentInstanceBuilder {
    #[must_use]
    pub fn instance_id(mut self, v: String) -> Self {
        self.instance_id = Some(v);
        self
    }

    #[must_use]
    pub fn image_ref(mut self, v: String) -> Self {
        self.image_ref = Some(v);
        self
    }

    #[must_use]
    pub fn revision(mut self, v: i32) -> Self {
        self.revision = Some(v);
        self
    }

    #[must_use]
    pub fn max_instances(mut self, v: u32) -> Self {
        self.max_instances = Some(v);
        self
    }

    #[must_use]
    pub fn annotations(mut self, v: BTreeMap<String, String>) -> Self {
        self.annotations = Some(v);
        self
    }

    pub fn build(self) -> Result<ComponentInstance> {
        Ok(ComponentInstance {
            image_ref: self
                .image_ref
                .ok_or_else(|| "image_ref is required".to_string())?,
            instance_id: self
                .instance_id
                .ok_or_else(|| "id is required".to_string())?,
            revision: self.revision.unwrap_or_default(),
            max_instances: self.max_instances.unwrap_or_default(),
            annotations: self.annotations,
        })
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use super::{ComponentDescription, ComponentInstance};

    #[test]
    fn component_desc_builder() {
        assert_eq!(
            ComponentDescription {
                id: "id".into(),
                image_ref: "ref".into(),
                name: Some("name".into()),
                annotations: Some(BTreeMap::from([("a".into(), "b".into())])),
                revision: 0,
                max_instances: 1,
            },
            ComponentDescription::builder()
                .id("id".into())
                .name("test".into())
                .image_ref("ref".into())
                .name("name".into())
                .annotations(BTreeMap::from([("a".into(), "b".into())]))
                .revision(0)
                .max_instances(1)
                .build()
                .unwrap()
        )
    }

    #[test]
    fn component_instance_builder() {
        assert_eq!(
            ComponentInstance {
                instance_id: "id".into(),
                image_ref: "ref".into(),
                annotations: Some(BTreeMap::from([("a".into(), "b".into())])),
                revision: 0,
                max_instances: 1,
            },
            ComponentInstance::builder()
                .instance_id("id".into())
                .image_ref("ref".into())
                .annotations(BTreeMap::from([("a".into(), "b".into())]))
                .revision(0)
                .max_instances(1)
                .build()
                .unwrap()
        )
    }
}