Skip to content

Commit

Permalink
change visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hafihaf123 committed Feb 5, 2025
1 parent 6c1944b commit 83b118a
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 64 deletions.
4 changes: 2 additions & 2 deletions rama-http/src/headers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
19 changes: 19 additions & 0 deletions rama-http/src/headers/x_robots_tag_components/custom_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HeaderValueString>) {
(&self.key, &self.value)
}
}

impl From<HeaderValueString> 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),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions rama-http/src/headers/x_robots_tag_components/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
56 changes: 41 additions & 15 deletions rama-http/src/headers/x_robots_tag_components/robots_tag.rs
Original file line number Diff line number Diff line change
@@ -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 [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
pub fn [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
self.[<$field>] = [<$field>];
self
}

pub(super) fn [<with_ $field>](mut self, [<$field>]: $type) -> Self {
pub fn [<with_ $field>](mut self, [<$field>]: $type) -> Self {
self.[<$field>] = [<$field>];
self
}
Expand All @@ -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 [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
pub fn [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
self.[<$field>] = Some([<$field>]);
self
}

pub(super) fn [<with_ $field>](mut self, [<$field>]: $type) -> Self {
pub fn [<with_ $field>](mut self, [<$field>]: $type) -> Self {
self.[<$field>] = Some([<$field>]);
self
}
Expand All @@ -44,7 +43,7 @@ macro_rules! getter_setter {
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct RobotsTag {
pub struct RobotsTag {
bot_name: Option<HeaderValueString>,
all: bool,
no_index: bool,
Expand All @@ -58,7 +57,6 @@ pub(crate) struct RobotsTag {
no_translate: bool,
no_image_index: bool,
unavailable_after: Option<ValidDate>,
// custom rules
no_ai: bool,
no_image_ai: bool,
spc: bool,
Expand Down Expand Up @@ -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<Item = (&HeaderValueString, &Option<HeaderValueString>)> {
self.custom_rules.iter().map(|x| x.as_tuple())
}

pub fn builder() -> Builder {
Builder::new()
}

Expand All @@ -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);
Expand All @@ -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<Utc>> {
self.unavailable_after.as_deref()
}

pub fn set_unavailable_after(&mut self, unavailable_after: DateTime<Utc>) -> &mut Self {
self.unavailable_after = Some(unavailable_after.into());
self
}

pub fn with_unavailable_after(mut self, unavailable_after: DateTime<Utc>) -> Self {
self.unavailable_after = Some(unavailable_after.into());
self
}
}

impl Display for RobotsTag {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.[<set_ $field>]([<$field>]);
self
}

pub(in crate::headers::x_robots_tag_components) fn [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
pub fn [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
self.0.[<set_ $field>]([<$field>]);
self
}
Expand All @@ -25,46 +23,37 @@ 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<RobotsTag> {
pub fn [<$field>](self, [<$field>]: $type) -> Builder<RobotsTag> {
Builder(RobotsTag::new_with_bot_name(self.0.bot_name)).[<$field>]([<$field>])
}
}
};
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::headers::x_robots_tag_components) struct Builder<T = ()>(T);
pub struct Builder<T = ()>(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<HeaderValueString>,
) -> Builder<NoTag> {
pub fn bot_name(&self, bot_name: Option<HeaderValueString>) -> Builder<NoTag> {
Builder(NoTag { bot_name })
}
}

pub(in crate::headers::x_robots_tag_components) struct NoTag {
pub struct NoTag {
bot_name: Option<HeaderValueString>,
}

impl Builder<NoTag> {
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
}
Expand All @@ -80,31 +69,34 @@ impl Builder<NoTag> {
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<Utc>);
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<Builder<RobotsTag>, OpaqueError> {
pub fn add_field(self, s: &str) -> Result<Builder<RobotsTag>, OpaqueError> {
let mut builder = Builder(RobotsTag::new_with_bot_name(self.0.bot_name));
builder.add_field(s)?;
Ok(builder)
}
}

impl Builder<RobotsTag> {
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
}

Expand All @@ -120,15 +112,12 @@ impl Builder<RobotsTag> {
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<Utc>);
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)?)
Expand All @@ -137,7 +126,7 @@ impl Builder<RobotsTag> {
} 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: <date/time>") {
self.set_unavailable_after(value.parse()?)
self.set_unavailable_after(value.parse::<ValidDate>()?.into())
} else {
return Err(OpaqueError::from_std(Error::invalid()));
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -79,9 +79,7 @@ impl Parser<'_> {
Ok(None)
}

pub(in crate::headers) fn parse_value(
value: &HeaderValue,
) -> Result<Vec<RobotsTag>, OpaqueError> {
pub(crate) fn parse_value(value: &HeaderValue) -> Result<Vec<RobotsTag>, OpaqueError> {
Parser::new(value.to_str().map_err(OpaqueError::from_std)?).collect::<Result<Vec<_>, _>>()
}
}
6 changes: 6 additions & 0 deletions rama-http/src/headers/x_robots_tag_components/valid_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ impl From<ValidDate> for DateTime<Utc> {
}
}

impl From<DateTime<Utc>> for ValidDate {
fn from(value: DateTime<Utc>) -> Self {
Self::new(value)
}
}

impl AsRef<DateTime<Utc>> for ValidDate {
fn as_ref(&self) -> &DateTime<Utc> {
&self.0
Expand Down

0 comments on commit 83b118a

Please sign in to comment.