|
1 |
| -use bson::Bson; |
| 1 | +use chrono::{DateTime, UTC}; |
| 2 | +use bson::{Array,Bson,Document}; |
| 3 | +use super::oid::ObjectId; |
2 | 4 | use std::collections::BTreeMap;
|
3 | 5 | use std::fmt::{Display, Error, Formatter};
|
4 | 6 | use std::iter::{FromIterator, Map};
|
5 | 7 | use std::vec::IntoIter;
|
6 | 8 | use std::slice;
|
7 | 9 |
|
| 10 | +/// Error to indicate that either a value was empty or it contained an unexpected |
| 11 | +/// type, for use with the direct getters. |
| 12 | +#[derive(Debug,PartialEq)] |
| 13 | +pub enum ValueAccessError { |
| 14 | + NotPresent, |
| 15 | + UnexpectedType |
| 16 | +} |
| 17 | + |
| 18 | +pub type ValueAccessResult<T> = Result<T, ValueAccessError>; |
| 19 | + |
8 | 20 | /// A BSON document represented as an associative BTree Map with insertion ordering.
|
9 |
| -#[derive(Debug, Clone)] |
| 21 | +#[derive(Debug,Clone,PartialEq)] |
10 | 22 | pub struct OrderedDocument {
|
11 | 23 | pub keys: Vec<String>,
|
12 | 24 | document: BTreeMap<String, Bson>,
|
@@ -147,11 +159,109 @@ impl OrderedDocument {
|
147 | 159 | self.document.get(key)
|
148 | 160 | }
|
149 | 161 |
|
150 |
| - /// Gets a mutable reference to the value in the entry. |
| 162 | + /// Gets a mutable reference to the Bson corresponding to the key |
151 | 163 | pub fn get_mut(&mut self, key: &str) -> Option<&mut Bson> {
|
152 | 164 | self.document.get_mut(key)
|
153 | 165 | }
|
154 | 166 |
|
| 167 | + /// Get a floating point value for this key if it exists and has |
| 168 | + /// the correct type. |
| 169 | + pub fn get_f64(&self, key: &str) -> ValueAccessResult<f64> { |
| 170 | + match self.get(key) { |
| 171 | + Some(&Bson::FloatingPoint(v)) => Ok(v), |
| 172 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 173 | + None => Err(ValueAccessError::NotPresent) |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /// Get a string slice this key if it exists and has the correct type. |
| 178 | + pub fn get_str(&self, key: &str) -> ValueAccessResult<&str> { |
| 179 | + match self.get(key) { |
| 180 | + Some(&Bson::String(ref v)) => Ok(v), |
| 181 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 182 | + None => Err(ValueAccessError::NotPresent) |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /// Get a reference to an array for this key if it exists and has |
| 187 | + /// the correct type. |
| 188 | + pub fn get_array(&self, key: &str) -> ValueAccessResult<&Array> { |
| 189 | + match self.get(key) { |
| 190 | + Some(&Bson::Array(ref v)) => Ok(v), |
| 191 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 192 | + None => Err(ValueAccessError::NotPresent) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /// Get a reference to a document for this key if it exists and has |
| 197 | + /// the correct type. |
| 198 | + pub fn get_document(&self, key: &str) -> ValueAccessResult<&Document> { |
| 199 | + match self.get(key) { |
| 200 | + Some(&Bson::Document(ref v)) => Ok(v), |
| 201 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 202 | + None => Err(ValueAccessError::NotPresent) |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + /// Get a bool value for this key if it exists and has the correct type. |
| 207 | + pub fn get_bool(&self, key: &str) -> ValueAccessResult<bool> { |
| 208 | + match self.get(key) { |
| 209 | + Some(&Bson::Boolean(v)) => Ok(v), |
| 210 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 211 | + None => Err(ValueAccessError::NotPresent) |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /// Returns wether this key has a null value |
| 216 | + pub fn is_null(&self, key: &str) -> bool { |
| 217 | + self.get(key) == Some(&Bson::Null) |
| 218 | + } |
| 219 | + |
| 220 | + /// Get an i32 value for this key if it exists and has the correct type. |
| 221 | + pub fn get_i32(&self, key: &str) -> ValueAccessResult<i32> { |
| 222 | + match self.get(key) { |
| 223 | + Some(&Bson::I32(v)) => Ok(v), |
| 224 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 225 | + None => Err(ValueAccessError::NotPresent) |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + /// Get an i64 value for this key if it exists and has the correct type. |
| 230 | + pub fn get_i64(&self, key: &str) -> ValueAccessResult<i64> { |
| 231 | + match self.get(key) { |
| 232 | + Some(&Bson::I64(v)) => Ok(v), |
| 233 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 234 | + None => Err(ValueAccessError::NotPresent) |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + /// Get a time stamp value for this key if it exists and has the correct type. |
| 239 | + pub fn get_time_stamp(&self, key: &str) -> ValueAccessResult<i64> { |
| 240 | + match self.get(key) { |
| 241 | + Some(&Bson::TimeStamp(v)) => Ok(v), |
| 242 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 243 | + None => Err(ValueAccessError::NotPresent) |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + /// Get an object id value for this key if it exists and has the correct type. |
| 248 | + pub fn get_object_id(&self, key: &str) -> ValueAccessResult<&ObjectId> { |
| 249 | + match self.get(key) { |
| 250 | + Some(&Bson::ObjectId(ref v)) => Ok(v), |
| 251 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 252 | + None => Err(ValueAccessError::NotPresent) |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + /// Get a UTC datetime value for this key if it exists and has the correct type. |
| 257 | + pub fn get_utc_datetime(&self, key: &str) -> ValueAccessResult<&DateTime<UTC>> { |
| 258 | + match self.get(key) { |
| 259 | + Some(&Bson::UtcDatetime(ref v)) => Ok(v), |
| 260 | + Some(_) => Err(ValueAccessError::UnexpectedType), |
| 261 | + None => Err(ValueAccessError::NotPresent) |
| 262 | + } |
| 263 | + } |
| 264 | + |
155 | 265 | /// Returns true if the map contains a value for the specified key.
|
156 | 266 | pub fn contains_key(&self, key: &str) -> bool {
|
157 | 267 | self.document.contains_key(key)
|
|
0 commit comments