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
use crate::models::CellValue::{Boolean, Numeric, Text};
use crate::types::Result;

use core::fmt::Error;
use core::option::Option;
use core::option::Option::{None, Some};
use core::result::Result::{Err, Ok};
use serde::{Deserialize, Serialize};
use serde_json::Number;

/// Represents the `value` attribute in the `Cell` struct definition.
///
/// Will be transmitted as one of the following:
///     * text -- a `string` value
///     * number -- a numeric value, generally an `f64` type
///     * boolean -- a `bool` value
///
/// # Description
///
/// Per the docs, this value represents one of either a string, a number, or
/// a Boolean value -- depending on the cell type and the data in the cell.
/// Cell values larger than 4000 characters are silently truncated. An empty
/// cell returns no value.
///
/// # Docs
/// - https://smartsheet-platform.github.io/api-docs/#cell-object
/// - https://smartsheet-platform.github.io/api-docs/#cell-reference
///
#[derive(Clone, PartialEq, Deserialize, Debug, Serialize)]
#[serde(untagged)]
pub enum CellValue {
    Text(String),
    Boolean(bool),
    Numeric(Number),
}

impl CellValue {
    pub fn as_str(&self) -> Result<&str> {
        if let Text(value) = &self {
            Ok(value)
        } else {
            Err(Box::new(Error::default()))
        }
    }

    pub fn as_str_safe(&self) -> Option<&str> {
        if let Text(value) = &self {
            Some(value)
        } else {
            None
        }
    }

    pub fn as_number(&self) -> Result<&Number> {
        if let Numeric(value) = &self {
            Ok(value)
        } else {
            Err(Box::new(Error::default()))
        }
    }

    pub fn as_u64(&self) -> Result<u64> {
        if let Some(value) = self.as_number()?.as_u64() {
            Ok(value)
        } else {
            Err(Box::new(Error::default()))
        }
    }

    pub fn as_f64(&self) -> Result<f64> {
        if let Some(value) = self.as_number()?.as_f64() {
            Ok(value)
        } else {
            Err(Box::new(Error::default()))
        }
    }

    pub fn as_bool(&self) -> Result<bool> {
        if let Boolean(v) = &self {
            Ok(*v)
        } else {
            Err(Box::new(Error::default()))
        }
    }
}