Skip to content

Commit

Permalink
Implemented TryFrom<String> for ObjectId
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmcc committed Oct 11, 2022
1 parent 5c0755b commit 4630a51
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/oid.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! ObjectId

use std::{
convert::TryInto,
convert::{TryFrom, TryInto},
error,
fmt,
result,
str::FromStr,
sync::atomic::{AtomicUsize, Ordering},
time::SystemTime,
};
use std::convert::TryFrom;

use hex::{self, FromHexError};
use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -230,6 +229,13 @@ impl TryFrom<&str> for ObjectId {
}
}

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 @@ -291,9 +297,12 @@ fn test_counter_overflow_usize_max() {

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

#[test]
fn test_display() {
Expand Down Expand Up @@ -346,9 +355,12 @@ mod test {

#[test]
fn test_string_conversions() {
let str = "000000000000000000000000";
let id1 = ObjectId::try_from(str).expect("Unable to convert from string");
let id2: ObjectId = str.try_into().unwrap();
assert_eq!(id1, id2);
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);
}
}

0 comments on commit 4630a51

Please sign in to comment.