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
+
1
15
/// A BSON array referencing raw bytes stored elsewhere.
2
16
pub struct RawArray {
3
17
doc : RawDocumentRef ,
4
18
}
5
19
6
20
impl RawArray {
7
- fn new ( data : & [ u8 ] ) -> RawResult < & RawArray > {
21
+ pub ( super ) fn new ( data : & [ u8 ] ) -> Result < & RawArray > {
8
22
Ok ( RawArray :: from_doc ( RawDocumentRef :: new ( data) ?) )
9
23
}
10
24
@@ -22,88 +36,88 @@ impl RawArray {
22
36
}
23
37
24
38
/// 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 < ' _ > > > {
26
40
self . into_iter ( ) . nth ( index) . transpose ( )
27
41
}
28
42
29
43
fn get_with < ' a , T > (
30
44
& ' a self ,
31
45
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 > > {
34
48
self . get ( index) ?. map ( f) . transpose ( )
35
49
}
36
50
37
51
/// Gets the BSON double at the given index or returns an error if the value at that index isn't
38
52
/// 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)
41
55
}
42
56
43
57
/// Gets a reference to the string at the given index or returns an error if the
44
58
/// 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)
47
61
}
48
62
49
63
/// Gets a reference to the document at the given index or returns an error if the
50
64
/// 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)
53
67
}
54
68
55
69
/// Gets a reference to the array at the given index or returns an error if the
56
70
/// 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)
59
73
}
60
74
61
75
/// Gets a reference to the BSON binary value at the given index or returns an error if the
62
76
/// 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)
65
79
}
66
80
67
81
/// Gets the ObjectId at the given index or returns an error if the value at that index isn't an
68
82
/// 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)
71
85
}
72
86
73
87
/// Gets the boolean at the given index or returns an error if the value at that index isn't a
74
88
/// 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)
77
91
}
78
92
79
93
/// Gets the DateTime at the given index or returns an error if the value at that index isn't a
80
94
/// 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 ) )
83
97
}
84
98
85
99
/// Gets a reference to the BSON regex at the given index or returns an error if the
86
100
/// 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)
89
103
}
90
104
91
105
/// Gets a reference to the BSON timestamp at the given index or returns an error if the
92
106
/// 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)
95
109
}
96
110
97
111
/// Gets the BSON int32 at the given index or returns an error if the value at that index isn't
98
112
/// 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)
101
115
}
102
116
103
117
/// Gets BSON int64 at the given index or returns an error if the value at that index isn't a
104
118
/// 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)
107
121
}
108
122
109
123
/// Gets a reference to the raw bytes of the RawArray.
@@ -115,7 +129,7 @@ impl RawArray {
115
129
impl TryFrom < & RawArray > for Vec < Bson > {
116
130
type Error = Error ;
117
131
118
- fn try_from ( arr : & RawArray ) -> RawResult < Vec < Bson > > {
132
+ fn try_from ( arr : & RawArray ) -> Result < Vec < Bson > > {
119
133
arr. into_iter ( )
120
134
. map ( |result| {
121
135
let rawbson = result?;
@@ -127,7 +141,7 @@ impl TryFrom<&RawArray> for Vec<Bson> {
127
141
128
142
impl < ' a > IntoIterator for & ' a RawArray {
129
143
type IntoIter = RawArrayIter < ' a > ;
130
- type Item = RawResult < elem :: RawBson < ' a > > ;
144
+ type Item = Result < RawBson < ' a > > ;
131
145
132
146
fn into_iter ( self ) -> RawArrayIter < ' a > {
133
147
RawArrayIter {
@@ -142,9 +156,9 @@ pub struct RawArrayIter<'a> {
142
156
}
143
157
144
158
impl < ' a > Iterator for RawArrayIter < ' a > {
145
- type Item = RawResult < elem :: RawBson < ' a > > ;
159
+ type Item = Result < RawBson < ' a > > ;
146
160
147
- fn next ( & mut self ) -> Option < RawResult < RawBson < ' a > > > {
161
+ fn next ( & mut self ) -> Option < Result < RawBson < ' a > > > {
148
162
match self . inner . next ( ) {
149
163
Some ( Ok ( ( _, v) ) ) => Some ( Ok ( v) ) ,
150
164
Some ( Err ( e) ) => Some ( Err ( e) ) ,
0 commit comments