wick_interface_types/signatures/
operation.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{contents_equal, Field, Type};
4
5/// The signature of a Wick component, including its input and output types.
6#[derive(Debug, Clone, Default, Serialize, Deserialize, Eq)]
7#[must_use]
8#[non_exhaustive]
9pub struct OperationSignature {
10  /// The name of the component.
11  #[serde(default)]
12  pub name: String,
13
14  /// The operation's configuration.
15  #[serde(default, skip_serializing_if = "Vec::is_empty")]
16  pub config: Vec<Field>,
17
18  /// The component's inputs.
19  #[serde(default)]
20  #[serde(skip_serializing_if = "Vec::is_empty")]
21  pub inputs: Vec<Field>,
22
23  /// The component's outputs.
24  #[serde(default)]
25  #[serde(skip_serializing_if = "Vec::is_empty")]
26  pub outputs: Vec<Field>,
27}
28
29impl PartialEq for OperationSignature {
30  fn eq(&self, other: &Self) -> bool {
31    self.name == other.name
32      && contents_equal(&self.inputs, &other.inputs)
33      && contents_equal(&self.outputs, &other.outputs)
34  }
35}
36
37impl OperationSignature {
38  pub fn new<T: Into<String>>(name: T, inputs: Vec<Field>, outputs: Vec<Field>, config: Vec<Field>) -> Self {
39    Self {
40      name: name.into(),
41      config,
42      inputs,
43      outputs,
44    }
45  }
46
47  /// Get the name of the operation.
48  #[must_use]
49  pub fn name(&self) -> &str {
50    &self.name
51  }
52
53  /// Get the operation's configuration.
54  #[must_use]
55  pub fn config(&self) -> &[Field] {
56    &self.config
57  }
58
59  /// Get the operation's inputs.
60  #[must_use]
61  pub fn inputs(&self) -> &[Field] {
62    &self.inputs
63  }
64
65  /// Get the operation's outputs.
66  #[must_use]
67  pub fn outputs(&self) -> &[Field] {
68    &self.outputs
69  }
70
71  /// Create a new [OperationSignature] with the passed name.
72  pub fn new_named<T: Into<String>>(name: T) -> Self {
73    Self {
74      name: name.into(),
75      ..Default::default()
76    }
77  }
78
79  /// Add an input port.
80  pub fn add_input<T: Into<String>>(mut self, name: T, ty: Type) -> Self {
81    self.inputs.push(Field::new(name, ty));
82    self
83  }
84
85  /// Add an input port.
86  pub fn add_output<T: Into<String>>(mut self, name: T, ty: Type) -> Self {
87    self.outputs.push(Field::new(name, ty));
88    self
89  }
90}