bramble_data/object/
mod.rs1use crate::{Error, Map, Result};
4
5mod de;
6mod ser;
7
8pub use de::from_object;
9pub use ser::{to_object, Serializer};
10
11#[derive(Debug, PartialEq, Clone)]
13pub enum Object {
14 Null,
16 Boolean(bool),
18 Integer(i64),
20 Float(f64),
22 String(std::string::String),
24 Raw(Vec<u8>),
26 List(Vec<Object>),
28 Map(Map),
30}
31
32impl Object {
33 pub fn as_null(&self) -> Result<()> {
35 match self {
36 Object::Null => Ok(()),
37 _ => Err(Error::WrongType),
38 }
39 }
40
41 pub fn as_boolean(&self) -> Result<bool> {
43 match self {
44 Object::Boolean(b) => Ok(*b),
45 _ => Err(Error::WrongType),
46 }
47 }
48
49 pub fn as_integer(&self) -> Result<i64> {
51 match self {
52 Object::Integer(i) => Ok(*i),
53 _ => Err(Error::WrongType),
54 }
55 }
56
57 pub fn as_float(&self) -> Result<f64> {
59 match self {
60 Object::Float(f) => Ok(*f),
61 _ => Err(Error::WrongType),
62 }
63 }
64
65 pub fn as_string(&self) -> Result<&str> {
67 match self {
68 Object::String(s) => Ok(s),
69 _ => Err(Error::WrongType),
70 }
71 }
72
73 pub fn as_raw(&self) -> Result<&[u8]> {
75 match self {
76 Object::Raw(r) => Ok(r),
77 _ => Err(Error::WrongType),
78 }
79 }
80
81 pub fn as_list(&self) -> Result<&[Object]> {
83 match self {
84 Object::List(l) => Ok(l),
85 _ => Err(Error::WrongType),
86 }
87 }
88
89 pub fn as_map(&self) -> Result<&Map> {
91 match self {
92 Object::Map(d) => Ok(d),
93 _ => Err(Error::WrongType),
94 }
95 }
96
97 pub fn to_null(self) -> Result<()> {
99 self.as_null()
100 }
101
102 pub fn to_boolean(self) -> Result<bool> {
104 self.as_boolean()
105 }
106
107 pub fn to_integer(self) -> Result<i64> {
109 self.as_integer()
110 }
111
112 pub fn to_float(self) -> Result<f64> {
114 self.as_float()
115 }
116
117 pub fn to_string(self) -> Result<String> {
119 match self {
120 Object::String(s) => Ok(s),
121 _ => Err(Error::WrongType),
122 }
123 }
124
125 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 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 pub fn to_map(self) -> Result<Map> {
143 match self {
144 Object::Map(d) => Ok(d),
145 _ => Err(Error::WrongType),
146 }
147 }
148}