1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// The MIT License (MIT)

// Copyright (c) 2015 Y. T. Chung <zonyitoo@gmail.com>

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

//! Constants derived from the [BSON Specification Version 1.1](http://bsonspec.org/spec.html).

use std::convert::From;

pub const ELEMENT_TYPE_FLOATING_POINT: u8 = 0x01;
pub const ELEMENT_TYPE_UTF8_STRING: u8 = 0x02;
pub const ELEMENT_TYPE_EMBEDDED_DOCUMENT: u8 = 0x03;
pub const ELEMENT_TYPE_ARRAY: u8 = 0x04;
pub const ELEMENT_TYPE_BINARY: u8 = 0x05;
pub const ELEMENT_TYPE_UNDEFINED: u8 = 0x06; // Deprecated
pub const ELEMENT_TYPE_OBJECT_ID: u8 = 0x07;
pub const ELEMENT_TYPE_BOOLEAN: u8 = 0x08;
pub const ELEMENT_TYPE_UTC_DATETIME: u8 = 0x09;
pub const ELEMENT_TYPE_NULL_VALUE: u8 = 0x0A;
pub const ELEMENT_TYPE_REGULAR_EXPRESSION: u8 = 0x0B;
pub const ELEMENT_TYPE_DBPOINTER: u8 = 0x0C; // Deprecated
pub const ELEMENT_TYPE_JAVASCRIPT_CODE: u8 = 0x0D;
pub const ELEMENT_TYPE_SYMBOL: u8 = 0x0E; // Deprecated
pub const ELEMENT_TYPE_JAVASCRIPT_CODE_WITH_SCOPE: u8 = 0x0F;
pub const ELEMENT_TYPE_32BIT_INTEGER: u8 = 0x10;
pub const ELEMENT_TYPE_TIMESTAMP: u8 = 0x11;
pub const ELEMENT_TYPE_64BIT_INTEGER: u8 = 0x12;
pub const ELEMENT_TYPE_128BIT_DECIMAL: u8 = 0x13;
pub const ELEMENT_TYPE_MINKEY: u8 = 0xFF;
pub const ELEMENT_TYPE_MAXKEY: u8 = 0x7F;

pub const BINARY_SUBTYPE_GENERIC: u8 = 0x00;
pub const BINARY_SUBTYPE_FUNCTION: u8 = 0x01;
pub const BINARY_SUBTYPE_BINARY_OLD: u8 = 0x02;
pub const BINARY_SUBTYPE_UUID_OLD: u8 = 0x03;
pub const BINARY_SUBTYPE_UUID: u8 = 0x04;
pub const BINARY_SUBTYPE_MD5: u8 = 0x05;

/// All available BSON element types.
///
/// Not all element types are representable by the `Bson` type.
#[repr(u8)]
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum ElementType {
    /// 64-bit binary floating point
    FloatingPoint = ELEMENT_TYPE_FLOATING_POINT,
    /// UTF-8 string
    Utf8String = ELEMENT_TYPE_UTF8_STRING,
    /// Embedded document
    EmbeddedDocument = ELEMENT_TYPE_EMBEDDED_DOCUMENT,
    /// Array
    Array = ELEMENT_TYPE_ARRAY,
    /// Binary data
    Binary = ELEMENT_TYPE_BINARY,
    /// Deprecated. Undefined (value)
    Undefined = ELEMENT_TYPE_UNDEFINED,
    /// [ObjectId](http://dochub.mongodb.org/core/objectids)
    ObjectId = ELEMENT_TYPE_OBJECT_ID,
    /// Boolean value
    Boolean = ELEMENT_TYPE_BOOLEAN,
    /// UTC datetime
    UtcDatetime = ELEMENT_TYPE_UTC_DATETIME,
    /// Null value
    NullValue = ELEMENT_TYPE_NULL_VALUE,
    /// Regular expression - The first cstring is the regex pattern, the second is the regex options string.
    /// Options are identified by characters, which must be stored in alphabetical order.
    /// Valid options are 'i' for case insensitive matching, 'm' for multiline matching, 'x' for verbose mode,
    /// 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode ('.' matches everything), and 'u' to
    /// make \w, \W, etc. match unicode.
    RegularExpression = ELEMENT_TYPE_REGULAR_EXPRESSION,
    /// Deprecated.
    DbPointer = ELEMENT_TYPE_DBPOINTER,
    /// JavaScript code
    JavaScriptCode = ELEMENT_TYPE_JAVASCRIPT_CODE,
    /// Deprecated.
    Symbol = ELEMENT_TYPE_SYMBOL,
    /// JavaScript code w/ scope
    JavaScriptCodeWithScope = ELEMENT_TYPE_JAVASCRIPT_CODE_WITH_SCOPE,
    /// 32-bit integer
    Integer32Bit = ELEMENT_TYPE_32BIT_INTEGER,
    /// Timestamp
    TimeStamp = ELEMENT_TYPE_TIMESTAMP,
    /// 64-bit integer
    Integer64Bit = ELEMENT_TYPE_64BIT_INTEGER,
    /// [128-bit decimal floating point](https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst)
    #[cfg(feature = "decimal128")]
    Decimal128Bit = ELEMENT_TYPE_128BIT_DECIMAL,
    MaxKey = ELEMENT_TYPE_MAXKEY,
    MinKey = ELEMENT_TYPE_MINKEY,
}

