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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use field::Field;
use method::Method;

/// Actions show available behaviors an entity exposes.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Action {
    /// Describes the nature of an action based on the current representation.
    /// Possible values are implementation-dependent and should be documented.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub class: Option<Vec<String>>,

    /// A collection of fields, expressed as an array of objects in JSON Siren such as `{ "fields" : [{ ... }] }`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fields: Option<Vec<Field>>,

    /// A string that identifies the action to be performed.
    /// Action names MUST be unique within the set of actions for an entity.
    /// The behavior of clients when parsing a Siren document that violates this constraint is undefined. 
    pub name: String,

    /// The URI of the action.
    pub href: String,

    /// An enumerated attribute mapping to a protocol method.
    /// For HTTP, these values may be `GET`, `PUT`, `POST`, `DELETE`, or `PATCH`.
    /// As new methods are introduced, this list can be extended.
    /// If this attribute is omitted, `GET` should be assumed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub method: Option<Method>,

    /// Descriptive text about the action.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// The encoding type for the request.
    /// When omitted and the fields attribute exists, the default value is `application/x-www-form-urlencoded`.
    #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
    pub typ: Option<String>,
}

impl Action {
    /// Create a Action builder with the given name and href.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action = Action::builder("add-item", "http://api.x.io/orders/42/items");
    /// ```
    pub fn builder(name: impl Into<String>, href: impl Into<String>) -> ActionBuilder {
        ActionBuilder {
            class: None,
            fields: None,
            name: name.into(),
            href: href.into(),
            method: None,
            title: None,
            typ: None,
        }
    }

    /// Returns a reference to the Action's Classes.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .classes(vec!["item"]).into();
    /// 
    /// assert_eq!(&Some(vec!["item".to_string()]), action.classes());
    /// ```
    pub fn classes(&self) -> &Option<Vec<String>> {
        &self.class
    }

    /// Returns a reference to the Action's Fields.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .fields(vec![
    ///         Field::builder("orderNumber").typ("hidden").value("42"),
    ///         Field::builder("productCode").typ("text"),
    ///         Field::builder("quantity").typ("number"),
    ///     ]).into();
    /// 
    /// assert_eq!(
    ///     &Some(vec![
    ///         Field::builder("orderNumber").typ("hidden").value("42").into(),
    ///         Field::builder("productCode").typ("text").into(),
    ///         Field::builder("quantity").typ("number").into(),
    ///     ]),
    ///     action.fields(),
    /// );
    /// ```
    pub fn fields(&self) -> &Option<Vec<Field>> {
        &self.fields
    }

    /// Returns a reference to the Action's Method.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .method(Method::Post).into();
    /// 
    /// assert_eq!(&Some(Method::Post), action.method());
    /// ```
    pub fn method(&self) -> &Option<Method> {
        &self.method
    }

    /// Returns a reference to the Action's title.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .title("Add Item").into();
    /// 
    /// assert_eq!(&Some("Add Item".to_string()), action.title());
    /// ```
    pub fn title(&self) -> &Option<String> {
        &self.title
    }

    /// Returns a reference to the Action's type.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .typ("application/x-www-form-urlencoded").into();
    /// 
    /// assert_eq!(&Some("application/x-www-form-urlencoded".to_string()), action.typ());
    /// ```
    // Change this to an enum?
    pub fn typ(&self) -> &Option<String> {
        &self.typ
    }
}

/// A builder of Actions
#[derive(Clone, Debug, PartialEq)]
pub struct ActionBuilder {
    /// Describes the nature of an action based on the current representation.
    /// Possible values are implementation-dependent and should be documented.
    pub class: Option<Vec<String>>,

    /// A collection of fields, expressed as an array of objects in JSON Siren such as `{ "fields" : [{ ... }] }`.
    pub fields: Option<Vec<Field>>,

    /// A string that identifies the action to be performed.
    /// Action names MUST be unique within the set of actions for an entity.
    /// The behavior of clients when parsing a Siren document that violates this constraint is undefined. 
    pub name: String,

    /// The URI of the action.
    pub href: String,

