Skip to content

Commit

Permalink
enhance code, add valid_date.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
hafihaf123 committed Jan 27, 2025
1 parent 707a209 commit f280156
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 48 deletions.
10 changes: 5 additions & 5 deletions rama-http/src/headers/x_robots_tag/custom_rule.rs
Original file line number Diff line number Diff line change
@@ -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<HeaderValueString>,
}

impl CustomRule {
pub fn new(key: &str) -> Result<Self, FromStrError> {
pub(super) fn new(key: &str) -> Result<Self, FromStrError> {
Ok(Self {
key: key.parse()?,
value: None,
})
}

pub fn with_value(key: &str, value: &str) -> Result<Self, FromStrError> {
pub(super) fn with_value(key: &str, value: &str) -> Result<Self, FromStrError> {
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 5 additions & 8 deletions rama-http/src/headers/x_robots_tag/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
44 changes: 23 additions & 21 deletions rama-http/src/headers/x_robots_tag/robots_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
pub(super) fn [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
self.[<$field>] = [<$field>];
self
}

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

#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct RobotsTag {
pub(super) struct RobotsTag {
bot_name: Option<HeaderValueString>,
all: bool,
no_index: bool,
Expand All @@ -58,7 +56,7 @@ pub struct RobotsTag {
max_video_preview: Option<u32>,
no_translate: bool,
no_image_index: bool,
unavailable_after: Option<chrono::DateTime<chrono::Utc>>, // "A date must be specified in a format such as RFC 822, RFC 850, or ISO 8601."
unavailable_after: Option<ValidDate>, // "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,
Expand All @@ -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<HeaderValueString>) -> Self {
pub(super) fn new_with_bot_name(bot_name: Option<HeaderValueString>) -> 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()
}

Expand All @@ -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<chrono::Utc>, 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);
}
35 changes: 22 additions & 13 deletions rama-http/src/headers/x_robots_tag/robots_tag_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,53 @@ 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.[<set_ $field>]([<$field>]);
self
}

pub fn [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
pub(super) fn [<set_ $field>](&mut self, [<$field>]: $type) -> &mut Self {
self.0.[<set_ $field>]([<$field>]);
self
}

pub(super) fn [<with_ $field>](mut self, [<$field>]: $type) -> Self {
self.0.[<set_ $field>]([<$field>]);
self
}
}
};
}

#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct RobotsTagBuilder<T = ()>(T);
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct RobotsTagBuilder<T = ()>(T);

impl RobotsTagBuilder<()> {
pub fn new() -> Self {
pub(super) fn new() -> Self {
RobotsTagBuilder(())
}

pub fn bot_name(self, bot_name: Option<HeaderValueString>) -> RobotsTagBuilder<RobotsTag> {
RobotsTagBuilder(RobotsTag::with_bot_name(bot_name))
pub(super) fn bot_name(
self,
bot_name: Option<HeaderValueString>,
) -> RobotsTagBuilder<RobotsTag> {
RobotsTagBuilder(RobotsTag::new_with_bot_name(bot_name))
}
}

impl RobotsTagBuilder<RobotsTag> {
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);
Expand All @@ -54,7 +63,7 @@ impl RobotsTagBuilder<RobotsTag> {
builder_field!(max_video_preview, u32);
builder_field!(no_translate, bool);
builder_field!(no_image_index, bool);
builder_field!(unavailable_after, chrono::DateTime<chrono::Utc>);
builder_field!(unavailable_after, ValidDate);
builder_field!(no_ai, bool);
builder_field!(no_image_ai, bool);
builder_field!(spc, bool);
Expand Down
46 changes: 46 additions & 0 deletions rama-http/src/headers/x_robots_tag/valid_date.rs
Original file line number Diff line number Diff line change
@@ -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<Utc>);

impl ValidDate {
pub(super) fn new(date: DateTime<Utc>) -> Result<Self, OpaqueError> {
Ok(Self(date))
}
}

impl Deref for ValidDate {
type Target = DateTime<Utc>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

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

impl TryFrom<DateTime<Utc>> for ValidDate {
type Error = OpaqueError;

fn try_from(value: DateTime<Utc>) -> Result<Self, Self::Error> {
ValidDate::new(value)
}
}

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

impl AsMut<DateTime<Utc>> for ValidDate {
fn as_mut(&mut self) -> &mut DateTime<Utc> {
&mut self.0
}
}

0 comments on commit f280156

Please sign in to comment.