morethantext/src/field.rs

246 lines
6.4 KiB
Rust

use chrono::prelude::*;
use crate::ErrorType;
use std::fmt;
use uuid::Uuid;
#[derive(Clone, Debug)]
pub enum Field {
DateTime(DateTime<Utc>),
ErrorType(ErrorType),
Static(String),
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()),
}
}
pub fn to_datetime(&self) -> Result<DateTime<Utc>, String> {
match self {
Field::DateTime(data) => Ok(data.clone()),
_ => Err("not a datetime".to_string()),
}
}
pub fn to_error_type(&self) -> Result<ErrorType, String> {
match self {
Field::ErrorType(data) => Ok(data.clone()),
_ => Err("not an error type".to_string()),
}
}
}
impl From<String> for Field {
fn from(value: String) -> Self {
match Uuid::try_from(value.as_str()) {
Ok(data) => return Field::Uuid(data),
Err(_) => {}
}
Field::Static(value)
}
}
impl From<&str> for Field {
fn from(value: &str) -> Self {
match Uuid::try_from(value) {
Ok(data) => return Field::Uuid(data),
Err(_) => {}
}
Field::Static(value.into())
}
}
impl From<Uuid> for Field {
fn from(value: Uuid) -> Self {
Field::Uuid(value)
}
}
impl From<DateTime<Utc>> for Field {
fn from(value: DateTime<Utc>) -> Self {
Field::DateTime(value)
}
}
impl From<ErrorType> for Field {
fn from(value: ErrorType) -> Self {
Field::ErrorType(value)
}
}
impl fmt::Display for Field {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Field::DateTime(data) => write!(f, "{}", data),
Field::Static(data) => write!(f, "{}", data),
Field::Uuid(data) => write!(f, "{}", data),
_ => write!(f, ""),
}
}
}
#[cfg(test)]
mod fields {
use super::*;
#[test]
fn string_to_field() {
let entries = ["test1".to_string(), "test2".to_string()];
for data in entries {
match data.clone().into() {
Field::Static(result) => assert_eq!(result, data),
_ => unreachable!("shouuld have been a static field"),
}
}
}
#[test]
fn str_to_field() {
let entries = ["test1", "test2"];
for data in entries {
match data.into() {
Field::Static(result) => assert_eq!(result, data),
_ => unreachable!("shouuld have been a static field"),
}
}
}
#[test]
fn uuid_to_field() {
let id = Uuid::new_v4();
match id.into() {
Field::Uuid(result) => assert_eq!(result, id),
_ => unreachable!("should have been a uuid field"),
}
}
#[test]
fn uuid_string_to_field() {
let id = Uuid::new_v4();
let id_string = id.to_string();
match id_string.into() {
Field::Uuid(result) => assert_eq!(result, id),
_ => unreachable!("should have been a uuid field"),
}
}
#[test]
fn uuid_str_to_field() {
let id = Uuid::new_v4();
let id_string = id.to_string();
let id_str = id_string.as_str();
match id_str.into() {
Field::Uuid(result) => assert_eq!(result, id),
_ => unreachable!("should have been a uuid field"),
}
}
#[test]
fn uuid_field_to_string() {
let id = Uuid::new_v4();
let result = id.to_string();
let input = Field::Uuid(id);
assert_eq!(input.to_string(), result);
}
#[test]
fn str_field_to_string() {
let result = "Something goes here";
let input: Field = result.into();
assert_eq!(input.to_string(), result);
}
#[test]
fn string_field_to_string() {
let result = "Another string".to_string();
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(_) => {}
}
}
#[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");
}
#[test]
fn from_error_to_field() {
let err = ErrorType::DocumentNotFound;
let field: Field = err.into();
match field {
Field::ErrorType(data) => match data {
ErrorType::DocumentNotFound => {},
//_ => unreachable!("got {:?}: should have been Document not found", data),
},
_ => unreachable!("should have been an error type"),
}
}
#[test]
fn from_field_to_error_type_error() {
let field: Field = Uuid::new_v4().into();
assert!(field.to_error_type().is_err(), "should generate an error");
}
#[test]
fn from_field_to_error_type() {
let err = ErrorType::DocumentNotFound;
let field: Field = err.into();
let result = field.to_error_type().unwrap();
match result {
ErrorType::DocumentNotFound => {},
//_ => unreachable!("got {:?}: should have been document not found", result),
}
}
}