bramble_data/object/
mod.rs

1//! BDF objects
2
3use crate::{Error, Map, Result};
4
5mod de;
6mod ser;
7
8pub use de::from_object;
9pub use ser::{to_object, Serializer};
10
11/// A BDF object
12#[derive(Debug, PartialEq, Clone)]
13pub enum Object {
14    /// The null object
15    Null,
16    /// A boolean
17    Boolean(bool),
18    /// An integer
19    Integer(i64),
20    /// A floating-point number
21    Float(f64),
22    /// A string
23    String(std::string::String),
24    /// A binary blob
25    Raw(Vec<u8>),
26    /// A list of BDF objects
27    List(Vec<Object>),
28    /// A map of strings to BDF objects
29    Map(Map),
30}
31
32impl Object {
33    /// Returns the value of this null object, if it is one.
34    pub fn as_null(&self) -> Result<()> {
35        match self {
36            Object::Null => Ok(()),
37            _ => Err(Error::WrongType),
38        }
39    }
40
41    /// Returns the value of this boolean object, if it is one.
42    pub fn as_boolean(&self) -> Result<bool> {
43        match self {
44            Object::Boolean(b) => Ok(*b),
45            _ => Err(Error::WrongType),
46        }
47    }
48
49    /// Returns the value of this integer object, if it is one.
50    pub fn as_integer(&self) -> Result<i64> {
51        match self {
52            Object::Integer(i) => Ok(*i),
53            _ => Err(Error::WrongType),
54        }
55    }
56
57    /// Returns the value of this float object, if it is one.
58    pub fn as_float(&self) -> Result<f64> {
59        match self {
60            Object::Float(f) => Ok(*f),
61            _ => Err(Error::WrongType),
62        }
63    }
64
65    /// Returns the value of this string object, if it is one.
66    pub fn as_string(&self) -> Result<&str> {
67        match self {
68            Object::String(s) => Ok(s),
69            _ => Err(Error::WrongType),
70        }
71    }
72
73    /// Returns the value of this raw object, if it is one.
74    pub fn as_raw(&self) -> Result<&[u8]> {
75        match self {
76            Object::Raw(r) => Ok(r),
77            _ => Err(Error::WrongType),
78        }
79    }
80
81    /// Returns the value of this list object, if it is one.
82    pub fn as_list(&self) -> Result<&[Object]> {
83        match self {
84            Object::List(l) => Ok(l),
85            _ => Err(Error::WrongType),
86        }
87    }
88
89    /// Returns the value of this map object, if it is one.
90    pub fn as_map(&self) -> Result<&Map> {
91        match self {
92            Object::Map(d) => Ok(d),
93            _ => Err(Error::WrongType),
94        }
95    }
96
97    /// Consumes this null object and returns its value, if it is one.
98    pub fn to_null(self) -> Result<()> {
99        self.as_null()
100    }
101
102    /// Consumes this boolean object and returns its value, if it is one.
103    pub fn to_boolean(self) -> Result<bool> {
104        self.as_boolean()
105    }
106
107    /// Consumes this integer object and returns its value, if it is one.
108    pub fn to_integer(self) -> Result<i64> {
109        self.as_integer()
110    }
111
112    /// Consumes this float object and returns its value, if it is one.
113    pub fn to_float(self) -> Result<f64> {
114        self.as_float()
115    }
116
117    /// Consumes this string object and returns its value, if it is one.
118    pub fn to_string(self) -> Result<String> {
119        match self {
120            Object::String(s) => Ok(s),
121            _ => Err(Error::WrongType),
122        }
123    }
124
125    /// Consumes this raw object and returns its value, if it is one.
126    pub fn to_raw(self) -> Result<Vec<u8>> {
127        match self {
128            Object::Raw(r) => Ok(r),
129            _ => Err(Error::WrongType),
130        }
131    }
132
133    /// Consumes this list object and returns its value, if it is one.
134    pub fn to_list(self) -> Result<Vec<Object>> {
135        match self {
136            Object::List(l) => Ok(l),
137            _ => Err(Error::WrongType),
138        }
139    }
140
141    /// Consumes this map object and returns its value, if it is one.
142    pub fn to_map(self) -> Result<Map> {
143        match self {
144            Object::Map(d) => Ok(d),
145            _ => Err(Error::WrongType),
146        }
147    }
148}