smartsheet-rs 0.6.1

Async Smartsheet API implementation in Rust
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use crate::models::{AccessLevel, AttachmentMeta, Cell, Column, Discussion, IndentEnabled, User};
use crate::types::Result;
use crate::utils::is_default;

use core::option::Option;
use std::io::{Error, ErrorKind};

use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Row {
    /// Row Id
    #[serde(skip_serializing_if = "is_default")]
    pub id: u64,
    /// Parent Sheet Id
    #[serde(skip_serializing)]
    pub sheet_id: Option<u64>,
    /// Enum: ADMIN, EDITOR, EDITOR_SHARE, OWNER, VIEWER
    #[serde(skip_serializing)]
    pub access_level: Option<AccessLevel>,
    /// Attachments on row. Only returned if the include query string
    /// parameter contains attachments.
    #[serde(skip_serializing)]
    pub attachments: Option<Vec<AttachmentMeta>>,
    /// Cells belonging to the row.
    pub cells: Vec<Cell>,
    /// Columns of row. Only returned if the include query string parameter
    /// contains columns.
    #[serde(default)]
    #[serde(skip_serializing)]
    pub columns: Vec<Column>,
    /// Describes this row's conditional format. Only returned if the include
    /// query string parameter contains format and this row has a conditional
    /// format applied.
    #[serde(skip_serializing)]
    pub conditional_format: Option<String>,
    /// string or number
    #[serde(skip_serializing)]
    pub created_at: String,
    /// User object containing name and email of the creator of this row.
    #[serde(skip_serializing)]
    pub created_by: Option<User>,
    /// Discussions on the row. Only returned if the include query string
    /// parameter contains discussions.
    #[serde(skip_serializing)]
    pub discussions: Option<Vec<Discussion>>,
    /// Indicates whether the row is expanded or collapsed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expanded: Option<bool>,
    /// Indicates if the row is filtered out by a column filter. Only returned
    /// if the include query string parameter contains filters.
    #[serde(skip_serializing)]
    pub filtered_out: Option<bool>,
    /// Format descriptor. Only returned if the include query string parameter
    /// contains format and this row has a non-default format applied.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,
    /// Only returned, with a value of true, if the sheet is a project sheet
    /// with dependencies enabled and this row is in the critical path.
    #[serde(skip_serializing)]
    pub in_critical_path: Option<bool>,
    /// Indicates whether the row is locked.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locked: Option<bool>,
    /// Indicates whether the row is locked for the requesting user.
    #[serde(skip_serializing)]
    pub locked_for_user: Option<bool>,
    /// string or number
    #[serde(skip_serializing)]
    pub modified_at: String,
    /// User object containing name and email of the last person to modify
    /// this row.
    #[serde(skip_serializing)]
    pub modified_by: Option<User>,
    /// URL that represents a direct link to the row in Smartsheet. Only
    /// returned if the include query string parameter contains `rowPermalink`.
    #[serde(skip_serializing)]
    pub permalink: Option<String>,
    /// Row number within the sheet.
    #[serde(skip_serializing)]
    pub row_number: u64,
    /// Sheet version number that is incremented every time a sheet is modified.
    #[serde(skip_serializing)]
    pub version: Option<u64>,
    // TODO: Add Proof field
    // Proof Object
    // pub proofs: Proofs
    /// # Note
    ///
    /// The following are used in the [Row Location] specified attributes.
    ///
    /// [Row Location]: https://smartsheet.redoc.ly/#section/Specify-Row-Location

    /// Sibling Row Id
    ///
    /// Also used to [specify row location] when adding/updating rows.
    ///
    /// [specify row location]: https://smartsheet.redoc.ly/#section/Specify-Row-Location
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sibling_id: Option<u64>,
    /// Parent Id, used to [specify row location] when adding/updating rows.
    ///
    /// [specify row location]: https://smartsheet.redoc.ly/#section/Specify-Row-Location
    #[serde(skip_deserializing)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<u64>,
    /// Row Location Specifier: Top of a sheet
    #[serde(skip_deserializing)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to_top: Option<bool>,
    /// Row Location Specifier: Bottom of a sheet
    #[serde(skip_deserializing)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to_bottom: Option<bool>,
    /// Row Location Specifier: Indent one existing row, must have a value of "1"
    #[serde(skip_deserializing)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub indent: Option<IndentEnabled>,
    /// Row Location Specifier: Outdent one existing row, must have a value of "1"
    #[serde(skip_deserializing)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub outdent: Option<IndentEnabled>,
}

