mybatis_drive/types/
mod.rs1pub const BINARY_SUBTYPE_JSON: u8 = 0x90;
2
3
4
5
6
7pub mod json;
8
9use std::fmt::Formatter;
10use rbson::Bson;
11use rbson::spec::BinarySubtype;
12pub use json::*;
13
14pub mod uuids;
15
16pub use uuids::*;
17
18pub mod bytes;
19
20pub use bytes::*;
21
22pub mod datetime_native;
23
24pub use datetime_native::*;
25
26pub mod datetime_utc;
27
28pub use datetime_utc::*;
29
30pub mod date_native;
31
32pub use date_native::*;
33
34pub mod date_utc;
35
36pub use date_utc::*;
37
38pub mod time_native;
39
40pub use time_native::*;
41
42pub mod time_utc;
43
44pub use time_utc::*;
45
46pub mod decimal;
47
48pub use decimal::*;
49
50pub mod timestamp;
51
52pub use timestamp::*;
53
54pub mod timestamp_z;
55
56pub use timestamp_z::*;
57
58pub mod bools;
59
60pub use bools::*;
61
62pub trait Format {
63 fn do_format(&self) -> String;
64}
65
66
67impl Format for Bson {
68 fn do_format(&self) -> String {
69 match self {
70 Bson::Double(d) => { format!("{}", d) }
71 Bson::String(s) => { format!("\"{}\"", s) }
72 Bson::Array(arr) => {
73 arr.do_format()
74 }
75 Bson::Document(d) => {
76 let mut buf = String::new();
77 buf.push_str("{");
78 for (k, v) in d {
79 buf.push_str("\"");
80 buf.push_str(k);
81 buf.push_str("\"");
82 buf.push_str(":");
83 buf.push_str(&v.do_format());
84 buf.push_str(",");
85 }
86 buf.pop();
87 buf.push_str("}");
88 buf
89 }
90 Bson::Boolean(b) => { format!("{}", b) }
91 Bson::Null => { format!("null") }
92 Bson::RegularExpression(j) => { format!("{:?}", j) }
93 Bson::JavaScriptCode(j) => { format!("{:?}", j) }
94 Bson::JavaScriptCodeWithScope(j) => { format!("{:?}", j) }
95 Bson::Int32(i) => { format!("{}", i) }
96 Bson::Int64(i) => { format!("{}", i) }
97 Bson::UInt32(i) => { format!("{}", i) }
98 Bson::UInt64(i) => { format!("{}", i) }
99 Bson::Timestamp(s) => {
100 format!("\"{}\"", Timestamp::from(s.clone()))
101 }
102 Bson::Binary(d) => {
103 match d.subtype {
104 BinarySubtype::Generic => {
105 let bytes_len = d.bytes.len();
106 if bytes_len > 8192 {
107 format!("bytes({})", d.bytes.len())
109 } else {
110 self.to_string()
111 }
112 }
113 BinarySubtype::Uuid => {
114 format!("\"{}\"", crate::types::Uuid::from(d))
115 }
116 BinarySubtype::UserDefined(type_id) => {
117 match type_id {
118 crate::types::BINARY_SUBTYPE_JSON => {
119 format!("{}", String::from_utf8(d.bytes.to_owned()).unwrap_or_default())
120 }
121 _ => {
122 format!("un supported!")
123 }
124 }
125 }
126 _ => {
127 format!("un supported!")
128 }
129 }
130 }
131 Bson::ObjectId(id) => {
132 format!("\"{}\"", id)
133 }
134 Bson::DateTime(dt) => {
135 format!("\"{}\"", DateTimeNative::from(dt.clone()))
136 }
137 Bson::Symbol(s) => { format!("{}", s) }
138 Bson::Decimal128(d) => {
139 format!("{}", d)
140 }
141 Bson::Undefined => {
142 format!("{}", "Undefined")
143 }
144 Bson::MaxKey => { format!("{}", "MaxKey") }
145 Bson::MinKey => { format!("{}", "MinKey") }
146 Bson::DbPointer(p) => { format!("{:?}", p) }
147 }
148 }
149}
150
151impl Format for Vec<Bson> {
152 fn do_format(&self) -> String {
153 let mut buf = String::new();
154 buf.push_str("[");
155 for item in self {
156 buf.push_str(&item.do_format());
157 buf.push_str(",");
158 }
159 buf.pop();
160 buf.push_str("]");
161 buf
162 }
163}