Debian Codegraph @

src/spec.rs

raw
1 // The MIT License (MIT)
2
3 // Copyright (c) 2015 Y. T. Chung <[email protected]>
4
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 //! Constants derived from the [BSON Specification Version 1.1](http://bsonspec.org/spec.html).
23
24 use std::convert::From;
25
26 const ELEMENT_TYPE_FLOATING_POINT: u8 = 0x01;
27 const ELEMENT_TYPE_UTF8_STRING: u8 = 0x02;
28 const ELEMENT_TYPE_EMBEDDED_DOCUMENT: u8 = 0x03;
29 const ELEMENT_TYPE_ARRAY: u8 = 0x04;
30 const ELEMENT_TYPE_BINARY: u8 = 0x05;
31 const ELEMENT_TYPE_UNDEFINED: u8 = 0x06; // Deprecated
32 const ELEMENT_TYPE_OBJECT_ID: u8 = 0x07;
33 const ELEMENT_TYPE_BOOLEAN: u8 = 0x08;
34 const ELEMENT_TYPE_UTC_DATETIME: u8 = 0x09;
35 const ELEMENT_TYPE_NULL_VALUE: u8 = 0x0A;
36 const ELEMENT_TYPE_REGULAR_EXPRESSION: u8 = 0x0B;
37 const ELEMENT_TYPE_DBPOINTER: u8 = 0x0C; // Deprecated
38 const ELEMENT_TYPE_JAVASCRIPT_CODE: u8 = 0x0D;
39 const ELEMENT_TYPE_SYMBOL: u8 = 0x0E; // Deprecated
40 const ELEMENT_TYPE_JAVASCRIPT_CODE_WITH_SCOPE: u8 = 0x0F;
41 const ELEMENT_TYPE_32BIT_INTEGER: u8 = 0x10;
42 const ELEMENT_TYPE_TIMESTAMP: u8 = 0x11;
43 const ELEMENT_TYPE_64BIT_INTEGER: u8 = 0x12;
44 #[allow(unused)]
45 const ELEMENT_TYPE_128BIT_DECIMAL: u8 = 0x13;
46 const ELEMENT_TYPE_MINKEY: u8 = 0xFF;
47 const ELEMENT_TYPE_MAXKEY: u8 = 0x7F;
48
49 const BINARY_SUBTYPE_GENERIC: u8 = 0x00;
50 const BINARY_SUBTYPE_FUNCTION: u8 = 0x01;
51 const BINARY_SUBTYPE_BINARY_OLD: u8 = 0x02;
52 const BINARY_SUBTYPE_UUID_OLD: u8 = 0x03;
53 const BINARY_SUBTYPE_UUID: u8 = 0x04;
54 const BINARY_SUBTYPE_MD5: u8 = 0x05;
55 const BINARY_SUBTYPE_ENCRYPTED: u8 = 0x06;
56 const BINARY_SUBTYPE_COLUMN: u8 = 0x07;
57 const BINARY_SUBTYPE_SENSITIVE: u8 = 0x08;
58 const BINARY_SUBTYPE_USER_DEFINED: u8 = 0x80;
59
60 /// All available BSON element types.
61 ///
62 /// Not all element types are representable by the [`Bson`](crate::Bson) type.
63 #[repr(u8)]
64 #[derive(Debug, Eq, PartialEq, Clone, Copy)]
65 pub enum ElementType {
66 /// 64-bit binary floating point
67 Double = ELEMENT_TYPE_FLOATING_POINT,
68 /// UTF-8 string
69 String = ELEMENT_TYPE_UTF8_STRING,
70 /// Embedded document
71 EmbeddedDocument = ELEMENT_TYPE_EMBEDDED_DOCUMENT,
72 /// Array
73 Array = ELEMENT_TYPE_ARRAY,
74 /// Binary data
75 Binary = ELEMENT_TYPE_BINARY,
76 /// Deprecated. Undefined (value)
77 Undefined = ELEMENT_TYPE_UNDEFINED,
78 /// [ObjectId](http://dochub.mongodb.org/core/objectids)
79 ObjectId = ELEMENT_TYPE_OBJECT_ID,
80 /// Bool value
81 Boolean = ELEMENT_TYPE_BOOLEAN,
82 /// UTC datetime
83 DateTime = ELEMENT_TYPE_UTC_DATETIME,
84 /// Null value
85 Null = ELEMENT_TYPE_NULL_VALUE,
86 /// Regular expression - The first cstring is the regex pattern, the second is the regex
87 /// options string. Options are identified by characters, which must be stored in
88 /// alphabetical order. Valid options are 'i' for case insensitive matching, 'm' for
89 /// multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent,
90 /// 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match
91 /// unicode.
92 RegularExpression = ELEMENT_TYPE_REGULAR_EXPRESSION,
93 /// Deprecated.
94 DbPointer = ELEMENT_TYPE_DBPOINTER,
95 /// JavaScript code
96 JavaScriptCode = ELEMENT_TYPE_JAVASCRIPT_CODE,
97 /// Deprecated.
98 Symbol = ELEMENT_TYPE_SYMBOL,
99 /// JavaScript code w/ scope
100 JavaScriptCodeWithScope = ELEMENT_TYPE_JAVASCRIPT_CODE_WITH_SCOPE,
101 /// 32-bit integer
102 Int32 = ELEMENT_TYPE_32BIT_INTEGER,
103 /// Timestamp
104 Timestamp = ELEMENT_TYPE_TIMESTAMP,
105 /// 64-bit integer
106 Int64 = ELEMENT_TYPE_64BIT_INTEGER,
107 /// [128-bit decimal floating point](https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst)
108 Decimal128 = ELEMENT_TYPE_128BIT_DECIMAL,
109 MaxKey = ELEMENT_TYPE_MAXKEY,
110 MinKey = ELEMENT_TYPE_MINKEY,
111 }
112
113 impl ElementType {
114 /// Attempt to convert from a `u8`.
115 #[inline]
116 pub fn from(tag: u8) -> Option<ElementType> {
117 use self::ElementType::*;
118 Some(match tag {
119 ELEMENT_TYPE_FLOATING_POINT => Self::Double,
120 ELEMENT_TYPE_UTF8_STRING => Self::String,
121 ELEMENT_TYPE_EMBEDDED_DOCUMENT => EmbeddedDocument,
122 ELEMENT_TYPE_ARRAY => Array,
123 ELEMENT_TYPE_BINARY => Binary,
124 ELEMENT_TYPE_UNDEFINED => Undefined,
125 ELEMENT_TYPE_OBJECT_ID => ObjectId,
126 ELEMENT_TYPE_BOOLEAN => Boolean,
127 ELEMENT_TYPE_UTC_DATETIME => Self::DateTime,
128 ELEMENT_TYPE_NULL_VALUE => Self::Null,
129 ELEMENT_TYPE_REGULAR_EXPRESSION => RegularExpression,
130 ELEMENT_TYPE_DBPOINTER => DbPointer,
131 ELEMENT_TYPE_JAVASCRIPT_CODE => JavaScriptCode,
132 ELEMENT_TYPE_SYMBOL => Symbol,
133 ELEMENT_TYPE_JAVASCRIPT_CODE_WITH_SCOPE => JavaScriptCodeWithScope,
134 ELEMENT_TYPE_32BIT_INTEGER => Int32,
135 ELEMENT_TYPE_TIMESTAMP => Timestamp,
136 ELEMENT_TYPE_64BIT_INTEGER => Int64,
137 ELEMENT_TYPE_128BIT_DECIMAL => Decimal128,
138 ELEMENT_TYPE_MAXKEY => MaxKey,
139 ELEMENT_TYPE_MINKEY => MinKey,
140 _ => return None,
141 })
142 }
143 }
144
145 /// The available binary subtypes, plus a user-defined slot.
146 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
147 #[non_exhaustive]
148 pub enum BinarySubtype {
149 Generic,
150 Function,
151 BinaryOld,
152 UuidOld,
153 Uuid,
154 Md5,
155 Encrypted,
156 Column,
157 Sensitive,
158 UserDefined(u8),
159 Reserved(u8),
160 }
161
162 impl From<BinarySubtype> for u8 {
163 #[inline]
164 fn from(t: BinarySubtype) -> u8 {
165 match t {
166 BinarySubtype::Generic => BINARY_SUBTYPE_GENERIC,
167 BinarySubtype::Function => BINARY_SUBTYPE_FUNCTION,
168 BinarySubtype::BinaryOld => BINARY_SUBTYPE_BINARY_OLD,
169 BinarySubtype::UuidOld => BINARY_SUBTYPE_UUID_OLD,
170 BinarySubtype::Uuid => BINARY_SUBTYPE_UUID,
171 BinarySubtype::Md5 => BINARY_SUBTYPE_MD5,
172 BinarySubtype::Encrypted => BINARY_SUBTYPE_ENCRYPTED,
173 BinarySubtype::Column => BINARY_SUBTYPE_COLUMN,
174 BinarySubtype::Sensitive => BINARY_SUBTYPE_SENSITIVE,
175 BinarySubtype::UserDefined(x) => x,
176 BinarySubtype::Reserved(x) => x,
177 }
178 }
179 }
180
181 impl From<u8> for BinarySubtype {
182 #[inline]
183 fn from(t: u8) -> BinarySubtype {
184 match t {
185 BINARY_SUBTYPE_GENERIC => BinarySubtype::Generic,
186 BINARY_SUBTYPE_FUNCTION => BinarySubtype::Function,
187 BINARY_SUBTYPE_BINARY_OLD => BinarySubtype::BinaryOld,
188 BINARY_SUBTYPE_UUID_OLD => BinarySubtype::UuidOld,
189 BINARY_SUBTYPE_UUID => BinarySubtype::Uuid,
190 BINARY_SUBTYPE_MD5 => BinarySubtype::Md5,
191 BINARY_SUBTYPE_ENCRYPTED => BinarySubtype::Encrypted,
192 BINARY_SUBTYPE_COLUMN => BinarySubtype::Column,
193 BINARY_SUBTYPE_SENSITIVE => BinarySubtype::Sensitive,
194 _ if t < BINARY_SUBTYPE_USER_DEFINED => BinarySubtype::Reserved(t),
195 _ => BinarySubtype::UserDefined(t),
196 }
197 }
198 }