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
//! BDF objects

use crate::{Error, Map, Result};

mod de;
mod ser;

pub use de::from_object;
pub use ser::{to_object, Serializer};

/// A BDF object
#[derive(Debug, PartialEq, Clone)]
pub enum Object {
    /// The null object
    Null,
    /// A boolean
    Boolean(bool),
    /// An integer
    Integer(i64),
    /// A floating-point number
    Float(f64),
    /// A string
    String(std::string::String),
    /// A binary blob
    Raw(Vec<u8>),
    /// A list of BDF objects
    List(Vec<Object>),
    /// A map of strings to BDF objects
    Map(Map),
}

impl Object {
    /// Returns the value of this null object, if it is one.
    pub fn as_null(&self) -> Result<()> {
        match self {
            Object::Null => Ok(()),
            _ => Err(Error::WrongType),
        }
    }

    /// Returns the value of this boolean object, if it is one.
    pub fn as_boolean(&self) -> Result<bool> {
        match self {
            Object::Boolean(b) => Ok(*b),
            _ => Err(Error::WrongType),
        }
    }

    /// Returns the value of this integer object, if it is one.
    pub fn as_integer(&self) -> Result<i64> {
        match self {
            Object::Integer(i) => Ok(*i),
            _ => Err(Error::WrongType),
        }
    }

    /// Returns the value of this float object, if it is one.
    pub fn as_float(&self) -> Result<f64> {
        match self {
            Object::Float(f) => Ok(*f),
            _ => Err(Error::WrongType),
        }
    }

    /// Returns the value of this string object, if it is one.
    pub fn as_string(&self) -> Result<&str> {
        match self {
            Object::String(s) => Ok(s),
            _ => Err(Error::WrongType),
        }
    }

    /// Returns the value of this raw object, if it is one.
    pub fn as_raw(&self) -> Result<&[u8]> {
        match self {
            Object::Raw(r) => Ok(r),
            _ => Err(Error::WrongType),
        }
    }

    /// Returns the value of this list object, if it is one.
    pub fn as_list(&self) -> Result<&[Object]> {
        match self {
            Object::List(l) => Ok(l),
            _ => Err(Error::WrongType),
        }
    }

    /// Returns the value of this map object, if it is one.
    pub fn as_map(&self) -> Result<&Map> {
        match self {
            Object::Map(d) => Ok(d),
            _ => Err(Error::WrongType),
        }
    }

    /// Consumes this null object and returns its value, if it is one.
    pub fn to_null(self) -> Result<()> {
        self.as_null()
    }

    /// Consumes this boolean object and returns its value, if it is one.
    pub fn to_boolean(self) -> Result<bool> {
        self.as_boolean()
    }

    /// Consumes this integer object and returns its value, if it is one.
    pub fn to_integer(self) -> Result<i64> {
        self.as_integer()
    }

    /// Consumes this float object and returns its value, if it is one.
    pub fn to_float(self) -> Result<f64> {
        self.as_float()
    }

    /// Consumes this string object and returns its value, if it is one.
    pub fn to_string(self) -> Result<String> {
        match self {
            Object::String(s) => Ok(s),
            _ => Err(Error::WrongType),
        }
    }

    /// Consumes this raw object and returns its value, if it is one.
    pub fn to_raw(self) -> Result<Vec<u8>> {
        match self {
            Object::Raw(r) => Ok(r),
            _ => Err(Error::WrongType),
        }
    }

    /// Consumes this list object and returns its value, if it is one.
    pub fn to_list(self) -> Result<Vec<Object>> {
        match self {
            Object::List(l) => Ok(l),
            _ => Err(Error::WrongType),
        }
    }

    /// Consumes this map object and returns its value, if it is one.
    pub fn to_map(self) -> Result<Map> {
        match self {
            Object::Map(d) => Ok(d),
            _ => Err(Error::WrongType),
        }
    }
}