From 83b118a47319eba09a94f3648f5a6bcd2774b500 Mon Sep 17 00:00:00 2001 From: hafihaf123 Date: Wed, 5 Feb 2025 22:21:19 +0100 Subject: [PATCH] change visibility --- rama-http/src/headers/mod.rs | 4 +- .../x_robots_tag_components/custom_rule.rs | 19 ++++++ .../max_image_preview_setting.rs | 2 +- .../headers/x_robots_tag_components/mod.rs | 7 ++- .../x_robots_tag_components/robots_tag.rs | 56 ++++++++++++----- .../robots_tag_components/builder.rs | 63 ++++++++----------- .../robots_tag_components/mod.rs | 4 +- .../robots_tag_components/parser.rs | 8 +-- .../x_robots_tag_components/valid_date.rs | 6 ++ 9 files changed, 105 insertions(+), 64 deletions(-) diff --git a/rama-http/src/headers/mod.rs b/rama-http/src/headers/mod.rs index af7ea92f..3a82b06d 100644 --- a/rama-http/src/headers/mod.rs +++ b/rama-http/src/headers/mod.rs @@ -103,9 +103,9 @@ pub use ::rama_http_types::headers::HeaderExt; pub(crate) mod util; -mod x_robots_tag_components; +pub mod x_robots_tag_components; -pub mod x_robots_tag; +mod x_robots_tag; pub use x_robots_tag::XRobotsTag; pub use util::quality_value::{Quality, QualityValue}; diff --git a/rama-http/src/headers/x_robots_tag_components/custom_rule.rs b/rama-http/src/headers/x_robots_tag_components/custom_rule.rs index bb2ab466..78d67411 100644 --- a/rama-http/src/headers/x_robots_tag_components/custom_rule.rs +++ b/rama-http/src/headers/x_robots_tag_components/custom_rule.rs @@ -28,4 +28,23 @@ impl CustomRule { pub(super) fn value(&self) -> Option<&HeaderValueString> { self.value.as_ref() } + + pub(super) fn as_tuple(&self) -> (&HeaderValueString, &Option) { + (&self.key, &self.value) + } +} + +impl From for CustomRule { + fn from(key: HeaderValueString) -> Self { + Self { key, value: None } + } +} + +impl From<(HeaderValueString, HeaderValueString)> for CustomRule { + fn from(key_value: (HeaderValueString, HeaderValueString)) -> Self { + Self { + key: key_value.0, + value: Some(key_value.1), + } + } } diff --git a/rama-http/src/headers/x_robots_tag_components/max_image_preview_setting.rs b/rama-http/src/headers/x_robots_tag_components/max_image_preview_setting.rs index 7b05e69c..5d0fe378 100644 --- a/rama-http/src/headers/x_robots_tag_components/max_image_preview_setting.rs +++ b/rama-http/src/headers/x_robots_tag_components/max_image_preview_setting.rs @@ -4,7 +4,7 @@ use std::str::FromStr; use MaxImagePreviewSetting::*; #[derive(Clone, Debug, Eq, PartialEq)] -pub(super) enum MaxImagePreviewSetting { +pub enum MaxImagePreviewSetting { None, Standard, Large, diff --git a/rama-http/src/headers/x_robots_tag_components/mod.rs b/rama-http/src/headers/x_robots_tag_components/mod.rs index 97834688..df64d8ec 100644 --- a/rama-http/src/headers/x_robots_tag_components/mod.rs +++ b/rama-http/src/headers/x_robots_tag_components/mod.rs @@ -1,10 +1,13 @@ mod robots_tag; -pub(super) use robots_tag::RobotsTag; +pub use robots_tag::RobotsTag; mod max_image_preview_setting; +pub use max_image_preview_setting::MaxImagePreviewSetting; mod custom_rule; +use custom_rule::CustomRule; mod valid_date; +use valid_date::ValidDate; -pub(super) mod robots_tag_components; +pub mod robots_tag_components; diff --git a/rama-http/src/headers/x_robots_tag_components/robots_tag.rs b/rama-http/src/headers/x_robots_tag_components/robots_tag.rs index 6a5d5b58..71c62185 100644 --- a/rama-http/src/headers/x_robots_tag_components/robots_tag.rs +++ b/rama-http/src/headers/x_robots_tag_components/robots_tag.rs @@ -1,23 +1,22 @@ use crate::headers::util::value_string::HeaderValueString; -use crate::headers::x_robots_tag_components::custom_rule::CustomRule; -use crate::headers::x_robots_tag_components::max_image_preview_setting::MaxImagePreviewSetting; use crate::headers::x_robots_tag_components::robots_tag_components::Builder; -use crate::headers::x_robots_tag_components::valid_date::ValidDate; +use crate::headers::x_robots_tag_components::{CustomRule, MaxImagePreviewSetting, ValidDate}; +use chrono::{DateTime, Utc}; use std::fmt::{Display, Formatter}; macro_rules! getter_setter { ($field:ident, $type:ty) => { paste::paste! { - pub(super) fn [<$field>](&self) -> $type { + pub fn [<$field>](&self) -> $type { self.[<$field>] } - pub(super) fn [](&mut self, [<$field>]: $type) -> &mut Self { + pub fn [](&mut self, [<$field>]: $type) -> &mut Self { self.[<$field>] = [<$field>]; self } - pub(super) fn [](mut self, [<$field>]: $type) -> Self { + pub fn [](mut self, [<$field>]: $type) -> Self { self.[<$field>] = [<$field>]; self } @@ -26,16 +25,16 @@ macro_rules! getter_setter { ($field:ident, $type:ty, optional) => { paste::paste! { - pub(super) fn [<$field>](&self) -> Option<&$type> { + pub fn [<$field>](&self) -> Option<&$type> { self.[<$field>].as_ref() } - pub(super) fn [](&mut self, [<$field>]: $type) -> &mut Self { + pub fn [](&mut self, [<$field>]: $type) -> &mut Self { self.[<$field>] = Some([<$field>]); self } - pub(super) fn [](mut self, [<$field>]: $type) -> Self { + pub fn [](mut self, [<$field>]: $type) -> Self { self.[<$field>] = Some([<$field>]); self } @@ -44,7 +43,7 @@ macro_rules! getter_setter { } #[derive(Clone, Debug, Eq, PartialEq)] -pub(crate) struct RobotsTag { +pub struct RobotsTag { bot_name: Option, all: bool, no_index: bool, @@ -58,7 +57,6 @@ pub(crate) struct RobotsTag { no_translate: bool, no_image_index: bool, unavailable_after: Option, - // custom rules no_ai: bool, no_image_ai: bool, spc: bool, @@ -88,12 +86,27 @@ impl RobotsTag { } } - pub(super) fn add_custom_rule(&mut self, rule: CustomRule) -> &mut Self { - self.custom_rules.push(rule); + pub fn add_custom_rule_simple(&mut self, key: HeaderValueString) -> &mut Self { + self.custom_rules.push(key.into()); self } - pub(super) fn builder() -> Builder { + pub fn add_custom_rule_composite( + &mut self, + key: HeaderValueString, + value: HeaderValueString, + ) -> &mut Self { + self.custom_rules.push((key, value).into()); + self + } + + pub fn custom_rules( + &self, + ) -> impl Iterator)> { + self.custom_rules.iter().map(|x| x.as_tuple()) + } + + pub fn builder() -> Builder { Builder::new() } @@ -109,7 +122,6 @@ impl RobotsTag { getter_setter!(max_video_preview, u32, optional); getter_setter!(no_translate, bool); getter_setter!(no_image_index, bool); - getter_setter!(unavailable_after, ValidDate, optional); getter_setter!(no_ai, bool); getter_setter!(no_image_ai, bool); getter_setter!(spc, bool); @@ -131,6 +143,20 @@ impl RobotsTag { || field_name.eq_ignore_ascii_case("noimageai") || field_name.eq_ignore_ascii_case("spc") } + + pub fn unavailable_after(&self) -> Option<&DateTime> { + self.unavailable_after.as_deref() + } + + pub fn set_unavailable_after(&mut self, unavailable_after: DateTime) -> &mut Self { + self.unavailable_after = Some(unavailable_after.into()); + self + } + + pub fn with_unavailable_after(mut self, unavailable_after: DateTime) -> Self { + self.unavailable_after = Some(unavailable_after.into()); + self + } } impl Display for RobotsTag { diff --git a/rama-http/src/headers/x_robots_tag_components/robots_tag_components/builder.rs b/rama-http/src/headers/x_robots_tag_components/robots_tag_components/builder.rs index 495778b4..0f25a117 100644 --- a/rama-http/src/headers/x_robots_tag_components/robots_tag_components/builder.rs +++ b/rama-http/src/headers/x_robots_tag_components/robots_tag_components/builder.rs @@ -1,20 +1,18 @@ use crate::headers::util::value_string::HeaderValueString; -use crate::headers::x_robots_tag_components::custom_rule::CustomRule; -use crate::headers::x_robots_tag_components::max_image_preview_setting::MaxImagePreviewSetting; -use crate::headers::x_robots_tag_components::robots_tag::RobotsTag; -use crate::headers::x_robots_tag_components::valid_date::ValidDate; +use crate::headers::x_robots_tag_components::{MaxImagePreviewSetting, RobotsTag, ValidDate}; +use chrono::{DateTime, Utc}; use headers::Error; use rama_core::error::OpaqueError; macro_rules! robots_tag_builder_field { ($field:ident, $type:ty) => { paste::paste! { - pub(in crate::headers::x_robots_tag_components) fn [<$field>](mut self, [<$field>]: $type) -> Self { + pub fn [<$field>](mut self, [<$field>]: $type) -> Self { self.0.[]([<$field>]); self } - pub(in crate::headers::x_robots_tag_components) fn [](&mut self, [<$field>]: $type) -> &mut Self { + pub fn [](&mut self, [<$field>]: $type) -> &mut Self { self.0.[]([<$field>]); self } @@ -25,7 +23,7 @@ macro_rules! robots_tag_builder_field { macro_rules! no_tag_builder_field { ($field:ident, $type:ty) => { paste::paste! { - pub(in crate::headers::x_robots_tag_components) fn [<$field>](self, [<$field>]: $type) -> Builder { + pub fn [<$field>](self, [<$field>]: $type) -> Builder { Builder(RobotsTag::new_with_bot_name(self.0.bot_name)).[<$field>]([<$field>]) } } @@ -33,38 +31,29 @@ macro_rules! no_tag_builder_field { } #[derive(Clone, Debug, Eq, PartialEq)] -pub(in crate::headers::x_robots_tag_components) struct Builder(T); +pub struct Builder(T); impl Builder<()> { - pub(in crate::headers::x_robots_tag_components) fn new() -> Self { + pub fn new() -> Self { Builder(()) } - pub(in crate::headers::x_robots_tag_components) fn bot_name( - &self, - bot_name: Option, - ) -> Builder { + pub fn bot_name(&self, bot_name: Option) -> Builder { Builder(NoTag { bot_name }) } } -pub(in crate::headers::x_robots_tag_components) struct NoTag { +pub struct NoTag { bot_name: Option, } impl Builder { - pub(in crate::headers::x_robots_tag_components) fn bot_name( - mut self, - bot_name: HeaderValueString, - ) -> Self { + pub fn bot_name(mut self, bot_name: HeaderValueString) -> Self { self.0.bot_name = Some(bot_name); self } - pub(in crate::headers::x_robots_tag_components) fn set_bot_name( - &mut self, - bot_name: HeaderValueString, - ) -> &mut Self { + pub fn set_bot_name(&mut self, bot_name: HeaderValueString) -> &mut Self { self.0.bot_name = Some(bot_name); self } @@ -80,15 +69,12 @@ impl Builder { no_tag_builder_field!(max_video_preview, u32); no_tag_builder_field!(no_translate, bool); no_tag_builder_field!(no_image_index, bool); - no_tag_builder_field!(unavailable_after, ValidDate); + no_tag_builder_field!(unavailable_after, DateTime); no_tag_builder_field!(no_ai, bool); no_tag_builder_field!(no_image_ai, bool); no_tag_builder_field!(spc, bool); - pub(in crate::headers::x_robots_tag_components) fn add_field( - self, - s: &str, - ) -> Result, OpaqueError> { + pub fn add_field(self, s: &str) -> Result, OpaqueError> { let mut builder = Builder(RobotsTag::new_with_bot_name(self.0.bot_name)); builder.add_field(s)?; Ok(builder) @@ -96,15 +82,21 @@ impl Builder { } impl Builder { - pub(in crate::headers::x_robots_tag_components) fn build(self) -> RobotsTag { + pub fn build(self) -> RobotsTag { self.0 } - pub(in crate::headers::x_robots_tag_components) fn add_custom_rule( + pub fn add_custom_rule_simple(&mut self, key: HeaderValueString) -> &mut Self { + self.0.add_custom_rule_simple(key); + self + } + + pub fn add_custom_rule_composite( &mut self, - rule: CustomRule, + key: HeaderValueString, + value: HeaderValueString, ) -> &mut Self { - self.0.add_custom_rule(rule); + self.0.add_custom_rule_composite(key, value); self } @@ -120,15 +112,12 @@ impl Builder { robots_tag_builder_field!(max_video_preview, u32); robots_tag_builder_field!(no_translate, bool); robots_tag_builder_field!(no_image_index, bool); - robots_tag_builder_field!(unavailable_after, ValidDate); + robots_tag_builder_field!(unavailable_after, DateTime); robots_tag_builder_field!(no_ai, bool); robots_tag_builder_field!(no_image_ai, bool); robots_tag_builder_field!(spc, bool); - pub(in crate::headers::x_robots_tag_components) fn add_field( - &mut self, - s: &str, - ) -> Result<&mut Self, OpaqueError> { + pub fn add_field(&mut self, s: &str) -> Result<&mut Self, OpaqueError> { if let Some((key, value)) = s.trim().split_once(':') { Ok(if key.eq_ignore_ascii_case("max-snippet") { self.set_max_snippet(value.parse().map_err(OpaqueError::from_std)?) @@ -137,7 +126,7 @@ impl Builder { } else if key.eq_ignore_ascii_case("max-video-preview") { self.set_max_video_preview(value.parse().map_err(OpaqueError::from_std)?) } else if key.eq_ignore_ascii_case("unavailable_after: ") { - self.set_unavailable_after(value.parse()?) + self.set_unavailable_after(value.parse::()?.into()) } else { return Err(OpaqueError::from_std(Error::invalid())); }) diff --git a/rama-http/src/headers/x_robots_tag_components/robots_tag_components/mod.rs b/rama-http/src/headers/x_robots_tag_components/robots_tag_components/mod.rs index c43d54b2..797103e0 100644 --- a/rama-http/src/headers/x_robots_tag_components/robots_tag_components/mod.rs +++ b/rama-http/src/headers/x_robots_tag_components/robots_tag_components/mod.rs @@ -1,5 +1,5 @@ mod builder; -pub(super) use builder::Builder; +pub use builder::Builder; mod parser; -pub(in crate::headers) use parser::Parser; +pub(crate) use parser::Parser; diff --git a/rama-http/src/headers/x_robots_tag_components/robots_tag_components/parser.rs b/rama-http/src/headers/x_robots_tag_components/robots_tag_components/parser.rs index 7918706d..36cd8ef4 100644 --- a/rama-http/src/headers/x_robots_tag_components/robots_tag_components/parser.rs +++ b/rama-http/src/headers/x_robots_tag_components/robots_tag_components/parser.rs @@ -4,12 +4,12 @@ use http::HeaderValue; use rama_core::error::OpaqueError; use std::str::FromStr; -pub(in crate::headers) struct Parser<'a> { +pub(crate) struct Parser<'a> { remaining: Option<&'a str>, } impl<'a> Parser<'a> { - pub(in crate::headers) fn new(remaining: &'a str) -> Self { + pub(crate) fn new(remaining: &'a str) -> Self { let remaining = match remaining.trim() { "" => None, text => Some(text), @@ -79,9 +79,7 @@ impl Parser<'_> { Ok(None) } - pub(in crate::headers) fn parse_value( - value: &HeaderValue, - ) -> Result, OpaqueError> { + pub(crate) fn parse_value(value: &HeaderValue) -> Result, OpaqueError> { Parser::new(value.to_str().map_err(OpaqueError::from_std)?).collect::, _>>() } } diff --git a/rama-http/src/headers/x_robots_tag_components/valid_date.rs b/rama-http/src/headers/x_robots_tag_components/valid_date.rs index 764379e6..0630045c 100644 --- a/rama-http/src/headers/x_robots_tag_components/valid_date.rs +++ b/rama-http/src/headers/x_robots_tag_components/valid_date.rs @@ -27,6 +27,12 @@ impl From for DateTime { } } +impl From> for ValidDate { + fn from(value: DateTime) -> Self { + Self::new(value) + } +} + impl AsRef> for ValidDate { fn as_ref(&self) -> &DateTime { &self.0