wick_interface_types/signatures/
component.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{contents_equal, Field, OperationSignature, TypeDefinition};
4
5/// Signature for Collections.
6#[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, derive_builder::Builder)]
7#[builder(default)]
8#[must_use]
9#[non_exhaustive]
10pub struct ComponentSignature {
11  /// Name of the collection.
12  pub name: Option<String>,
13
14  /// The format of the component signature.
15  pub format: ComponentVersion,
16
17  /// Component implementation version.
18  #[serde(default)]
19  pub metadata: ComponentMetadata,
20
21  /// A map of type signatures referenced elsewhere.
22  #[serde(default, skip_serializing_if = "Vec::is_empty")]
23  pub wellknown: Vec<WellKnownSchema>,
24
25  /// A map of type signatures referenced elsewhere.
26  #[serde(default, skip_serializing_if = "Vec::is_empty")]
27  pub types: Vec<TypeDefinition>,
28
29  /// A list of [OperationSignature]s in this component.
30  #[serde(default, skip_serializing_if = "Vec::is_empty")]
31  pub operations: Vec<OperationSignature>,
32
33  /// The component's configuration for this implementation.
34  #[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  /// Create a new [ComponentSignature] with the passed name.
51  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  /// Create a new [ComponentSignature] with the passed name.
69  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  /// Get the [OperationSignature] for the requested component.
78  pub fn get_operation(&self, operation_name: &str) -> Option<&OperationSignature> {
79    self.operations.iter().find(|op| op.name == operation_name)
80  }
81
82  /// Add a [OperationSignature] to the collection.
83  pub fn add_operation(mut self, signature: OperationSignature) -> Self {
84    self.operations.push(signature);
85    self
86  }
87
88  /// Set the version of the [ComponentSignature].
89  pub fn set_version<T: Into<String>>(mut self, version: T) -> Self {
90    self.metadata.version = Some(version.into());
91    self
92  }
93
94  /// Set the format of the [ComponentSignature].
95  pub const fn format(mut self, format: ComponentVersion) -> Self {
96    self.format = format;
97    self
98  }
99
100  /// Set the features of the [ComponentSignature].
101  #[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)]
114/// The umbrella version of the component.
115pub enum ComponentVersion {
116  /// Version 0 Wick components.
117  V0 = 0,
118  /// Version 1 Wick components.
119  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]
140/// The Wick features this collection supports.
141pub struct ComponentMetadata {
142  /// Version of the component.
143  #[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/// An entry from a well-known schema
154#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
155#[non_exhaustive]
156pub struct WellKnownSchema {
157  /// The capability the schema provides.
158  pub capabilities: Vec<String>,
159  /// The location where you can find and validate the schema.
160  pub url: String,
161  /// The schema itself.
162  pub schema: ComponentSignature,
163}