From f2ece7292b00ec6af0567f8bf978d09157380285 Mon Sep 17 00:00:00 2001 From: Pau Figueras Date: Sun, 12 May 2024 04:02:30 +0200 Subject: [PATCH] feat(pages domain): add basic logic, errors to pages (FE Links) domain --- src/contexts/mod.rs | 1 + src/contexts/pages/domain/errors/mod.rs | 14 ++++++++ src/contexts/pages/domain/mod.rs | 47 +++++++++++++++++++++++++ src/contexts/pages/mod.rs | 1 + 4 files changed, 63 insertions(+) create mode 100644 src/contexts/pages/domain/errors/mod.rs create mode 100644 src/contexts/pages/domain/mod.rs create mode 100644 src/contexts/pages/mod.rs diff --git a/src/contexts/mod.rs b/src/contexts/mod.rs index eec3c89..b60cbae 100644 --- a/src/contexts/mod.rs +++ b/src/contexts/mod.rs @@ -1 +1,2 @@ +pub mod pages; pub mod shared; diff --git a/src/contexts/pages/domain/errors/mod.rs b/src/contexts/pages/domain/errors/mod.rs new file mode 100644 index 0000000..6974796 --- /dev/null +++ b/src/contexts/pages/domain/errors/mod.rs @@ -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 {} diff --git a/src/contexts/pages/domain/mod.rs b/src/contexts/pages/domain/mod.rs new file mode 100644 index 0000000..16844a0 --- /dev/null +++ b/src/contexts/pages/domain/mod.rs @@ -0,0 +1,47 @@ +use self::errors::RouteError; + +pub mod errors; + +pub(crate) struct RouteValueObject { + link: String, +} + +impl RouteValueObject { + fn ensure_route_valid(value: T) -> Result { + // TODO: remove nested if clause + let starting_char: Option = 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(value: T) -> Result { + 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(name: T, route: T) -> Result { + let route_value: Result = RouteValueObject::new(route); + match route_value { + Err(error) => Err(error), + Ok(_) => Ok(Page { + name: name.to_string(), + route: route_value.unwrap(), + }), + } + } +} diff --git a/src/contexts/pages/mod.rs b/src/contexts/pages/mod.rs new file mode 100644 index 0000000..d7abca1 --- /dev/null +++ b/src/contexts/pages/mod.rs @@ -0,0 +1 @@ +pub mod domain;