|
1 | | -#[cfg(feature = "bson-3")] |
2 | | -pub(crate) trait RawDocumentBufExt { |
3 | | - fn append_ref<'a>( |
| 1 | +use crate::bson::RawBson; |
| 2 | + |
| 3 | +pub(crate) trait RawDocumentBufExt: Sized { |
| 4 | + fn append_err(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) -> RawResult<()>; |
| 5 | + |
| 6 | + fn append_ref_err<'a>( |
4 | 7 | &mut self, |
5 | 8 | key: impl AsRef<str>, |
6 | 9 | value: impl Into<crate::bson::raw::RawBsonRef<'a>>, |
7 | | - ); |
| 10 | + ) -> RawResult<()>; |
| 11 | + |
| 12 | + #[cfg(not(feature = "bson-3"))] |
| 13 | + fn decode_from_bytes(data: Vec<u8>) -> RawResult<Self>; |
8 | 14 | } |
9 | 15 |
|
10 | 16 | #[cfg(feature = "bson-3")] |
11 | 17 | impl RawDocumentBufExt for crate::bson::RawDocumentBuf { |
12 | | - fn append_ref<'a>( |
| 18 | + fn append_err(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) -> RawResult<()> { |
| 19 | + self.append(key, value.into()) |
| 20 | + } |
| 21 | + |
| 22 | + fn append_ref_err<'a>( |
13 | 23 | &mut self, |
14 | 24 | key: impl AsRef<str>, |
15 | 25 | value: impl Into<crate::bson::raw::RawBsonRef<'a>>, |
16 | | - ) { |
| 26 | + ) -> RawResult<()> { |
17 | 27 | self.append(key, value) |
18 | 28 | } |
19 | 29 | } |
20 | 30 |
|
| 31 | +#[cfg(not(feature = "bson-3"))] |
| 32 | +impl RawDocumentBufExt for crate::bson::RawDocumentBuf { |
| 33 | + fn append_err(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) -> RawResult<()> { |
| 34 | + self.append(key, value); |
| 35 | + Ok(()) |
| 36 | + } |
| 37 | + |
| 38 | + fn append_ref_err<'a>( |
| 39 | + &mut self, |
| 40 | + key: impl AsRef<str>, |
| 41 | + value: impl Into<crate::bson::raw::RawBsonRef<'a>>, |
| 42 | + ) -> RawResult<()> { |
| 43 | + self.append_ref(key, value); |
| 44 | + Ok(()) |
| 45 | + } |
| 46 | + |
| 47 | + fn decode_from_bytes(data: Vec<u8>) -> RawResult<Self> { |
| 48 | + Self::from_bytes(data) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pub(crate) trait RawArrayBufExt: Sized { |
| 53 | + #[allow(dead_code)] |
| 54 | + fn from_iter_err<V: Into<RawBson>, I: IntoIterator<Item = V>>(iter: I) -> RawResult<Self>; |
| 55 | + |
| 56 | + fn push_err(&mut self, value: impl Into<RawBson>) -> RawResult<()>; |
| 57 | +} |
| 58 | + |
21 | 59 | #[cfg(feature = "bson-3")] |
22 | | -pub(crate) use crate::bson::error::Result as RawResult; |
| 60 | +impl RawArrayBufExt for crate::bson::RawArrayBuf { |
| 61 | + fn from_iter_err<V: Into<RawBson>, I: IntoIterator<Item = V>>(iter: I) -> RawResult<Self> { |
| 62 | + Self::from_iter(iter.into_iter().map(|v| v.into())) |
| 63 | + } |
| 64 | + |
| 65 | + fn push_err(&mut self, value: impl Into<RawBson>) -> RawResult<()> { |
| 66 | + self.push(value.into()) |
| 67 | + } |
| 68 | +} |
23 | 69 |
|
24 | 70 | #[cfg(not(feature = "bson-3"))] |
25 | | -pub(crate) use crate::bson::raw::Result as RawResult; |
| 71 | +impl RawArrayBufExt for crate::bson::RawArrayBuf { |
| 72 | + fn from_iter_err<V: Into<RawBson>, I: IntoIterator<Item = V>>(iter: I) -> RawResult<Self> { |
| 73 | + Ok(Self::from_iter(iter)) |
| 74 | + } |
26 | 75 |
|
27 | | -#[cfg(feature = "bson-3")] |
28 | | -pub(crate) use crate::bson::error::Error as RawError; |
| 76 | + fn push_err(&mut self, value: impl Into<RawBson>) -> RawResult<()> { |
| 77 | + self.push(value); |
| 78 | + Ok(()) |
| 79 | + } |
| 80 | +} |
29 | 81 |
|
30 | 82 | #[cfg(not(feature = "bson-3"))] |
31 | | -pub(crate) use crate::bson::raw::Error as RawError; |
| 83 | +pub(crate) trait RawDocumentExt { |
| 84 | + fn decode_from_bytes<D: AsRef<[u8]> + ?Sized>(data: &D) -> RawResult<&Self>; |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(not(feature = "bson-3"))] |
| 88 | +impl RawDocumentExt for crate::bson::RawDocument { |
| 89 | + fn decode_from_bytes<D: AsRef<[u8]> + ?Sized>(data: &D) -> RawResult<&Self> { |
| 90 | + Self::from_bytes(data) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(not(feature = "bson-3"))] |
| 95 | +#[allow(dead_code)] |
| 96 | +pub(crate) trait DocumentExt { |
| 97 | + fn encode_to_vec(&self) -> crate::bson::ser::Result<Vec<u8>>; |
| 98 | +} |
| 99 | + |
| 100 | +#[cfg(not(feature = "bson-3"))] |
| 101 | +impl DocumentExt for crate::bson::Document { |
| 102 | + fn encode_to_vec(&self) -> crate::bson::ser::Result<Vec<u8>> { |
| 103 | + let mut out = vec![]; |
| 104 | + self.to_writer(&mut out)?; |
| 105 | + Ok(out) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +macro_rules! use_either { |
| 110 | + ($($name:ident => $path3:path | $path2:path);+;) => { |
| 111 | + $( |
| 112 | + #[cfg(feature = "bson-3")] |
| 113 | + pub(crate) use crate::bson::{$path3 as $name}; |
| 114 | + |
| 115 | + #[cfg(not(feature = "bson-3"))] |
| 116 | + #[allow(unused_imports)] |
| 117 | + pub(crate) use crate::bson::{$path2 as $name}; |
| 118 | + )+ |
| 119 | + }; |
| 120 | +} |
| 121 | + |
| 122 | +// Exported name => bson3 import | bson2 import |
| 123 | +use_either! { |
| 124 | + RawResult => error::Result | raw::Result; |
| 125 | + RawError => error::Error | raw::Error; |
| 126 | + serialize_to_raw_document_buf => serialize_to_raw_document_buf | to_raw_document_buf; |
| 127 | + serialize_to_document => serialize_to_document | to_document; |
| 128 | + serialize_to_bson => serialize_to_bson | to_bson; |
| 129 | + deserialize_from_slice => deserialize_from_slice | from_slice; |
| 130 | + deserialize_from_document => deserialize_from_document | from_document; |
| 131 | + deserialize_from_bson => deserialize_from_bson | from_bson; |
| 132 | +} |
0 commit comments