feat: create a task model

This commit is contained in:
Matouš Volf
2024-08-22 22:08:47 +02:00
parent 73fe2bab8b
commit 12ea8b5de2
8 changed files with 211 additions and 0 deletions

View File

@ -1,3 +1,4 @@
pub(crate) mod error;
pub(crate) mod error_vec;
pub(crate) mod project_create_error;
pub(crate) mod task_create_error;

View File

@ -0,0 +1,52 @@
use crate::errors::error::Error;
use crate::errors::error_vec::ErrorVec;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::str::FromStr;
use validator::{ValidationErrors, ValidationErrorsKind};
#[derive(Serialize, Deserialize, Debug)]
pub enum TaskCreateError {
TitleLengthInvalid,
ProjectNotFound,
Error(Error),
}
impl From<ValidationErrors> for ErrorVec<TaskCreateError> {
fn from(e: ValidationErrors) -> Self {
e.errors()
.iter()
.flat_map(|(&field, error_kind)| match field {
"title" => match error_kind {
ValidationErrorsKind::Field(validation_errors) => validation_errors
.iter()
.map(|validation_error| validation_error.code.as_ref())
.map(|code| match code {
"title_length" => TaskCreateError::TitleLengthInvalid,
_ => panic!("Unexpected validation error code: `{code}`."),
})
.collect::<Vec<TaskCreateError>>(),
_ => panic!("Unexpected validation error kind."),
},
_ => panic!("Unexpected validation field name: `{field}`."),
})
.collect::<Vec<TaskCreateError>>()
.into()
}
}
// has to be implemented for Dioxus server functions
impl Display for TaskCreateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
// has to be implemented for Dioxus server functions
impl FromStr for TaskCreateError {
type Err = ();
fn from_str(_: &str) -> Result<Self, Self::Err> {
Ok(TaskCreateError::TitleLengthInvalid)
}
}