Completed the trip to client and back.
This commit is contained in:
29
src/field.rs
29
src/field.rs
@ -8,6 +8,15 @@ pub enum Field {
|
||||
Uuid(Uuid),
|
||||
}
|
||||
|
||||
impl Field {
|
||||
pub fn to_uuid(&self) -> Result<Uuid, String> {
|
||||
match self {
|
||||
Field::Uuid(data) => Ok(data.clone()),
|
||||
_ => Err("not a uuid".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Field {
|
||||
fn from(value: String) -> Self {
|
||||
match Uuid::try_from(value.as_str()) {
|
||||
@ -121,4 +130,24 @@ mod fields {
|
||||
let input: Field = result.clone().into();
|
||||
assert_eq!(input.to_string(), result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn field_to_uuid() {
|
||||
let id = Uuid::new_v4();
|
||||
let field: Field = id.into();
|
||||
match field.to_uuid() {
|
||||
Ok(result) => assert_eq!(result, id),
|
||||
Err(_) => unreachable!("did not convert to uuid"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_uuid_field_to_uuid() {
|
||||
let text = "Not a uuid.";
|
||||
let field: Field = text.into();
|
||||
match field.to_uuid() {
|
||||
Ok(_) => unreachable!("should return an error"),
|
||||
Err(_) => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user