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
parent e10412e438
commit f2ece7292b
4 changed files with 63 additions and 0 deletions

View file

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

View file

@ -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 {}

View file

@ -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(),
}),
}
}
}

View file

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