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
// 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.0](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_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)]
pub enum ElementType {
    FloatingPoint = ELEMENT_TYPE_FLOATING_POINT,
    Utf8String = ELEMENT_TYPE_UTF8_STRING,
    EmbeddedDocument = ELEMENT_TYPE_EMBEDDED_DOCUMENT,
    Array = ELEMENT_TYPE_ARRAY,
    Binary = ELEMENT_TYPE_BINARY,
    /// Deprecated.
    Undefined = ELEMENT_TYPE_UNDEFINED,
    ObjectId = ELEMENT_TYPE_OBJECT_ID,
    Boolean = ELEMENT_TYPE_BOOLEAN,
    UtcDatetime = ELEMENT_TYPE_UTC_DATETIME,
    NullValue = ELEMENT_TYPE_NULL_VALUE,
    RegularExpression = ELEMENT_TYPE_REGULAR_EXPRESSION,
    /// Deprecated.
    DbPointer = ELEMENT_TYPE_DBPOINTER,
    JavaScriptCode = ELEMENT_TYPE_JAVASCRIPT_CODE,
    /// Deprecated.
    Symbol = ELEMENT_TYPE_SYMBOL,
    JavaScriptCodeWithScope = ELEMENT_TYPE_JAVASCRIPT_CODE_WITH_SCOPE,
    Integer32Bit = ELEMENT_TYPE_32BIT_INTEGER,
    TimeStamp = ELEMENT_TYPE_TIMESTAMP,
    Integer64Bit = ELEMENT_TYPE_64BIT_INTEGER,

    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,
                 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),
        }
    }
}