Skip to content

Commit d6f1b7b

Browse files
committed
refactor top-level module into submodules
1 parent 0195fd9 commit d6f1b7b

File tree

5 files changed

+149
-123
lines changed

5 files changed

+149
-123
lines changed

src/raw/array.rs

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
use std::convert::TryFrom;
2+
3+
use super::{
4+
Error,
5+
RawBinary,
6+
RawBson,
7+
RawDocumentIter,
8+
RawDocumentRef,
9+
RawRegex,
10+
RawTimestamp,
11+
Result,
12+
};
13+
use crate::{oid::ObjectId, Bson, DateTime};
14+
115
/// A BSON array referencing raw bytes stored elsewhere.
216
pub struct RawArray {
317
doc: RawDocumentRef,
418
}
519

620
impl RawArray {
7-
fn new(data: &[u8]) -> RawResult<&RawArray> {
21+
pub(super) fn new(data: &[u8]) -> Result<&RawArray> {
822
Ok(RawArray::from_doc(RawDocumentRef::new(data)?))
923
}
1024

@@ -22,88 +36,88 @@ impl RawArray {
2236
}
2337

2438
/// Gets a reference to the value at the given index.
25-
pub fn get(&self, index: usize) -> RawResult<Option<RawBson<'_>>> {
39+
pub fn get(&self, index: usize) -> Result<Option<RawBson<'_>>> {
2640
self.into_iter().nth(index).transpose()
2741
}
2842

2943
fn get_with<'a, T>(
3044
&'a self,
3145
index: usize,
32-
f: impl FnOnce(elem::RawBson<'a>) -> RawResult<T>,
33-
) -> RawResult<Option<T>> {
46+
f: impl FnOnce(RawBson<'a>) -> Result<T>,
47+
) -> Result<Option<T>> {
3448
self.get(index)?.map(f).transpose()
3549
}
3650

3751
/// Gets the BSON double at the given index or returns an error if the value at that index isn't
3852
/// a double.
39-
pub fn get_f64(&self, index: usize) -> RawResult<Option<f64>> {
40-
self.get_with(index, elem::RawBson::as_f64)
53+
pub fn get_f64(&self, index: usize) -> Result<Option<f64>> {
54+
self.get_with(index, RawBson::as_f64)
4155
}
4256

4357
/// Gets a reference to the string at the given index or returns an error if the
4458
/// value at that index isn't a string.
45-
pub fn get_str(&self, index: usize) -> RawResult<Option<&str>> {
46-
self.get_with(index, elem::RawBson::as_str)
59+
pub fn get_str(&self, index: usize) -> Result<Option<&str>> {
60+
self.get_with(index, RawBson::as_str)
4761
}
4862

4963
/// Gets a reference to the document at the given index or returns an error if the
5064
/// value at that index isn't a document.
51-
pub fn get_document(&self, index: usize) -> RawResult<Option<&RawDocumentRef>> {
52-
self.get_with(index, elem::RawBson::as_document)
65+
pub fn get_document(&self, index: usize) -> Result<Option<&RawDocumentRef>> {
66+
self.get_with(index, RawBson::as_document)
5367
}
5468

5569
/// Gets a reference to the array at the given index or returns an error if the
5670
/// value at that index isn't a array.
57-
pub fn get_array(&self, index: usize) -> RawResult<Option<&RawArray>> {
58-
self.get_with(index, elem::RawBson::as_array)
71+
pub fn get_array(&self, index: usize) -> Result<Option<&RawArray>> {
72+
self.get_with(index, RawBson::as_array)
5973
}
6074

6175
/// Gets a reference to the BSON binary value at the given index or returns an error if the
6276
/// value at that index isn't a binary.
63-
pub fn get_binary(&self, index: usize) -> RawResult<Option<RawBinary<'_>>> {
64-
self.get_with(index, elem::RawBson::as_binary)
77+
pub fn get_binary(&self, index: usize) -> Result<Option<RawBinary<'_>>> {
78+
self.get_with(index, RawBson::as_binary)
6579
}
6680

6781
/// Gets the ObjectId at the given index or returns an error if the value at that index isn't an
6882
/// ObjectId.
69-
pub fn get_object_id(&self, index: usize) -> RawResult<Option<ObjectId>> {
70-
self.get_with(index, elem::RawBson::as_object_id)
83+
pub fn get_object_id(&self, index: usize) -> Result<Option<ObjectId>> {
84+
self.get_with(index, RawBson::as_object_id)
7185
}
7286

7387
/// Gets the boolean at the given index or returns an error if the value at that index isn't a
7488
/// boolean.
75-
pub fn get_bool(&self, index: usize) -> RawResult<Option<bool>> {
76-
self.get_with(index, elem::RawBson::as_bool)
89+
pub fn get_bool(&self, index: usize) -> Result<Option<bool>> {
90+
self.get_with(index, RawBson::as_bool)
7791
}
7892

7993
/// Gets the DateTime at the given index or returns an error if the value at that index isn't a
8094
/// DateTime.
81-
pub fn get_datetime(&self, index: usize) -> RawResult<Option<DateTime<Utc>>> {
82-
self.get_with(index, elem::RawBson::as_datetime)
95+
pub fn get_datetime(&self, index: usize) -> Result<Option<DateTime>> {
96+
Ok(self.get_with(index, RawBson::as_datetime)?.map(Into::into))
8397
}
8498

8599
/// Gets a reference to the BSON regex at the given index or returns an error if the
86100
/// value at that index isn't a regex.
87-
pub fn get_regex(&self, index: usize) -> RawResult<Option<RawRegex<'_>>> {
88-
self.get_with(index, elem::RawBson::as_regex)
101+
pub fn get_regex(&self, index: usize) -> Result<Option<RawRegex<'_>>> {
102+
self.get_with(index, RawBson::as_regex)
89103
}
90104

91105
/// Gets a reference to the BSON timestamp at the given index or returns an error if the
92106
/// value at that index isn't a timestamp.
93-
pub fn get_timestamp(&self, index: usize) -> RawResult<Option<RawTimestamp<'_>>> {
94-
self.get_with(index, elem::RawBson::as_timestamp)
107+
pub fn get_timestamp(&self, index: usize) -> Result<Option<RawTimestamp<'_>>> {
108+
self.get_with(index, RawBson::as_timestamp)
95109
}
96110

97111
/// Gets the BSON int32 at the given index or returns an error if the value at that index isn't
98112
/// a 32-bit integer.
99-
pub fn get_i32(&self, index: usize) -> RawResult<Option<i32>> {
100-
self.get_with(index, elem::RawBson::as_i32)
113+
pub fn get_i32(&self, index: usize) -> Result<Option<i32>> {
114+
self.get_with(index, RawBson::as_i32)
101115
}
102116

103117
/// Gets BSON int64 at the given index or returns an error if the value at that index isn't a
104118
/// 64-bit integer.
105-
pub fn get_i64(&self, index: usize) -> RawResult<Option<i64>> {
106-
self.get_with(index, elem::RawBson::as_i64)
119+
pub fn get_i64(&self, index: usize) -> Result<Option<i64>> {
120+
self.get_with(index, RawBson::as_i64)
107121
}
108122

109123
/// Gets a reference to the raw bytes of the RawArray.
@@ -115,7 +129,7 @@ impl RawArray {
115129
impl TryFrom<&RawArray> for Vec<Bson> {
116130
type Error = Error;
117131

118-
fn try_from(arr: &RawArray) -> RawResult<Vec<Bson>> {
132+
fn try_from(arr: &RawArray) -> Result<Vec<Bson>> {
119133
arr.into_iter()
120134
.map(|result| {
121135
let rawbson = result?;
@@ -127,7 +141,7 @@ impl TryFrom<&RawArray> for Vec<Bson> {
127141

128142
impl<'a> IntoIterator for &'a RawArray {
129143
type IntoIter = RawArrayIter<'a>;
130-
type Item = RawResult<elem::RawBson<'a>>;
144+
type Item = Result<RawBson<'a>>;
131145

132146
fn into_iter(self) -> RawArrayIter<'a> {
133147
RawArrayIter {
@@ -142,9 +156,9 @@ pub struct RawArrayIter<'a> {
142156
}
143157

144158
impl<'a> Iterator for RawArrayIter<'a> {
145-
type Item = RawResult<elem::RawBson<'a>>;
159+
type Item = Result<RawBson<'a>>;
146160

147-
fn next(&mut self) -> Option<RawResult<RawBson<'a>>> {
161+
fn next(&mut self) -> Option<Result<RawBson<'a>>> {
148162
match self.inner.next() {
149163
Some(Ok((_, v))) => Some(Ok(v)),
150164
Some(Err(e)) => Some(Err(e)),

0 commit comments

Comments
 (0)