impl ElementType {
    /// Attempt to convert from a `u8`.
    #[inline]
    pub fn from(tag: u8) -> Option<ElementType> {
        use self::ElementType::*;
        Some(match tag {
                 ELEMENT_TYPE_FLOATING_POINT => FloatingPoint,
                 ELEMENT_TYPE_UTF8_STRING => Utf8String,
                 ELEMENT_TYPE_EMBEDDED_DOCUMENT => EmbeddedDocument,
                 ELEMENT_TYPE_ARRAY => Array,
                 ELEMENT_TYPE_BINARY => Binary,
                 ELEMENT_TYPE_UNDEFINED => Undefined,
                 ELEMENT_TYPE_OBJECT_ID => ObjectId,
                 ELEMENT_TYPE_BOOLEAN => Boolean,
                 ELEMENT_TYPE_UTC_DATETIME => UtcDatetime,
                 ELEMENT_TYPE_NULL_VALUE => NullValue,
                 ELEMENT_TYPE_REGULAR_EXPRESSION => RegularExpression,
                 ELEMENT_TYPE_DBPOINTER => DbPointer,
                 ELEMENT_TYPE_JAVASCRIPT_CODE => JavaScriptCode,
                 ELEMENT_TYPE_SYMBOL => Symbol,
                 ELEMENT_TYPE_JAVASCRIPT_CODE_WITH_SCOPE => JavaScriptCodeWithScope,
                 ELEMENT_TYPE_32BIT_INTEGER => Integer32Bit,
                 ELEMENT_TYPE_TIMESTAMP => TimeStamp,
                 ELEMENT_TYPE_64BIT_INTEGER => Integer64Bit,
                 #[cfg(feature = "decimal128")]
                 ELEMENT_TYPE_128BIT_DECIMAL => Decimal128Bit,
                 ELEMENT_TYPE_MAXKEY => MaxKey,
                 ELEMENT_TYPE_MINKEY => MinKey,
                 _ => return None,
             })
    }
}

/// The available binary subtypes, plus a user-defined slot.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum BinarySubtype {
    Generic,
    Function,
    BinaryOld,
    UuidOld,
    Uuid,
    Md5,
    UserDefined(u8),
}

impl From<BinarySubtype> for u8 {
    #[inline]
    fn from(t: BinarySubtype) -> u8 {
        match t {
            BinarySubtype::Generic => BINARY_SUBTYPE_GENERIC,
            BinarySubtype::Function => BINARY_SUBTYPE_FUNCTION,
            BinarySubtype::BinaryOld => BINARY_SUBTYPE_BINARY_OLD,
            BinarySubtype::UuidOld => BINARY_SUBTYPE_UUID_OLD,
            BinarySubtype::Uuid => BINARY_SUBTYPE_UUID,
            BinarySubtype::Md5 => BINARY_SUBTYPE_MD5,
            BinarySubtype::UserDefined(x) => x,
        }
    }
}

impl From<u8> for BinarySubtype {
    #[inline]
    fn from(t: u8) -> BinarySubtype {
        match t {
            BINARY_SUBTYPE_GENERIC => BinarySubtype::Generic,
            BINARY_SUBTYPE_FUNCTION => BinarySubtype::Function,
            BINARY_SUBTYPE_BINARY_OLD => BinarySubtype::BinaryOld,
            BINARY_SUBTYPE_UUID_OLD => BinarySubtype::UuidOld,
            BINARY_SUBTYPE_UUID => BinarySubtype::Uuid,
            BINARY_SUBTYPE_MD5 => BinarySubtype::Md5,
            _ => BinarySubtype::UserDefined(t),
        }
    }
}