Skip to content

Commit

Permalink
enhance code
Browse files Browse the repository at this point in the history
  • Loading branch information
hafihaf123 committed Jan 31, 2025
1 parent 5933ec2 commit f10e6df
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ rama-tls = { version = "0.2.0-alpha.7", path = "rama-tls", optional = true }
rama-ua = { version = "0.2.0-alpha.7", path = "rama-ua", optional = true }
rama-utils = { version = "0.2.0-alpha.7", path = "rama-utils" }
serde_html_form = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
tokio = { workspace = true, features = ["macros", "io-std"], optional = true }
tracing = { workspace = true, optional = true }

Expand Down
30 changes: 15 additions & 15 deletions rama-http/src/headers/x_robots_tag.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
use crate::headers::x_robots_tag_components::RobotsTag;
use crate::headers::Error;
use headers::Header;
use http::{HeaderName, HeaderValue};
use crate::headers::Error;
use crate::headers::x_robots_tag_components::RobotsTag;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct XRobotsTag {
elements: Vec<RobotsTag>,
}

impl Header for XRobotsTag {
fn name() -> &'static HeaderName {
&crate::header::X_ROBOTS_TAG
}
fn name() -> &'static HeaderName {
&crate::header::X_ROBOTS_TAG
}

fn decode<'i, I>(values: &mut I) -> Result<Self, Error>
where
Self: Sized,
I: Iterator<Item=&'i HeaderValue>
{
todo!()
}
fn decode<'i, I>(values: &mut I) -> Result<Self, Error>
where
Self: Sized,
I: Iterator<Item = &'i HeaderValue>,
{
todo!()
}

fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
todo!()
}
fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
todo!()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ impl FromStr for MaxImagePreviewSetting {
type Err = OpaqueError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().trim() {
"none" => Ok(MaxImagePreviewSetting::None),
"standard" => Ok(MaxImagePreviewSetting::Standard),
"large" => Ok(MaxImagePreviewSetting::Large),
_ => Err(OpaqueError::from_display(
if s.eq_ignore_ascii_case("none") {
Ok(MaxImagePreviewSetting::None)
} else if s.eq_ignore_ascii_case("standard") {
Ok(MaxImagePreviewSetting::Standard)
} else if s.eq_ignore_ascii_case("large") {
Ok(MaxImagePreviewSetting::Large)
} else {
Err(OpaqueError::from_display(
"failed to parse MaxImagePreviewSetting",
)),
))
}
}
}
25 changes: 18 additions & 7 deletions rama-http/src/headers/x_robots_tag_components/robots_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ macro_rules! getter_setter {
};
}

#[derive(Clone, Debug, Eq, PartialEq, Default)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct RobotsTag {
bot_name: Option<HeaderValueString>,
all: bool,
Expand All @@ -65,17 +65,28 @@ pub(crate) struct RobotsTag {
}

impl RobotsTag {
pub(super) fn new() -> Self {
Default::default()
}

pub(super) fn new_with_bot_name(bot_name: Option<HeaderValueString>) -> Self {
Self {
bot_name,
..Default::default()
all: false,
no_index: false,
no_follow: false,
none: false,
no_snippet: false,
index_if_embedded: false,
max_snippet: 0,
max_image_preview: None,
max_video_preview: None,
no_translate: false,
no_image_index: false,
unavailable_after: None,
no_ai: false,
no_image_ai: false,
spc: false,
custom_rules: vec![],
}
}

pub(super) fn add_custom_rule(&mut self, rule: CustomRule) -> &mut Self {
self.custom_rules.push(rule);
self
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rama_core::error::OpaqueError;
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;
Expand All @@ -8,46 +9,53 @@ macro_rules! builder_field {
($field:ident, $type:ty) => {
paste::paste! {
pub(super) fn [<$field>](mut self, [<$field>]: $type) -> Self {
self.0.[<set_ $field>]([<$field>]);
self.content.[<set_ $field>]([<$field>]);
self
}

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

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

#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct RobotsTagBuilder<T = ()>(T);
pub(super) struct RobotsTagBuilder<T = ()> {
content: T,
valid: bool
}

impl RobotsTagBuilder<()> {
pub(super) fn new() -> Self {
RobotsTagBuilder(())
RobotsTagBuilder{content: (), valid: false }
}

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

impl RobotsTagBuilder<RobotsTag> {
pub(super) fn build(self) -> RobotsTag {
self.0
pub(super) fn build(self) -> Result<RobotsTag, OpaqueError> {
if self.valid {
Ok(self.content)
} else {
Err(OpaqueError::from_display("not a valid robots tag"))
}
}

pub(super) fn add_custom_rule(&mut self, rule: CustomRule) -> &mut Self {
self.0.add_custom_rule(rule);
self.content.add_custom_rule(rule);
self
}

Expand Down
16 changes: 4 additions & 12 deletions rama-http/src/headers/x_robots_tag_components/valid_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::str::FromStr;
pub(super) struct ValidDate(DateTime<Utc>);

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

Expand All @@ -26,14 +26,6 @@ impl From<ValidDate> for DateTime<Utc> {
}
}

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
Expand All @@ -50,14 +42,14 @@ impl FromStr for ValidDate {
type Err = OpaqueError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
ValidDate::new(
Ok(ValidDate::new(
DateTime::parse_from_rfc3339(s)
.or_else(|_| {
DateTime::parse_from_rfc2822(s)
.or_else(|_| DateTime::parse_from_str(s, "%A, %d-%b-%y %T %Z"))
})
.with_context(|| "Failed to parse date")?
.with_timezone(&Utc),
)
))
}
}

0 comments on commit f10e6df

Please sign in to comment.