wick_interface_types/signatures/
component.rs1use serde::{Deserialize, Serialize};
2
3use crate::{contents_equal, Field, OperationSignature, TypeDefinition};
4
5#[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, derive_builder::Builder)]
7#[builder(default)]
8#[must_use]
9#[non_exhaustive]
10pub struct ComponentSignature {
11 pub name: Option<String>,
13
14 pub format: ComponentVersion,
16
17 #[serde(default)]
19 pub metadata: ComponentMetadata,
20
21 #[serde(default, skip_serializing_if = "Vec::is_empty")]
23 pub wellknown: Vec<WellKnownSchema>,
24
25 #[serde(default, skip_serializing_if = "Vec::is_empty")]
27 pub types: Vec<TypeDefinition>,
28
29 #[serde(default, skip_serializing_if = "Vec::is_empty")]
31 pub operations: Vec<OperationSignature>,
32
33 #[serde(default, skip_serializing_if = "Vec::is_empty")]
35 pub config: Vec<Field>,
36}
37
38impl PartialEq for ComponentSignature {
39 fn eq(&self, other: &Self) -> bool {
40 let types_equal = contents_equal(&self.types, &other.types);
41 let operations_equal = contents_equal(&self.operations, &other.operations);
42 let config_equal = contents_equal(&self.config, &other.config);
43 let wellknown_equal = contents_equal(&self.wellknown, &other.wellknown);
44
45 self.format == other.format && types_equal && operations_equal && config_equal && wellknown_equal
46 }
47}
48
49impl ComponentSignature {
50 pub fn new<T: Into<String>>(
52 name: T,
53 version: Option<String>,
54 operations: Vec<OperationSignature>,
55 types: Vec<TypeDefinition>,
56 config: Vec<Field>,
57 ) -> Self {
58 Self {
59 name: Some(name.into()),
60 metadata: ComponentMetadata::new(version),
61 operations,
62 types,
63 config,
64 ..Default::default()
65 }
66 }
67
68 pub fn new_named<T: Into<String>>(name: T) -> Self {
70 Self {
71 name: Some(name.into()),
72 ..Default::default()
73 }
74 }
75
76 #[must_use]
77 pub fn get_operation(&self, operation_name: &str) -> Option<&OperationSignature> {
79 self.operations.iter().find(|op| op.name == operation_name)
80 }
81
82 pub fn add_operation(mut self, signature: OperationSignature) -> Self {
84 self.operations.push(signature);
85 self
86 }
87
88 pub fn set_version<T: Into<String>>(mut self, version: T) -> Self {
90 self.metadata.version = Some(version.into());
91 self
92 }
93
94 pub const fn format(mut self, format: ComponentVersion) -> Self {
96 self.format = format;
97 self
98 }
99
100 #[allow(clippy::missing_const_for_fn)]
102 pub fn metadata(self, features: ComponentMetadata) -> Self {
103 Self {
104 metadata: features,
105 ..self
106 }
107 }
108}
109
110#[derive(Debug, Clone, Copy, PartialEq, Eq, serde_repr::Deserialize_repr, serde_repr::Serialize_repr)]
111#[must_use]
112#[non_exhaustive]
113#[repr(u32)]
114pub enum ComponentVersion {
116 V0 = 0,
118 V1 = 1,
120}
121
122impl Default for ComponentVersion {
123 fn default() -> Self {
124 Self::V1
125 }
126}
127
128impl From<ComponentVersion> for u32 {
129 fn from(v: ComponentVersion) -> Self {
130 match v {
131 ComponentVersion::V0 => 0,
132 ComponentVersion::V1 => 1,
133 }
134 }
135}
136
137#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
138#[must_use]
139#[non_exhaustive]
140pub struct ComponentMetadata {
142 #[serde(skip_serializing_if = "Option::is_none")]
144 pub version: Option<String>,
145}
146
147impl ComponentMetadata {
148 pub const fn new(version: Option<String>) -> Self {
149 Self { version }
150 }
151}
152
153#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
155#[non_exhaustive]
156pub struct WellKnownSchema {
157 pub capabilities: Vec<String>,
159 pub url: String,
161 pub schema: ComponentSignature,
163}