Skip to content

Commit

Permalink
Merge pull request #875 from serates/master
Browse files Browse the repository at this point in the history
Add feature(bson-uuid) which will enable Uuid's from the bson crate
  • Loading branch information
sunli829 committed Apr 19, 2022
2 parents ecc75b8 + cb3fe37 commit 4218b8a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/types/external/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use bson::{oid::ObjectId, Bson, Document};

#[cfg(feature = "chrono")]
use bson::DateTime as UtcDateTime;
#[cfg(feature = "bson-uuid")]
use bson::Uuid;
#[cfg(feature = "chrono")]
use chrono::{DateTime, Utc};

Expand All @@ -21,6 +23,21 @@ impl ScalarType for ObjectId {
}
}

#[cfg(feature = "bson-uuid")]
#[Scalar(internal, name = "UUID")]
impl ScalarType for Uuid {
fn parse(value: Value) -> InputValueResult<Self> {
match value {
Value::String(s) => Ok(Uuid::parse_str(&s)?),
_ => Err(InputValueError::expected_type(value)),
}
}

fn to_value(&self) -> Value {
Value::String(self.to_string())
}
}

#[cfg(feature = "chrono")]
#[Scalar(internal, name = "DateTime")]
impl ScalarType for UtcDateTime {
Expand Down

0 comments on commit 4218b8a

Please sign in to comment.