feat(pages domain): add basic logic, errors to pages (FE Links) domain
This commit is contained in:
pare
e10412e438
commit
f2ece7292b
S'han modificat 4 arxius amb 63 adicions i 0 eliminacions
|
@ -1 +1,2 @@
|
||||||
|
pub mod pages;
|
||||||
pub mod shared;
|
pub mod shared;
|
||||||
|
|
14
src/contexts/pages/domain/errors/mod.rs
Normal file
14
src/contexts/pages/domain/errors/mod.rs
Normal 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 {}
|
47
src/contexts/pages/domain/mod.rs
Normal file
47
src/contexts/pages/domain/mod.rs
Normal 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(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
src/contexts/pages/mod.rs
Normal file
1
src/contexts/pages/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod domain;
|
Loading…
Referencia en una nova incidència