1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Field {
#[serde(skip_serializing_if = "Option::is_none")]
class: Option<Vec<String>>,
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "type")]
typ: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<String>,
}
impl Field {
pub fn builder(name: impl Into<String>) -> FieldBuilder {
FieldBuilder {
class: None,
name: name.into(),
title: None,
typ: None,
value: None,
}
}
/// Returns a reference to the Fields's Classes.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let field: Field = Field::builder("orderNumber")
/// .classes(vec!["item"]).into();
///
/// assert_eq!(&Some(vec!["item".to_string()]), field.classes());
/// ```
pub fn classes(&self) -> &Option<Vec<String>> {
&self.class
}
/// Returns a reference to the Fields's title.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let field: Field = Field::builder("orderNumber")
/// .title("field").into();
///
/// assert_eq!(&Some("field".to_string()), field.title());
/// ```
pub fn title(&self) -> &Option<String> {
&self.title
}
/// Returns a reference to the Fields's type.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let field: Field = Field::builder("orderNumber")
/// .typ("number").into();
///
/// assert_eq!(&Some("number".to_string()), field.typ());
/// ```
pub fn typ(&self) -> &Option<String> {
&self.typ
}
/// Returns a reference to the Fields's value.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let field: Field = Field::builder("orderNumber")
/// .value("42").into();
///
/// assert_eq!(&Some("42".to_string()), field.value());
/// ```
pub fn value(&self) -> &Option<String> {
&self.value
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct FieldBuilder {
class: Option<Vec<String>>,
name: String,
title: Option<String>,
typ: Option<String>,
value: Option<String>,
}
impl FieldBuilder {
/// Add a Class to the Field.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let field: Field = Field::builder("orderNumber")
/// .class("item").into();
///
/// assert_eq!(&Some(vec!["item".to_string()]), field.classes());
/// ```
pub fn class(self, class: impl Into<String>) -> Self {
self.classes(vec![class])
}
/// Add a vector of Classes to the Field.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let field: Field = Field::builder("orderNumber")
/// .classes(vec!["item"]).into();
///
/// assert_eq!(&Some(vec!["item".to_string()]), field.classes());
/// ```
pub fn classes(mut self, classes: Vec<impl Into<String>>) -> Self {
if let Some(ref mut s_class) = self.class {
for class in classes.into_iter() {
s_class.push(class.into());
}
} else {
let mut s_class = Vec::new();
for class in classes.into_iter() {
s_class.push(class.into());
}
self.class = Some(s_class)
}
self
}
/// Set the title of the Field.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let field: Field = Field::builder("orderNumber")
/// .title("field").into();
///
/// assert_eq!(&Some("field".to_string()), field.title());
/// ```
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
/// Set the Field's type.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let field: Field = Field::builder("orderNumber")
/// .typ("number")
/// .into();
///
/// assert_eq!(&Some("number".to_string()), field.typ());
/// ```
pub fn typ(mut self, typ: impl Into<String>) -> Self {
self.typ = Some(typ.into());
self
}
/// Sets the Field's value.
///
/// # Examples
///
/// ```rust
/// # extern crate vnd_siren;
/// # use vnd_siren::prelude::*;
/// let field: Field = Field::builder("orderNumber")
/// .value("42").into();
///
/// assert_eq!(&Some("42".to_string()), field.value());
/// ```
pub fn value(mut self, value: impl Into<String>) -> Self {
self.value = Some(value.into());
self
}
}
impl From<FieldBuilder> for Field {
fn from(builder: FieldBuilder) -> Field {
Field {
class: builder.class,
name: builder.name,
title: builder.title,
typ: builder.typ,
value: builder.value,
}
}
}