Skip to content

RUST-787 impl Display for various Bson types #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions src/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,15 @@ impl Display for Bson {
Bson::Document(ref doc) => write!(fmt, "{}", doc),
Bson::Boolean(b) => write!(fmt, "{}", b),
Bson::Null => write!(fmt, "null"),
Bson::RegularExpression(Regex {
ref pattern,
ref options,
}) => write!(fmt, "/{}/{}", pattern, options),
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(Timestamp { time, increment }) => {
write!(fmt, "Timestamp({}, {})", time, increment)
}
Bson::Binary(Binary { subtype, ref bytes }) => write!(
fmt,
"Binary({:#x}, {})",
u8::from(subtype),
base64::encode(bytes)
),
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),
Expand Down Expand Up @@ -984,6 +974,12 @@ pub struct Timestamp {
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;
Expand Down Expand Up @@ -1018,13 +1014,25 @@ pub struct Regex {
pub options: String,
}

impl Display for Regex {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "/{}/{}", self.pattern, self.options)
}
}

/// Represents a BSON code with scope value.
#[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)
}
}

/// Represents a BSON binary value.
#[derive(Debug, Clone, PartialEq)]
pub struct Binary {
Expand All @@ -1035,6 +1043,17 @@ pub struct Binary {
pub bytes: Vec<u8>,
}

impl Display for Binary {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"Binary({:#x}, {})",
u8::from(self.subtype),
base64::encode(&self.bytes)
)
}
}

impl Binary {
fn from_extended_doc(doc: &Document) -> Option<Self> {
let binary = doc.get_document("$binary").ok()?;
Expand Down
46 changes: 46 additions & 0 deletions src/tests/modules/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,52 @@ fn bson_default() {
assert_eq!(bson1, Bson::Null);
}

#[test]
fn test_display_timestamp_type() {
let x = Timestamp {
time: 100,
increment: 200,
};
let output = "Timestamp(100, 200)";
assert_eq!(format!("{}", x), output);
assert_eq!(format!("{}", Bson::from(x)), output);
}

#[test]
fn test_display_regex_type() {
let x = Regex {
pattern: String::from("pattern"),
options: String::from("options"),
};
let output = "/pattern/options";
assert_eq!(format!("{}", x), output);
assert_eq!(format!("{}", Bson::from(x)), output);
}

#[test]
fn test_display_jscodewithcontext_type() {
let x = JavaScriptCodeWithScope {
code: String::from("code"),
scope: doc! {"x": 2},
};
let output = "code";
assert_eq!(format!("{}", x), output);
assert_eq!(format!("{}", Bson::from(x)), output);
}

#[test]
fn test_display_binary_type() {
let encoded_bytes = "aGVsbG8gd29ybGQ=";
let bytes = base64::decode(encoded_bytes).unwrap();
let x = Binary {
subtype: BinarySubtype::Generic,
bytes,
};
let output = format!("Binary(0x0, {})", encoded_bytes);
assert_eq!(format!("{}", x), output);
assert_eq!(format!("{}", Bson::from(x)), output);
}

#[test]
fn document_default() {
let _guard = LOCK.run_concurrently();
Expand Down