    /// An enumerated attribute mapping to a protocol method.
    /// For HTTP, these values may be `GET`, `PUT`, `POST`, `DELETE`, or `PATCH`.
    /// As new methods are introduced, this list can be extended.
    /// If this attribute is omitted, `GET` should be assumed.
    pub method: Option<Method>,

    /// Descriptive text about the action.
    pub title: Option<String>,

    /// The encoding type for the request.
    /// When omitted and the fields attribute exists, the default value is `application/x-www-form-urlencoded`.
    pub typ: Option<String>,
}

impl ActionBuilder {
    /// Add a Class to the Action.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .class("item").into();
    /// 
    /// assert_eq!(&Some(vec!["item".to_string()]), action.classes());
    /// ```
    pub fn class(self, class: impl Into<String>) -> Self {
        self.classes(vec![class])
    }

    /// Add a vector of Classes to the Action.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .classes(vec!["item"]).into();
    /// 
    /// assert_eq!(&Some(vec!["item".to_string()]), action.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
    }

    /// Add a Field to the Action.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .field(Field::builder("orderNumber").typ("hidden").value("42"))
    ///     .field(Field::builder("productCode").typ("text"))
    ///     .field(Field::builder("quantity").typ("number"))
    ///     .into();
    /// 
    /// assert_eq!(
    ///     &Some(vec![
    ///         Field::builder("orderNumber").typ("hidden").value("42").into(),
    ///         Field::builder("productCode").typ("text").into(),
    ///         Field::builder("quantity").typ("number").into(),
    ///     ]),
    ///     action.fields(),
    /// );
    /// ```
    pub fn field(self, field: impl Into<Field>) -> Self {
        self.fields(vec![field])
    }

    /// Add a vector of Fields to the Action.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .fields(vec![
    ///         Field::builder("orderNumber").typ("hidden").value("42"),
    ///         Field::builder("productCode").typ("text"),
    ///         Field::builder("quantity").typ("number"),
    ///     ]).into();
    /// 
    /// assert_eq!(
    ///     &Some(vec![
    ///         Field::builder("orderNumber").typ("hidden").value("42").into(),
    ///         Field::builder("productCode").typ("text").into(),
    ///         Field::builder("quantity").typ("number").into(),
    ///     ]),
    ///     action.fields(),
    /// );
    /// ```
    pub fn fields(mut self, fields: Vec<impl Into<Field>>) -> Self {
        if let Some(ref mut s_fields) = self.fields {
            for field in fields.into_iter() {
                s_fields.push(field.into());
            }
        } else {
            let mut s_fields = Vec::new();

            for field in fields.into_iter() {
                s_fields.push(field.into());
            }

            self.fields = Some(s_fields);
        }

        self
    }

    /// Set the Method of the Action.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .method(Method::Post).into();
    /// 
    /// assert_eq!(&Some(Method::Post), action.method());
    /// ```
    pub fn method(mut self, method: Method) -> Self {
        self.method = Some(method);

        self
    }

    /// Set the title of the Action.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .title("Add Item").into();
    /// 
    /// assert_eq!(&Some("Add Item".to_string()), action.title());
    /// ```
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());

        self
    }

    /// Set the Action's type.
    /// 
    /// # Examples
    /// 
    /// ```rust
    /// # extern crate vnd_siren;
    /// # use vnd_siren::prelude::*;
    /// let action: Action = Action::builder("add-item", "http://api.x.io/orders/42/items")
    ///     .typ("application/x-www-form-urlencoded")
    ///     .into();
    /// 
    /// assert_eq!(&Some("application/x-www-form-urlencoded".to_string()), action.typ());
    /// ```
    pub fn typ(mut self, typ: impl Into<String>) -> Self {
        self.typ = Some(typ.into());

        self
    }
}

impl From<ActionBuilder> for Action {
    fn from(builder: ActionBuilder) -> Action {
        Action {
            class: builder.class,
            fields: builder.fields,
            name: builder.name,
            href: builder.href,
            method: builder.method,
            title: builder.title,
            typ: builder.typ,
        }
    }
}