From f10e6dfb8cd285d49524e1ffbc9c5170d0c48e75 Mon Sep 17 00:00:00 2001 From: hafihaf123 Date: Fri, 31 Jan 2025 20:50:38 +0100 Subject: [PATCH] enhance code --- Cargo.toml | 2 +- rama-http/src/headers/x_robots_tag.rs | 30 +++++++++---------- .../max_image_preview_setting.rs | 15 ++++++---- .../x_robots_tag_components/robots_tag.rs | 25 +++++++++++----- .../robots_tag_builder.rs | 26 ++++++++++------ .../x_robots_tag_components/valid_date.rs | 16 +++------- 6 files changed, 64 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1597da36..71b368a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/rama-http/src/headers/x_robots_tag.rs b/rama-http/src/headers/x_robots_tag.rs index 681d37e0..50262cb1 100644 --- a/rama-http/src/headers/x_robots_tag.rs +++ b/rama-http/src/headers/x_robots_tag.rs @@ -1,7 +1,7 @@ +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 { @@ -9,19 +9,19 @@ pub struct XRobotsTag { } 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 - where - Self: Sized, - I: Iterator - { - todo!() - } + fn decode<'i, I>(values: &mut I) -> Result + where + Self: Sized, + I: Iterator, + { + todo!() + } - fn encode>(&self, values: &mut E) { - todo!() - } + fn encode>(&self, values: &mut E) { + todo!() + } } 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 628c0233..f978705b 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 @@ -23,13 +23,16 @@ impl FromStr for MaxImagePreviewSetting { type Err = OpaqueError; fn from_str(s: &str) -> Result { - 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", - )), + )) } } } 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 f03c3710..47b463cb 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 @@ -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, all: bool, @@ -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) -> 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 diff --git a/rama-http/src/headers/x_robots_tag_components/robots_tag_builder.rs b/rama-http/src/headers/x_robots_tag_components/robots_tag_builder.rs index d2146cfa..314d49a5 100644 --- a/rama-http/src/headers/x_robots_tag_components/robots_tag_builder.rs +++ b/rama-http/src/headers/x_robots_tag_components/robots_tag_builder.rs @@ -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; @@ -8,17 +9,17 @@ macro_rules! builder_field { ($field:ident, $type:ty) => { paste::paste! { pub(super) fn [<$field>](mut self, [<$field>]: $type) -> Self { - self.0.[]([<$field>]); + self.content.[]([<$field>]); self } pub(super) fn [](&mut self, [<$field>]: $type) -> &mut Self { - self.0.[]([<$field>]); + self.content.[]([<$field>]); self } pub(super) fn [](mut self, [<$field>]: $type) -> Self { - self.0.[]([<$field>]); + self.content.[]([<$field>]); self } } @@ -26,28 +27,35 @@ macro_rules! builder_field { } #[derive(Clone, Debug, Eq, PartialEq)] -pub(super) struct RobotsTagBuilder(T); +pub(super) struct RobotsTagBuilder { + 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, ) -> RobotsTagBuilder { - RobotsTagBuilder(RobotsTag::new_with_bot_name(bot_name)) + RobotsTagBuilder{ content: RobotsTag::new_with_bot_name(bot_name), valid: false } } } impl RobotsTagBuilder { - pub(super) fn build(self) -> RobotsTag { - self.0 + pub(super) fn build(self) -> Result { + 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 } 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 bcd25e10..57eaa872 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 @@ -7,8 +7,8 @@ use std::str::FromStr; pub(super) struct ValidDate(DateTime); impl ValidDate { - pub(super) fn new(date: DateTime) -> Result { - Ok(Self(date)) + pub(super) fn new(date: DateTime) -> Self { + Self(date) } } @@ -26,14 +26,6 @@ impl From for DateTime { } } -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 @@ -50,7 +42,7 @@ impl FromStr for ValidDate { type Err = OpaqueError; fn from_str(s: &str) -> Result { - ValidDate::new( + Ok(ValidDate::new( DateTime::parse_from_rfc3339(s) .or_else(|_| { DateTime::parse_from_rfc2822(s) @@ -58,6 +50,6 @@ impl FromStr for ValidDate { }) .with_context(|| "Failed to parse date")? .with_timezone(&Utc), - ) + )) } }