Added datetime field.
This commit is contained in:
48
src/field.rs
48
src/field.rs
@ -1,3 +1,4 @@
|
||||
use chrono::prelude::*;
|
||||
use std::fmt;
|
||||
use uuid::Uuid;
|
||||
|
||||
@ -5,6 +6,7 @@ use uuid::Uuid;
|
||||
pub enum Field {
|
||||
Static(String),
|
||||
Uuid(Uuid),
|
||||
DateTime(DateTime<Utc>),
|
||||
}
|
||||
|
||||
impl Field {
|
||||
@ -14,6 +16,13 @@ impl Field {
|
||||
_ => Err("not a uuid".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_datetime(&self) -> Result<DateTime<Utc>, String> {
|
||||
match self {
|
||||
Field::DateTime(data) => Ok(data.clone()),
|
||||
_ => Err("not a datetime".to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Field {
|
||||
@ -42,11 +51,18 @@ impl From<Uuid> for Field {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<DateTime<Utc>> for Field {
|
||||
fn from(value: DateTime<Utc>) -> Self {
|
||||
Field::DateTime(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Field {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Field::Uuid(data) => write!(f, "{}", data),
|
||||
Field::Static(data) => write!(f, "{}", data),
|
||||
Field::DateTime(data) => write!(f, "{}", data),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -148,4 +164,36 @@ mod fields {
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn use_date_to_field() {
|
||||
let expected = Utc::now();
|
||||
let field: Field = expected.into();
|
||||
match field {
|
||||
Field::DateTime(result) => assert_eq!(result, expected),
|
||||
_ => unreachable!("should have been date time"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_datetime_field_to_string() {
|
||||
let now = Utc::now();
|
||||
let expected = now.to_string();
|
||||
let field: Field = now.into();
|
||||
assert_eq!(field.to_string(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_field_to_datetime() {
|
||||
let now = Utc::now();
|
||||
let field: Field = now.into();
|
||||
assert_eq!(field.to_datetime().unwrap(), now);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_static_field_to_datatime() {
|
||||
let txt = "Not gonna work.";
|
||||
let field: Field = txt.into();
|
||||
assert!(field.to_datetime().is_err(), "should not return a value");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user