Laying the ground work to add pages.

This commit is contained in:
2025-04-24 12:00:17 -04:00
parent cb69c4d55a
commit cb9bac9d8a
5 changed files with 103 additions and 17 deletions

View File

@ -1,12 +1,14 @@
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),
DateTime(DateTime<Utc>),
}
impl Field {
@ -23,6 +25,13 @@ impl Field {
_ => 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 {
@ -57,12 +66,19 @@ impl From<DateTime<Utc>> for Field {
}
}
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::Uuid(data) => write!(f, "{}", data),
Field::Static(data) => write!(f, "{}", data),
Field::DateTime(data) => write!(f, "{}", data),
Field::Static(data) => write!(f, "{}", data),
Field::Uuid(data) => write!(f, "{}", data),
_ => write!(f, ""),
}
}
}
@ -196,4 +212,34 @@ mod fields {
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),
}
}
}