use std::{
convert::{TryFrom, TryInto},
fmt::{self, Debug, Display, Formatter},
};
use serde_json::{json, Value};
pub use crate::document::Document;
use crate::{
oid::{self, ObjectId},
spec::{BinarySubtype, ElementType},
Binary,
Decimal128,
};
#[derive(Clone, PartialEq)]
pub enum Bson {
Double(f64),
String(String),
Array(Array),
Document(Document),
Boolean(bool),
Null,
RegularExpression(Regex),
JavaScriptCode(String),
JavaScriptCodeWithScope(JavaScriptCodeWithScope),
Int32(i32),
Int64(i64),
Timestamp(Timestamp),
Binary(Binary),
ObjectId(oid::ObjectId),
DateTime(crate::DateTime),
Symbol(String),
Decimal128(Decimal128),
Undefined,
MaxKey,
MinKey,
DbPointer(DbPointer),
}
pub type Array = Vec<Bson>;
impl Default for Bson {
fn default() -> Self {
Bson::Null
}
}
impl Display for Bson {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Bson::Double(f) => write!(fmt, "{}", f),
Bson::String(ref s) => write!(fmt, "\"{}\"", s),
Bson::Array(ref vec) => {
fmt.write_str("[")?;
let mut first = true;
for bson in vec {
if !first {
fmt.write_str(", ")?;
}
write!(fmt, "{}", bson)?;
first = false;
}
fmt.write_str("]")
}
Bson::Document(ref doc) => write!(fmt, "{}", doc),
Bson::Boolean(b) => write!(fmt, "{}", b),
Bson::Null => write!(fmt, "null"),
Bson::RegularExpression(ref x) => write!(fmt, "{}", x),
Bson::JavaScriptCode(ref code)
| Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope { ref code, .. }) => {
fmt.write_str(code)
}
Bson::Int32(i) => write!(fmt, "{}", i),
Bson::Int64(i) => write!(fmt, "{}", i),
Bson::Timestamp(ref x) => write!(fmt, "{}", x),
Bson::Binary(ref x) => write!(fmt, "{}", x),
Bson::ObjectId(ref id) => write!(fmt, "ObjectId(\"{}\")", id),
Bson::DateTime(date_time) => write!(fmt, "DateTime(\"{}\")", date_time),
Bson::Symbol(ref sym) => write!(fmt, "Symbol(\"{}\")", sym),
Bson::Decimal128(ref d) => write!(fmt, "{}", d),
Bson::Undefined => write!(fmt, "undefined"),
Bson::MinKey => write!(fmt, "MinKey"),
Bson::MaxKey => write!(fmt, "MaxKey"),
Bson::DbPointer(DbPointer {
ref namespace,
ref id,
}) => write!(fmt, "DbPointer({}, {})", namespace, id),
}
}
}
impl Debug for Bson {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
match *self {
Bson::Double(f) => fmt.debug_tuple("Double").field(&f).finish(),
Bson::String(ref s) => fmt.debug_tuple("String").field(s).finish(),
Bson::Array(ref vec) => {
write!(fmt, "Array(")?;
Debug::fmt(vec, fmt)?;
write!(fmt, ")")
}
Bson::Document(ref doc) => Debug::fmt(doc, fmt),
Bson::Boolean(b) => fmt.debug_tuple("Boolean").field(&b).finish(),
Bson::Null => write!(fmt, "Null"),
Bson::RegularExpression(ref regex) => Debug::fmt(regex, fmt),
Bson::JavaScriptCode(ref code) => {
fmt.debug_tuple("JavaScriptCode").field(code).finish()
}
Bson::JavaScriptCodeWithScope(ref code) => Debug::fmt(code, fmt),
Bson::Int32(i) => fmt.debug_tuple("Int32").field(&i).finish(),
Bson::Int64(i) => fmt.debug_tuple("Int64").field(&i).finish(),
Bson::Timestamp(ref t) => Debug::fmt(t, fmt),
Bson::Binary(ref b) => Debug::fmt(b, fmt),
Bson::ObjectId(ref id) => Debug::fmt(id, fmt),
Bson::DateTime(ref date_time) => Debug::fmt(date_time, fmt),
Bson::Symbol(ref sym) => fmt.debug_tuple("Symbol").field(sym).finish(),
Bson::Decimal128(ref d) => Debug::fmt(d, fmt),
Bson::Undefined => write!(fmt, "Undefined"),
Bson::MinKey => write!(fmt, "MinKey"),
Bson::MaxKey => write!(fmt, "MaxKey"),
Bson::DbPointer(ref pointer) => Debug::fmt(pointer, fmt),
}
}
}
impl From<f32> for Bson {
fn from(a: f32) -> Bson {
Bson::Double(a.into())
}
}
impl From<f64> for Bson {
fn from(a: f64) -> Bson {
Bson::Double(a)
}
}
impl From<&str> for Bson {
fn from(s: &str) -> Bson {
Bson::String(s.to_owned())
}
}
impl From<String> for Bson {
fn from(a: String) -> Bson {
Bson::String(a)
}
}
impl From<Document> for Bson {
fn from(a: Document) -> Bson {
Bson::Document(a)
}
}
impl From<bool> for Bson {
fn from(a: bool) -> Bson {
Bson::Boolean(a)
}
}
impl From<Regex> for Bson {
fn from(regex: Regex) -> Bson {
Bson::RegularExpression(regex)
}
}
impl From<JavaScriptCodeWithScope> for Bson {
fn from(code_with_scope: JavaScriptCodeWithScope) -> Bson {
Bson::JavaScriptCodeWithScope(code_with_scope)
}
}
impl From<Binary> for Bson {
fn from(binary: Binary) -> Bson {
Bson::Binary(binary)
}
}
impl From<Timestamp> for Bson {
fn from(ts: Timestamp) -> Bson {
Bson::Timestamp(ts)
}
}
impl<T> From<&T> for Bson
where
T: Clone + Into<Bson>,
{
fn from(t: &T) -> Bson {
t.clone().into()
}
}
impl<T> From<Vec<T>> for Bson
where
T: Into<Bson>,
{
fn from(v: Vec<T>) -> Bson {
Bson::Array(v.into_iter().map(|val| val.into()).collect())
}
}
impl<T> From<&[T]> for Bson
where
T: Clone + Into<Bson>,
{
fn from(s: &[T]) -> Bson {
Bson::Array(s.iter().cloned().map(|val| val.into()).collect())
}
}
impl<T: Into<Bson>> ::std::iter::FromIterator<T> for Bson {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Bson::Array(iter.into_iter().map(Into::into).collect())
}
}
impl From<i32> for Bson {
fn from(a: i32) -> Bson {
Bson::Int32(a)
}
}
impl From<i64> for Bson {
fn from(a: i64) -> Bson {
Bson::Int64(a)
}
}
impl From<u32> for Bson {
fn from(a: u32) -> Bson {
if let Ok(i) = i32::try_from(a) {
Bson::Int32(i)
} else {
Bson::Int64(a.into())
}
}
}
impl From<[u8; 12]> for Bson {
fn from(a: [u8; 12]) -> Bson {
Bson::ObjectId(oid::ObjectId::from_bytes(a))
}
}
impl From<oid::ObjectId> for Bson {
fn from(a: oid::ObjectId) -> Bson {
Bson::ObjectId(a)
}
}
#[cfg(feature = "time-0_3")]
#[cfg_attr(docsrs, doc(cfg(feature = "time-0_3")))]
impl From<time::OffsetDateTime> for Bson {
fn from(a: time::OffsetDateTime) -> Bson {
Bson::DateTime(crate::DateTime::from(a))
}
}
#[cfg(feature = "chrono-0_4")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono-0_4")))]
impl<T: chrono::TimeZone> From<chrono::DateTime<T>> for Bson {
fn from(a: chrono::DateTime<T>) -> Bson {
Bson::DateTime(crate::DateTime::from(a))
}
}
#[cfg(feature = "uuid-0_8")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-0_8")))]
impl From<uuid_0_8::Uuid> for Bson {
fn from(uuid: uuid_0_8::Uuid) -> Self {
Bson::Binary(uuid.into())
}
}
#[cfg(feature = "uuid-1")]
#[cfg_attr(docsrs, doc(cfg(feature = "uuid-1")))]
impl From<uuid::Uuid> for Bson {
fn from(uuid: uuid::Uuid) -> Self {
Bson::Binary(uuid.into())
}
}
impl From<crate::DateTime> for Bson {
fn from(dt: crate::DateTime) -> Self {
Bson::DateTime(dt)
}
}
impl From<DbPointer> for Bson {
fn from(a: DbPointer) -> Bson {
Bson::DbPointer(a)
}
}
impl From<Decimal128> for Bson {
fn from(d: Decimal128) -> Self {
Bson::Decimal128(d)
}
}
impl<T> From<Option<T>> for Bson
where
T: Into<Bson>,
{
fn from(a: Option<T>) -> Bson {
match a {
None => Bson::Null,
Some(t) => t.into(),
}
}
}
impl From<Bson> for Value {
fn from(bson: Bson) -> Self {
bson.into_relaxed_extjson()
}
}
impl Bson {
pub fn into_relaxed_extjson(self) -> Value {
match self {
Bson::Double(v) if v.is_nan() => {
let s = if v.is_sign_negative() { "-NaN" } else { "NaN" };
json!({ "$numberDouble": s })
}
Bson::Double(v) if v.is_infinite() => {
let s = if v.is_sign_negative() {
"-Infinity"
} else {
"Infinity"
};
json!({ "$numberDouble": s })
}
Bson::Double(v) => json!(v),
Bson::String(v) => json!(v),
Bson::Array(v) => Value::Array(v.into_iter().map(Bson::into_relaxed_extjson).collect()),
Bson::Document(v) => Value::Object(
v.into_iter()
.map(|(k, v)| (k, v.into_relaxed_extjson()))
.collect(),
),
Bson::Boolean(v) => json!(v),
Bson::Null => Value::Null,
Bson::RegularExpression(Regex { pattern, options }) => {
let mut chars: Vec<_> = options.chars().collect();
chars.sort_unstable();
let options: String = chars.into_iter().collect();
json!({
"$regularExpression": {
"pattern": pattern,
"options": options,
}
})
}
Bson::JavaScriptCode(code) => json!({ "$code": code }),
Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope { code, scope }) => json!({
"$code": code,
"$scope": scope,
}),
Bson::Int32(v) => v.into(),
Bson::Int64(v) => v.into(),
Bson::Timestamp(Timestamp { time, increment }) => json!({
"$timestamp": {
"t": time,
"i": increment,
}
}),
Bson::Binary(Binary { subtype, ref bytes }) => {
let tval: u8 = From::from(subtype);
json!({
"$binary": {
"base64": base64::encode(bytes),
"subType": hex::encode([tval]),
}
})
}
Bson::ObjectId(v) => json!({"$oid": v.to_hex()}),
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_time_0_3().year() <= 9999 => {
json!({
"$date": v.try_to_rfc3339_string().unwrap(),
})
}
Bson::DateTime(v) => json!({
"$date": { "$numberLong": v.timestamp_millis().to_string() },
}),
Bson::Symbol(v) => json!({ "$symbol": v }),
Bson::Decimal128(_) => panic!("Decimal128 extended JSON not implemented yet."),
Bson::Undefined => json!({ "$undefined": true }),
Bson::MinKey => json!({ "$minKey": 1 }),
Bson::MaxKey => json!({ "$maxKey": 1 }),
Bson::DbPointer(DbPointer {
ref namespace,
ref id,
}) => json!({
"$dbPointer": {
"$ref": namespace,
"$id": {
"$oid": id.to_hex()
}
}
}),
}
}
pub fn into_canonical_extjson(self) -> Value {
match self {
Bson::Int32(i) => json!({ "$numberInt": i.to_string() }),
Bson::Int64(i) => json!({ "$numberLong": i.to_string() }),
Bson::Double(f) if f.is_normal() => {
let mut s = f.to_string();
if f.fract() == 0.0 {
s.push_str(".0");
}
json!({ "$numberDouble": s })
}
Bson::Double(f) if f == 0.0 => {
let s = if f.is_sign_negative() { "-0.0" } else { "0.0" };
json!({ "$numberDouble": s })
}
Bson::DateTime(date) => {
json!({ "$date": { "$numberLong": date.timestamp_millis().to_string() } })
}
Bson::Array(arr) => {
Value::Array(arr.into_iter().map(Bson::into_canonical_extjson).collect())
}
Bson::Document(arr) => Value::Object(
arr.into_iter()
.map(|(k, v)| (k, v.into_canonical_extjson()))
.collect(),
),
Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope { code, scope }) => json!({
"$code": code,
"$scope": Bson::Document(scope).into_canonical_extjson(),
}),
other => other.into_relaxed_extjson(),
}
}
pub fn element_type(&self) -> ElementType {
match *self {
Bson::Double(..) => ElementType::Double,
Bson::String(..) => ElementType::String,
Bson::Array(..) => ElementType::Array,
Bson::Document(..) => ElementType::EmbeddedDocument,
Bson::Boolean(..) => ElementType::Boolean,
Bson::Null => ElementType::Null,
Bson::RegularExpression(..) => ElementType::RegularExpression,
Bson::JavaScriptCode(..) => ElementType::JavaScriptCode,
Bson::JavaScriptCodeWithScope(..) => ElementType::JavaScriptCodeWithScope,
Bson::Int32(..) => ElementType::Int32,
Bson::Int64(..) => ElementType::Int64,
Bson::Timestamp(..) => ElementType::Timestamp,
Bson::Binary(..) => ElementType::Binary,
Bson::ObjectId(..) => ElementType::ObjectId,
Bson::DateTime(..) => ElementType::DateTime,
Bson::Symbol(..) => ElementType::Symbol,
Bson::Decimal128(..) => ElementType::Decimal128,
Bson::Undefined => ElementType::Undefined,
Bson::MaxKey => ElementType::MaxKey,
Bson::MinKey => ElementType::MinKey,
Bson::DbPointer(..) => ElementType::DbPointer,
}
}
pub(crate) fn into_extended_document(self, rawbson: bool) -> Document {
match self {
Bson::RegularExpression(Regex {
ref pattern,
ref options,
}) => {
let mut chars: Vec<_> = options.chars().collect();
chars.sort_unstable();
let options: String = chars.into_iter().collect();
doc! {
"$regularExpression": {
"pattern": pattern,
"options": options,
}
}
}
Bson::JavaScriptCode(ref code) => {
doc! {
"$code": code,
}
}
Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope { code, scope }) => {
doc! {
"$code": code,
"$scope": scope,
}
}
Bson::Timestamp(Timestamp { time, increment }) => {
doc! {
"$timestamp": {
"t": time,
"i": increment,
}
}
}
Bson::Binary(Binary { subtype, bytes }) => {
let tval: u8 = From::from(subtype);
if rawbson {
doc! {
"$binary": {
"bytes": Binary { subtype: BinarySubtype::Generic, bytes },
"subType": Bson::Int32(tval.into())
}
}
} else {
doc! {
"$binary": {
"base64": base64::encode(bytes),
"subType": hex::encode([tval]),
}
}
}
}
Bson::ObjectId(ref v) => {
doc! {
"$oid": v.to_string(),
}
}
Bson::DateTime(v) if rawbson => doc! {
"$date": v.timestamp_millis(),
},
Bson::DateTime(v) if v.timestamp_millis() >= 0 && v.to_time_0_3().year() <= 9999 => {
doc! {
"$date": v.try_to_rfc3339_string().unwrap(),
}
}
Bson::DateTime(v) => doc! {
"$date": { "$numberLong": v.timestamp_millis().to_string() },
},
Bson::Symbol(ref v) => {
doc! {
"$symbol": v.to_owned(),
}
}
Bson::Undefined => {
doc! {
"$undefined": true,
}
}
Bson::MinKey => {
doc! {
"$minKey": 1,
}
}
Bson::MaxKey => {
doc! {
"$maxKey": 1,
}
}
Bson::DbPointer(DbPointer {
ref namespace,
ref id,
}) => {
doc! {
"$dbPointer": {
"$ref": namespace,
"$id": {
"$oid": id.to_string()
}
}
}
}
_ => panic!("Attempted conversion of invalid data type: {}", self),
}
}
pub(crate) fn from_extended_document(doc: Document) -> Bson {
if doc.len() > 2 {
return Bson::Document(doc);
}
let mut keys: Vec<_> = doc.keys().map(|s| s.as_str()).collect();
keys.sort_unstable();
match keys.as_slice() {
["$oid"] => {
if let Ok(oid) = doc.get_str("$oid") {
if let Ok(oid) = ObjectId::parse_str(oid) {
return Bson::ObjectId(oid);
}
}
}
["$symbol"] => {
if let Ok(symbol) = doc.get_str("$symbol") {
return Bson::Symbol(symbol.into());
}
}
["$numberInt"] => {
if let Ok(i) = doc.get_str("$numberInt") {
if let Ok(i) = i.parse() {
return Bson::Int32(i);
}
}
}
["$numberLong"] => {
if let Ok(i) = doc.get_str("$numberLong") {
if let Ok(i) = i.parse() {
return Bson::Int64(i);
}
}
}
["$numberDouble"] => match doc.get_str("$numberDouble") {
Ok("Infinity") => return Bson::Double(std::f64::INFINITY),
Ok("-Infinity") => return Bson::Double(std::f64::NEG_INFINITY),
Ok("NaN") => return Bson::Double(std::f64::NAN),
Ok(other) => {
if let Ok(d) = other.parse() {
return Bson::Double(d);
}
}
_ => {}
},
["$numberDecimalBytes"] => {
if let Ok(bytes) = doc.get_binary_generic("$numberDecimalBytes") {
if let Ok(b) = bytes.clone().try_into() {
return Bson::Decimal128(Decimal128 { bytes: b });
}
}
}
["$binary"] => {
if let Some(binary) = Binary::from_extended_doc(&doc) {
return Bson::Binary(binary);
}
}
["$code"] => {
if let Ok(code) = doc.get_str("$code") {
return Bson::JavaScriptCode(code.into());
}
}
["$code", "$scope"] => {
if let Ok(code) = doc.get_str("$code") {
if let Ok(scope) = doc.get_document("$scope") {
return Bson::JavaScriptCodeWithScope(JavaScriptCodeWithScope {
code: code.into(),
scope: scope.clone(),
});
}
}
}
["$timestamp"] => {
if let Ok(timestamp) = doc.get_document("$timestamp") {
if let Ok(t) = timestamp.get_i32("t") {
if let Ok(i) = timestamp.get_i32("i") {
return Bson::Timestamp(Timestamp {
time: t as u32,
increment: i as u32,
});
}
}
if let Ok(t) = timestamp.get_i64("t") {
if let Ok(i) = timestamp.get_i64("i") {
if t >= 0
&& i >= 0
&& t <= (std::u32::MAX as i64)
&& i <= (std::u32::MAX as i64)
{
return Bson::Timestamp(Timestamp {
time: t as u32,
increment: i as u32,
});
}
}
}
}
}
["$regularExpression"] => {
if let Ok(regex) = doc.get_document("$regularExpression") {
if let Ok(pattern) = regex.get_str("pattern") {
if let Ok(options) = regex.get_str("options") {
return Bson::RegularExpression(Regex::new(pattern, options));
}
}
}
}
["$dbPointer"] => {
if let Ok(db_pointer) = doc.get_document("$dbPointer") {
if let Ok(ns) = db_pointer.get_str("$ref") {
if let Ok(id) = db_pointer.get_object_id("$id") {
return Bson::DbPointer(DbPointer {
namespace: ns.into(),
id,
});
}
}
}
}
["$date"] => {
if let Ok(date) = doc.get_i64("$date") {
return Bson::DateTime(crate::DateTime::from_millis(date));
}
if let Ok(date) = doc.get_str("$date") {
if let Ok(dt) = crate::DateTime::parse_rfc3339_str(date) {
return Bson::DateTime(dt);
}
}
}
["$minKey"] => {
let min_key = doc.get("$minKey");
if min_key == Some(&Bson::Int32(1)) || min_key == Some(&Bson::Int64(1)) {
return Bson::MinKey;
}
}
["$maxKey"] => {
let max_key = doc.get("$maxKey");
if max_key == Some(&Bson::Int32(1)) || max_key == Some(&Bson::Int64(1)) {
return Bson::MaxKey;
}
}
["$undefined"] => {
if doc.get("$undefined") == Some(&Bson::Boolean(true)) {
return Bson::Undefined;
}
}
_ => {}
};
Bson::Document(
doc.into_iter()
.map(|(k, v)| {
let v = match v {
Bson::Document(v) => Bson::from_extended_document(v),
other => other,
};
(k, v)
})
.collect(),
)
}
}
impl Bson {
pub fn as_f64(&self) -> Option<f64> {
match *self {
Bson::Double(v) => Some(v),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match *self {
Bson::String(ref s) => Some(s),
_ => None,
}
}
pub fn as_str_mut(&mut self) -> Option<&mut str> {
match *self {
Bson::String(ref mut s) => Some(s),
_ => None,
}
}
pub fn as_array(&self) -> Option<&Array> {
match *self {
Bson::Array(ref v) => Some(v),
_ => None,
}
}
pub fn as_array_mut(&mut self) -> Option<&mut Array> {
match *self {
Bson::Array(ref mut v) => Some(v),
_ => None,
}
}
pub fn as_document(&self) -> Option<&Document> {
match *self {
Bson::Document(ref v) => Some(v),
_ => None,
}
}
pub fn as_document_mut(&mut self) -> Option<&mut Document> {
match *self {
Bson::Document(ref mut v) => Some(v),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match *self {
Bson::Boolean(v) => Some(v),
_ => None,
}
}
pub fn as_i32(&self) -> Option<i32> {
match *self {
Bson::Int32(v) => Some(v),
_ => None,
}
}
pub fn as_i64(&self) -> Option<i64> {
match *self {
Bson::Int64(v) => Some(v),
_ => None,
}
}
pub fn as_object_id(&self) -> Option<oid::ObjectId> {
match *self {
Bson::ObjectId(v) => Some(v),
_ => None,
}
}
pub fn as_object_id_mut(&mut self) -> Option<&mut oid::ObjectId> {
match *self {
Bson::ObjectId(ref mut v) => Some(v),
_ => None,
}
}
pub fn as_datetime(&self) -> Option<&crate::DateTime> {
match *self {
Bson::DateTime(ref v) => Some(v),
_ => None,
}
}
pub fn as_datetime_mut(&mut self) -> Option<&mut crate::DateTime> {
match *self {
Bson::DateTime(ref mut v) => Some(v),
_ => None,
}
}
pub fn as_symbol(&self) -> Option<&str> {
match *self {
Bson::Symbol(ref v) => Some(v),
_ => None,
}
}
pub fn as_symbol_mut(&mut self) -> Option<&mut str> {
match *self {
Bson::Symbol(ref mut v) => Some(v),
_ => None,
}
}
pub fn as_timestamp(&self) -> Option<Timestamp> {
match *self {
Bson::Timestamp(timestamp) => Some(timestamp),
_ => None,
}
}
pub fn as_null(&self) -> Option<()> {
match *self {
Bson::Null => Some(()),
_ => None,
}
}
pub fn as_db_pointer(&self) -> Option<&DbPointer> {
match self {
Bson::DbPointer(ref db_pointer) => Some(db_pointer),
_ => None,
}
}
}
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd, Clone, Copy, Hash)]
pub struct Timestamp {
pub time: u32,
pub increment: u32,
}
impl Display for Timestamp {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "Timestamp({}, {})", self.time, self.increment)
}
}
impl Timestamp {
pub(crate) fn to_le_i64(self) -> i64 {
let upper = (self.time.to_le() as u64) << 32;
let lower = self.increment.to_le() as u64;
(upper | lower) as i64
}
pub(crate) fn from_le_i64(val: i64) -> Self {
let ts = val.to_le();
Timestamp {
time: ((ts as u64) >> 32) as u32,
increment: (ts & 0xFFFF_FFFF) as u32,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Regex {
pub pattern: String,
pub options: String,
}
impl Regex {
pub(crate) fn new(pattern: impl AsRef<str>, options: impl AsRef<str>) -> Self {
let mut chars: Vec<_> = options.as_ref().chars().collect();
chars.sort_unstable();
let options: String = chars.into_iter().collect();
Self {
pattern: pattern.as_ref().to_string(),
options,
}
}
}
impl Display for Regex {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "/{}/{}", self.pattern, self.options)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct JavaScriptCodeWithScope {
pub code: String,
pub scope: Document,
}
impl Display for JavaScriptCodeWithScope {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(&self.code)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DbPointer {
pub(crate) namespace: String,
pub(crate) id: oid::ObjectId,
}