impl<const N: usize> From<&[Cell; N]> for Row {
    fn from(cells: &[Cell; N]) -> Self {
        Self {
            cells: cells.to_vec(),
            ..Default::default()
        }
    }
}

impl From<Vec<Cell>> for Row {
    fn from(cells: Vec<Cell>) -> Self {
        Self {
            cells,
            ..Default::default()
        }
    }
}

impl Row {
    pub fn with_cells<C: Into<Vec<Cell>>>(cells: C) -> Self {
        Row {
            cells: cells.into(),
            ..Default::default()
        }
    }

    pub fn with_id_and_cells<C: Into<Vec<Cell>>>(row_id: u64, cells: C) -> Self {
        Row {
            id: row_id,
            cells: cells.into(),
            ..Default::default()
        }
    }

    pub fn with_id_and_cells_slice<const N: usize>(row_id: u64, cells: &[Cell; N]) -> Self {
        Row {
            id: row_id,
            cells: cells.to_vec(),
            ..Default::default()
        }
    }

    /// Retrieve a specified `Cell` - for a given *column id* - from the `Row`
    pub fn get_cell_by_id(&self, column_id: u64) -> Result<&Cell> {
        return match self.cells.iter().find(|cell| cell.column_id == column_id) {
            Some(cell) => Ok(cell),
            None => Err(Box::from(Error::new(
                ErrorKind::NotFound,
                "No cell found for the given Column ID or Name",
            ))),
        };
    }

    /// Fluent setter for the `id` attribute (Row Id)
    pub fn id(mut self, id: u64) -> Self {
        self.id = id;
        self
    }

    /// Fluent setter for the `expanded` attribute
    pub fn expanded<B: Into<Option<bool>>>(mut self, expanded: B) -> Self {
        self.expanded = expanded.into();
        self
    }

    /// Fluent setter for the `format` attribute
    pub fn format<S: Into<String>>(mut self, format: S) -> Self {
        self.format = Some(format.into());
        self
    }

    /// Fluent setter for the `locked` attribute
    pub fn locked<B: Into<Option<bool>>>(mut self, locked: B) -> Self {
        self.locked = locked.into();
        self
    }

    /// Fluent setter for the `sibling_id` attribute
    pub fn sibling_id<U: Into<Option<u64>>>(mut self, sibling_id: U) -> Self {
        self.sibling_id = sibling_id.into();
        self
    }

    /// Fluent setter for the `parent_id` attribute
    pub fn parent_id<U: Into<Option<u64>>>(mut self, parent_id: U) -> Self {
        self.parent_id = parent_id.into();
        self
    }

    /// Fluent setter for the `to_top` attribute
    pub fn to_top<B: Into<Option<bool>>>(mut self, to_top: B) -> Self {
        self.to_top = to_top.into();
        self
    }

    /// Fluent setter for the `to_bottom` attribute
    pub fn to_bottom<B: Into<Option<bool>>>(mut self, to_bottom: B) -> Self {
        self.to_bottom = to_bottom.into();
        self
    }

    /// Fluent setter for the `indent` attribute
    pub fn indent<I: Into<Option<IndentEnabled>>>(mut self, indent: I) -> Self {
        self.indent = indent.into();
        self
    }

    /// Fluent setter for the `outdent` attribute
    pub fn outdent<I: Into<Option<IndentEnabled>>>(mut self, outdent: I) -> Self {
        self.outdent = outdent.into();
        self
    }
}

impl From<Row> for Vec<Row> {
    /// Useful when adding / updating row(s) to a sheet.
    fn from(row: Row) -> Self {
        vec![row]
    }
}

/// *Row Location Specifier* - A trait which allows us to define the
/// [Row Location] specified attributes for a collection (array or vector) of
/// `Row` objects.
///
/// [Row Location]: https://smartsheet.redoc.ly/#section/Specify-Row-Location
pub trait RowLocationSpecifier {
    /// Fluent setter for the `sibling_id` attribute
    fn sibling_id<U: Into<Option<u64>>>(&mut self, sibling_id: U) -> &mut Self;
    /// Fluent setter for the `parent_id` attribute
    fn parent_id<U: Into<Option<u64>>>(&mut self, parent_id: U) -> &mut Self;
    /// Fluent setter for the `to_top` attribute
    fn to_top<B: Into<Option<bool>>>(&mut self, to_top: B) -> &mut Self;
    /// Fluent setter for the `to_bottom` attribute
    fn to_bottom<B: Into<Option<bool>>>(&mut self, to_bottom: B) -> &mut Self;
    /// Fluent setter for the `indent` attribute
    fn indent<I: Into<Option<IndentEnabled>>>(&mut self, indent: I) -> &mut Self;
    /// Fluent setter for the `outdent` attribute
    fn outdent<I: Into<Option<IndentEnabled>>>(&mut self, outdent: I) -> &mut Self;
}

