wick_interface_types/
field.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{is_false, Type};
4
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
6#[non_exhaustive]
7pub struct Field {
8  /// The name of the field.
9  pub name: String,
10
11  /// The type of the field.
12  #[serde(rename = "type")]
13  #[cfg_attr(feature = "parser", serde(deserialize_with = "crate::types::deserialize_type"))]
14  #[cfg_attr(
15    feature = "yaml",
16    serde(serialize_with = "serde_yaml::with::singleton_map::serialize")
17  )]
18  pub ty: Type,
19
20  /// Whether the field is required.
21  #[cfg(feature = "value")]
22  #[serde(default, skip_serializing_if = "Option::is_none")]
23  pub default: Option<serde_json::Value>,
24
25  /// Whether the field is required.
26  #[serde(default, skip_serializing_if = "is_false")]
27  pub required: bool,
28
29  /// The description of the field.
30  #[serde(default, skip_serializing_if = "Option::is_none")]
31  pub description: Option<String>,
32}
33
34impl Field {
35  pub fn new<T: Into<String>>(name: T, ty: Type) -> Self {
36    Self::new_with_description(name, ty, None)
37  }
38
39  pub fn new_with_description<T: Into<String>>(name: T, ty: Type, desc: Option<String>) -> Self {
40    Self {
41      name: name.into(),
42      description: desc,
43      #[cfg(feature = "value")]
44      default: None,
45      required: !matches!(ty, Type::Optional { .. }),
46      ty,
47    }
48  }
49
50  /// Get the name of the field
51  #[must_use]
52  pub fn name(&self) -> &str {
53    &self.name
54  }
55
56  /// Get the type of the field
57  pub const fn ty(&self) -> &Type {
58    &self.ty
59  }
60
61  /// Get the description of the field
62  #[must_use]
63  pub fn description(&self) -> Option<&str> {
64    self.description.as_deref()
65  }
66
67  /// Get the default value of the field
68  #[must_use]
69  #[cfg(feature = "value")]
70  pub const fn default(&self) -> Option<&serde_json::Value> {
71    self.default.as_ref()
72  }
73
74  /// Get whether the field is required
75  #[must_use]
76  pub const fn required(&self) -> bool {
77    self.required
78  }
79
80  /// Consume the [Field] and return a [FieldValue] with the given value.
81  #[must_use]
82  #[cfg(feature = "value")]
83  pub fn with_value(self, value: impl Into<serde_json::Value>) -> FieldValue {
84    FieldValue::new(self, value.into())
85  }
86}
87
88impl std::fmt::Display for Field {
89  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90    f.write_str(&self.name)?;
91    f.write_str(": ")?;
92    self.ty.fmt(f)
93  }
94}
95
96/// A field and its value.
97#[cfg(feature = "value")]
98#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
99#[non_exhaustive]
100pub struct FieldValue {
101  /// The field.
102  pub field: Field,
103  /// The value of the field.
104  pub value: serde_json::Value,
105}
106
107#[cfg(feature = "value")]
108impl FieldValue {
109  /// Create a new field value.
110  #[must_use]
111  pub const fn new(field: Field, value: serde_json::Value) -> Self {
112    Self { field, value }
113  }
114
115  /// Get the name of the field
116  #[must_use]
117  pub fn name(&self) -> &str {
118    &self.field.name
119  }
120
121  /// Get the type of the field
122  pub const fn signature(&self) -> &Type {
123    &self.field.ty
124  }
125
126  /// Get the value of the field
127  #[must_use]
128  pub const fn value(&self) -> &serde_json::Value {
129    &self.value
130  }
131}