diff --git a/rama-http/src/headers/x_robots_tag/custom_rule.rs b/rama-http/src/headers/x_robots_tag/custom_rule.rs index aeb86a27..bb2ab466 100644 --- a/rama-http/src/headers/x_robots_tag/custom_rule.rs +++ b/rama-http/src/headers/x_robots_tag/custom_rule.rs @@ -1,31 +1,31 @@ use crate::headers::util::value_string::{FromStrError, HeaderValueString}; #[derive(Clone, Debug, Eq, PartialEq)] -pub struct CustomRule { +pub(super) struct CustomRule { key: HeaderValueString, value: Option, } impl CustomRule { - pub fn new(key: &str) -> Result { + pub(super) fn new(key: &str) -> Result { Ok(Self { key: key.parse()?, value: None, }) } - pub fn with_value(key: &str, value: &str) -> Result { + pub(super) fn with_value(key: &str, value: &str) -> Result { Ok(Self { key: key.parse()?, value: Some(value.parse()?), }) } - pub fn key(&self) -> &HeaderValueString { + pub(super) fn key(&self) -> &HeaderValueString { &self.key } - pub fn value(&self) -> Option<&HeaderValueString> { + pub(super) fn value(&self) -> Option<&HeaderValueString> { self.value.as_ref() } } diff --git a/rama-http/src/headers/x_robots_tag/max_image_preview_setting.rs b/rama-http/src/headers/x_robots_tag/max_image_preview_setting.rs index c5c5f2df..628c0233 100644 --- a/rama-http/src/headers/x_robots_tag/max_image_preview_setting.rs +++ b/rama-http/src/headers/x_robots_tag/max_image_preview_setting.rs @@ -3,7 +3,7 @@ use std::fmt::Formatter; use std::str::FromStr; #[derive(Clone, Debug, Eq, PartialEq)] -pub enum MaxImagePreviewSetting { +pub(super) enum MaxImagePreviewSetting { None, Standard, Large, diff --git a/rama-http/src/headers/x_robots_tag/mod.rs b/rama-http/src/headers/x_robots_tag/mod.rs index 8d5de836..a2a205e7 100644 --- a/rama-http/src/headers/x_robots_tag/mod.rs +++ b/rama-http/src/headers/x_robots_tag/mod.rs @@ -1,11 +1,8 @@ -pub mod robots_tag; -pub use robots_tag::RobotsTag; +mod robots_tag; -pub mod robots_tag_builder; -pub use robots_tag_builder::RobotsTagBuilder; +mod robots_tag_builder; -pub mod max_image_preview_setting; -pub use max_image_preview_setting::MaxImagePreviewSetting; +mod max_image_preview_setting; -pub mod custom_rule; -pub use custom_rule::CustomRule; +mod custom_rule; +mod valid_date; diff --git a/rama-http/src/headers/x_robots_tag/robots_tag.rs b/rama-http/src/headers/x_robots_tag/robots_tag.rs index 2eccd50b..02077166 100644 --- a/rama-http/src/headers/x_robots_tag/robots_tag.rs +++ b/rama-http/src/headers/x_robots_tag/robots_tag.rs @@ -2,15 +2,21 @@ use crate::headers::util::value_string::HeaderValueString; use crate::headers::x_robots_tag::custom_rule::CustomRule; use crate::headers::x_robots_tag::max_image_preview_setting::MaxImagePreviewSetting; use crate::headers::x_robots_tag::robots_tag_builder::RobotsTagBuilder; +use crate::headers::x_robots_tag::valid_date::ValidDate; macro_rules! getter_setter { ($field:ident, $type:ty) => { paste::paste! { - pub fn [<$field>](&self) -> $type { + pub(super) fn [<$field>](&self) -> $type { self.[<$field>] } - pub fn [](&mut self, [<$field>]: $type) -> &mut Self { + pub(super) fn [](&mut self, [<$field>]: $type) -> &mut Self { + self.[<$field>] = [<$field>]; + self + } + + pub(super) fn [](mut self, [<$field>]: $type) -> Self { self.[<$field>] = [<$field>]; self } @@ -19,25 +25,17 @@ macro_rules! getter_setter { ($field:ident, $type:ty, optional) => { paste::paste! { - pub fn [<$field>](&self) -> Option<&$type> { + pub(super) fn [<$field>](&self) -> Option<&$type> { self.[<$field>].as_ref() } - pub fn [](&mut self, [<$field>]: $type) -> &mut Self { + pub(super) fn [](&mut self, [<$field>]: $type) -> &mut Self { self.[<$field>] = Some([<$field>]); self } - } - }; - - ($field:ident, $type:ty, vec) => { - paste::paste! { - pub fn [<$field>](&self) -> &Vec<$type> { - &self.[<$field>] - } - pub fn [](&mut self, [<$field>]: $type) -> &mut Self { - self.[<$field>].push([<$field>]); + pub(super) fn [](mut self, [<$field>]: $type) -> Self { + self.[<$field>] = Some([<$field>]); self } } @@ -45,7 +43,7 @@ macro_rules! getter_setter { } #[derive(Clone, Debug, Eq, PartialEq, Default)] -pub struct RobotsTag { +pub(super) struct RobotsTag { bot_name: Option, all: bool, no_index: bool, @@ -58,7 +56,7 @@ pub struct RobotsTag { max_video_preview: Option, no_translate: bool, no_image_index: bool, - unavailable_after: Option>, // "A date must be specified in a format such as RFC 822, RFC 850, or ISO 8601." + unavailable_after: Option, // "A date must be specified in a format such as RFC 822, RFC 850, or ISO 8601." // custom rules no_ai: bool, no_image_ai: bool, @@ -67,18 +65,23 @@ pub struct RobotsTag { } impl RobotsTag { - pub fn new() -> Self { + pub(super) fn new() -> Self { Default::default() } - pub fn with_bot_name(bot_name: Option) -> Self { + pub(super) fn new_with_bot_name(bot_name: Option) -> Self { Self { bot_name, ..Default::default() } } - pub fn builder() -> RobotsTagBuilder { + pub(super) fn add_custom_rule(&mut self, rule: CustomRule) -> &mut Self { + self.custom_rules.push(rule); + self + } + + pub(super) fn builder() -> RobotsTagBuilder { RobotsTagBuilder::new() } @@ -94,9 +97,8 @@ impl RobotsTag { getter_setter!(max_video_preview, u32, optional); getter_setter!(no_translate, bool); getter_setter!(no_image_index, bool); - getter_setter!(unavailable_after, chrono::DateTime, optional); + getter_setter!(unavailable_after, ValidDate, optional); getter_setter!(no_ai, bool); getter_setter!(no_image_ai, bool); getter_setter!(spc, bool); - getter_setter!(custom_rules, CustomRule, vec); } diff --git a/rama-http/src/headers/x_robots_tag/robots_tag_builder.rs b/rama-http/src/headers/x_robots_tag/robots_tag_builder.rs index 00cb9b55..ef10a555 100644 --- a/rama-http/src/headers/x_robots_tag/robots_tag_builder.rs +++ b/rama-http/src/headers/x_robots_tag/robots_tag_builder.rs @@ -2,16 +2,22 @@ use crate::headers::util::value_string::HeaderValueString; use crate::headers::x_robots_tag::custom_rule::CustomRule; use crate::headers::x_robots_tag::max_image_preview_setting::MaxImagePreviewSetting; use crate::headers::x_robots_tag::robots_tag::RobotsTag; +use crate::headers::x_robots_tag::valid_date::ValidDate; macro_rules! builder_field { ($field:ident, $type:ty) => { paste::paste! { - pub fn [<$field>](mut self, [<$field>]: $type) -> Self { + pub(super) fn [<$field>](mut self, [<$field>]: $type) -> Self { self.0.[]([<$field>]); self } - pub fn [](&mut self, [<$field>]: $type) -> &mut Self { + pub(super) fn [](&mut self, [<$field>]: $type) -> &mut Self { + self.0.[]([<$field>]); + self + } + + pub(super) fn [](mut self, [<$field>]: $type) -> Self { self.0.[]([<$field>]); self } @@ -19,27 +25,30 @@ macro_rules! builder_field { }; } -#[derive(Clone, Debug, Eq, PartialEq, Default)] -pub struct RobotsTagBuilder(T); +#[derive(Clone, Debug, Eq, PartialEq)] +pub(super) struct RobotsTagBuilder(T); impl RobotsTagBuilder<()> { - pub fn new() -> Self { + pub(super) fn new() -> Self { RobotsTagBuilder(()) } - pub fn bot_name(self, bot_name: Option) -> RobotsTagBuilder { - RobotsTagBuilder(RobotsTag::with_bot_name(bot_name)) + pub(super) fn bot_name( + self, + bot_name: Option, + ) -> RobotsTagBuilder { + RobotsTagBuilder(RobotsTag::new_with_bot_name(bot_name)) } } impl RobotsTagBuilder { - pub fn add_custom_rule(&mut self, rule: CustomRule) -> &mut Self { - self.0.add_custom_rules(rule); - self + pub(super) fn build(self) -> RobotsTag { + self.0 } - pub fn build(self) -> RobotsTag { - self.0 + pub(super) fn add_custom_rule(&mut self, rule: CustomRule) -> &mut Self { + self.0.add_custom_rule(rule); + self } builder_field!(bot_name, HeaderValueString); @@ -54,7 +63,7 @@ impl RobotsTagBuilder { builder_field!(max_video_preview, u32); builder_field!(no_translate, bool); builder_field!(no_image_index, bool); - builder_field!(unavailable_after, chrono::DateTime); + builder_field!(unavailable_after, ValidDate); builder_field!(no_ai, bool); builder_field!(no_image_ai, bool); builder_field!(spc, bool); diff --git a/rama-http/src/headers/x_robots_tag/valid_date.rs b/rama-http/src/headers/x_robots_tag/valid_date.rs new file mode 100644 index 00000000..929c56e8 --- /dev/null +++ b/rama-http/src/headers/x_robots_tag/valid_date.rs @@ -0,0 +1,46 @@ +use chrono::{DateTime, Utc}; +use rama_core::error::OpaqueError; +use std::ops::Deref; + +#[derive(Clone, Debug, Eq, PartialEq)] +pub(super) struct ValidDate(DateTime); + +impl ValidDate { + pub(super) fn new(date: DateTime) -> Result { + Ok(Self(date)) + } +} + +impl Deref for ValidDate { + type Target = DateTime; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl From for DateTime { + fn from(value: ValidDate) -> Self { + value.0 + } +} + +impl TryFrom> for ValidDate { + type Error = OpaqueError; + + fn try_from(value: DateTime) -> Result { + ValidDate::new(value) + } +} + +impl AsRef> for ValidDate { + fn as_ref(&self) -> &DateTime { + &self.0 + } +} + +impl AsMut> for ValidDate { + fn as_mut(&mut self) -> &mut DateTime { + &mut self.0 + } +}