feat(pages domain): add basic logic, errors to pages (FE Links) domain

This commit is contained in:
Pau Figueras 2024-05-12 04:02:30 +02:00
pare e10412e438
commit f2ece7292b
S'han modificat 4 arxius amb 63 adicions i 0 eliminacions

Veure arxiu

@ -1 +1,2 @@
pub mod pages;
pub mod shared;

Veure arxiu

@ -0,0 +1,14 @@
use std::fmt::Display;
#[derive(Debug)]
pub enum RouteError {
RouteNotStartsWithSlash(String),
}
impl Display for RouteError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Ooops! {}", self)
}
}
impl std::error::Error for RouteError {}

Veure arxiu

@ -0,0 +1,47 @@
use self::errors::RouteError;
pub mod errors;
pub(crate) struct RouteValueObject {
link: String,
}
impl RouteValueObject {
fn ensure_route_valid<T: ToString>(value: T) -> Result<bool, RouteError> {
// TODO: remove nested if clause
let starting_char: Option<char> = value.to_string().chars().next();
if let Some(slash) = starting_char {
if slash != '/' {
return Err(RouteError::RouteNotStartsWithSlash(format!(
"Route should start with '/', but starts with {}",
slash
)));
}
}
Ok(true)
}
fn new<T: ToString>(value: T) -> Result<Self, RouteError> {
let bind_value: String = value.to_string();
Self::ensure_route_valid(&bind_value)?;
Ok(RouteValueObject { link: bind_value })
}
}
pub struct Page {
name: String,
route: RouteValueObject,
}
impl Page {
fn new<T: ToString>(name: T, route: T) -> Result<Self, impl std::error::Error> {
let route_value: Result<RouteValueObject, _> = RouteValueObject::new(route);
match route_value {
Err(error) => Err(error),
Ok(_) => Ok(Page {
name: name.to_string(),
route: route_value.unwrap(),
}),
}
}
}

Veure arxiu

@ -0,0 +1 @@
pub mod domain;