use serde::{Deserialize, Serialize};
use crate::{contents_equal, Field, Type};
#[derive(Debug, Clone, Default, Serialize, Deserialize, Eq)]
#[must_use]
#[non_exhaustive]
pub struct OperationSignature {
#[serde(default)]
pub name: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub config: Vec<Field>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub inputs: Vec<Field>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub outputs: Vec<Field>,
}
impl PartialEq for OperationSignature {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& contents_equal(&self.inputs, &other.inputs)
&& contents_equal(&self.outputs, &other.outputs)
}
}
impl OperationSignature {
pub fn new<T: Into<String>>(name: T, inputs: Vec<Field>, outputs: Vec<Field>, config: Vec<Field>) -> Self {
Self {
name: name.into(),
config,
inputs,
outputs,
}
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn config(&self) -> &[Field] {
&self.config
}
#[must_use]
pub fn inputs(&self) -> &[Field] {
&self.inputs
}
#[must_use]
pub fn outputs(&self) -> &[Field] {
&self.outputs
}
pub fn new_named<T: Into<String>>(name: T) -> Self {
Self {
name: name.into(),
..Default::default()
}
}
pub fn add_input<T: Into<String>>(mut self, name: T, ty: Type) -> Self {
self.inputs.push(Field::new(name, ty));
self
}
pub fn add_output<T: Into<String>>(mut self, name: T, ty: Type) -> Self {
self.outputs.push(Field::new(name, ty));
self
}
}