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
pub const BINARY_SUBTYPE_JSON: u8 = 0x90;





pub mod json;

use std::fmt::Formatter;
use rbson::Bson;
use rbson::spec::BinarySubtype;
pub use json::*;

pub mod uuids;

pub use uuids::*;

pub mod bytes;

pub use bytes::*;

pub mod datetime_native;

pub use datetime_native::*;

pub mod datetime_utc;

pub use datetime_utc::*;

pub mod date_native;

pub use date_native::*;

pub mod date_utc;

pub use date_utc::*;

pub mod time_native;

pub use time_native::*;

pub mod time_utc;

pub use time_utc::*;

pub mod decimal;

pub use decimal::*;

pub mod timestamp;

pub use timestamp::*;

pub mod timestamp_z;

pub use timestamp_z::*;

pub mod bools;

pub use bools::*;

pub trait Format {
    fn do_format(&self) -> String;
}


impl Format for Bson {
    fn do_format(&self) -> String {
        match self {
            Bson::Double(d) => { format!("{}", d) }
            Bson::String(s) => { format!("\"{}\"", s) }
            Bson::Array(arr) => {
                arr.do_format()
            }
            Bson::Document(d) => {
                let mut buf = String::new();
                buf.push_str("{");
                for (k, v) in d {
                    buf.push_str("\"");
                    buf.push_str(k);
                    buf.push_str("\"");
                    buf.push_str(":");
                    buf.push_str(&v.do_format());
                    buf.push_str(",");
                }
                buf.pop();
                buf.push_str("}");
                buf
            }
            Bson::Boolean(b) => { format!("{}", b) }
            Bson::Null => { format!("null") }
            Bson::RegularExpression(j) => { format!("{:?}", j) }
            Bson::JavaScriptCode(j) => { format!("{:?}", j) }
            Bson::JavaScriptCodeWithScope(j) => { format!("{:?}", j) }
            Bson::Int32(i) => { format!("{}", i) }
            Bson::Int64(i) => { format!("{}", i) }
            Bson::UInt32(i) => { format!("{}", i) }
            Bson::UInt64(i) => { format!("{}", i) }
            Bson::Timestamp(s) => {
                format!("\"{}\"", Timestamp::from(s.clone()))
            }
            Bson::Binary(d) => {
                match d.subtype {
                    BinarySubtype::Generic => {
                        let bytes_len = d.bytes.len();
                        if bytes_len > 8192 {
                            //> 1kb
                            format!("bytes({})", d.bytes.len())
                        } else {
                            self.to_string()
                        }
                    }
                    BinarySubtype::Uuid => {
                        format!("\"{}\"", crate::types::Uuid::from(d))
                    }
                    BinarySubtype::UserDefined(type_id) => {
                        match type_id {
                            crate::types::BINARY_SUBTYPE_JSON => {
                                format!("{}", String::from_utf8(d.bytes.to_owned()).unwrap_or_default())
                            }
                            _ => {
                                format!("un supported!")
                            }
                        }
                    }
                    _ => {
                        format!("un supported!")
                    }
                }
            }
            Bson::ObjectId(id) => {
                format!("\"{}\"", id)
            }
            Bson::DateTime(dt) => {
                format!("\"{}\"", DateTimeNative::from(dt.clone()))
            }
            Bson::Symbol(s) => { format!("{}", s) }
            Bson::Decimal128(d) => {
                format!("{}", d)
            }
            Bson::Undefined => {
                format!("{}", "Undefined")
            }
            Bson::MaxKey => { format!("{}", "MaxKey") }
            Bson::MinKey => { format!("{}", "MinKey") }
            Bson::DbPointer(p) => { format!("{:?}", p) }
        }
    }
}

impl Format for Vec<Bson> {
    fn do_format(&self) -> String {
        let mut buf = String::new();
        buf.push_str("[");
        for item in self {
            buf.push_str(&item.do_format());
            buf.push_str(",");
        }
        buf.pop();
        buf.push_str("]");
        buf
    }
}