macro_rules! impl_row_location_specifier {
    (for $($t:ty),+) => {
        $(impl RowLocationSpecifier for $t {

            fn sibling_id<U: Into<Option<u64>>>(&mut self, sibling_id: U) -> &mut Self {
                let sibling_id = sibling_id.into();
                for row in self.iter_mut() {
                    row.sibling_id = sibling_id;
                }
                self
            }

            fn parent_id<U: Into<Option<u64>>>(&mut self, parent_id: U) -> &mut Self {
                let parent_id = parent_id.into();
                for row in self.iter_mut() {
                    row.parent_id = parent_id;
                }
                self
            }

            fn to_top<B: Into<Option<bool>>>(&mut self, to_top: B) -> &mut Self {
                let to_top = to_top.into();
                for row in self.iter_mut() {
                    row.to_top = to_top;
                }
                self
            }

            fn to_bottom<B: Into<Option<bool>>>(&mut self, to_bottom: B) -> &mut Self {
                let to_bottom = to_bottom.into();
                for row in self.iter_mut() {
                    row.to_bottom = to_bottom;
                }
                self
            }

            fn indent<I: Into<Option<IndentEnabled>>>(&mut self, indent: I) -> &mut Self {
                let indent = indent.into();
                for row in self.iter_mut() {
                    row.indent = indent;
                }
                self
            }

            fn outdent<I: Into<Option<IndentEnabled>>>(&mut self, outdent: I) -> &mut Self {
                let outdent = outdent.into();
                for row in self.iter_mut() {
                    row.outdent = outdent;
                }
                self
            }
        })*
    }
}

impl_row_location_specifier!(for Vec<Row>, [Row]);

#[cfg(test)]
mod tests {
    use super::*;
    use indoc::indoc;
    use serde_json::to_string_pretty;

    #[test]
    fn test_expanded() {
        let row = Row::default();
        assert_eq!(row.expanded, None);

        let row = row.expanded(true);

        assert_eq!(row.expanded, Some(true));
    }

    #[test]
    fn test_format() {
        let row = Row::default();
        assert_eq!(row.format, None);

        let row = row.format("test");
        assert_eq!(row.format, Some("test".to_owned()));
    }

    /// Test that only the [required fields] are populated when serializing
    /// a `Row` object.
    ///
    /// [required fields]: https://smartsheet.redoc.ly/#tag/rows
    #[test]
    fn test_serialize_row() {
        let row = Row {
            id: 123,
            sheet_id: Some(321),
            access_level: Some(AccessLevel::Admin),
            attachments: Some(vec![]),
            cells: vec![],
            columns: vec![],
            conditional_format: Some("conditional fmt".to_owned()),
            created_at: "abc".to_owned(),
            created_by: Some(User {
                email: "a@b.com".to_owned(),
                name: Some("Test".to_owned()),
            }),
            discussions: Some(vec![]),
            expanded: Some(false),
            filtered_out: Some(true),
            format: Some("my fmt".to_owned()),
            in_critical_path: Some(true),
            locked: Some(false),
            locked_for_user: Some(true),
            modified_at: "abc".to_owned(),
            modified_by: Some(User {
                email: "z@a.com".to_owned(),
                name: Some("My Name".to_owned()),
            }),
            permalink: Some("test link".to_owned()),
            row_number: 123,
            version: Some(111),
            sibling_id: Some(123),
            parent_id: Some(321),
            to_top: Some(true),
            to_bottom: Some(true),
            indent: Some(IndentEnabled::TRUE),
            outdent: Some(IndentEnabled::TRUE),
        };

        let s = to_string_pretty(&row).unwrap();

        println!("{}", s);

        assert_eq!(
            s,
            indoc!(
                r#"
            {
              "id": 123,
              "cells": [],
              "expanded": false,
              "format": "my fmt",
              "locked": false,
              "siblingId": 123,
              "parentId": 321,
              "toTop": true,
              "toBottom": true,
              "indent": 1,
              "outdent": 1
            }
                "#
            )
            .trim()
        )
    }
}