Skip to content
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

#376 Implemented TryFrom<&str> for ObjectId #378

Closed
Closed
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
29 changes: 28 additions & 1 deletion src/oid.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! ObjectId

use std::{
convert::TryInto,
convert::{TryFrom, TryInto},
error,
fmt,
result,
Expand Down Expand Up @@ -222,6 +222,20 @@ impl fmt::Debug for ObjectId {
}
}

impl TryFrom<&str> for ObjectId {
type Error = Error;
fn try_from(value: &str) -> result::Result<Self, Self::Error> {
ObjectId::parse_str(value)
}
}

impl TryFrom<String> for ObjectId {
type Error = Error;
fn try_from(value: String) -> result::Result<Self, Self::Error> {
ObjectId::parse_str(value.as_str())
}
}

#[cfg(test)]
use crate::tests::LOCK;

Expand Down Expand Up @@ -283,6 +297,8 @@ fn test_counter_overflow_usize_max() {

#[cfg(test)]
mod test {
use crate::oid::ObjectId;
use std::convert::{TryFrom, TryInto};
use time::macros::datetime;

#[test]
Expand Down Expand Up @@ -333,4 +349,15 @@ mod test {
id.timestamp().to_time_0_3()
);
}

#[test]
fn test_string_conversions() {
let str1 = "000000000000000000000000";
let str2 = String::from("000000000000000000000000");
let id1 = ObjectId::try_from(str1).expect("Unable to convert from string");
let id2 = ObjectId::try_from(str2).expect("Unable to convert from string");
let id3: ObjectId = str1.try_into().unwrap();
assert_eq!(id1, id3);
assert_eq!(id2, id3);
}
}