-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 382/ support X-Robots-Tag as a typed http header XRobotsTag #393
base: main
Are you sure you want to change the base?
Changes from 19 commits
ce6e2b7
ff26238
caefce6
a7b8ebd
f696c50
78c2ba6
23c8fef
36af384
4dacfcb
a57a00b
d4fa1ad
e66d95b
6d0cf14
6c350db
2c2dcfa
97230f5
881e70c
e003827
2ea9085
707a209
f280156
92cd0cc
bd571c4
5933ec2
f10e6df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub(crate) mod csv; | ||
/// Internal utility functions for headers. | ||
pub(crate) mod quality_value; | ||
|
||
pub(crate) mod value_string; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use std::{ | ||
fmt, | ||
str::{self, FromStr}, | ||
}; | ||
|
||
use bytes::Bytes; | ||
use http::header::HeaderValue; | ||
|
||
use crate::headers::Error; | ||
|
||
/// A value that is both a valid `HeaderValue` and `String`. | ||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct HeaderValueString { | ||
/// Care must be taken to only set this value when it is also | ||
/// a valid `String`, since `as_str` will convert to a `&str` | ||
/// in an unchecked manner. | ||
value: HeaderValue, | ||
} | ||
|
||
impl HeaderValueString { | ||
pub(crate) fn from_val(val: &HeaderValue) -> Result<Self, Error> { | ||
if val.to_str().is_ok() { | ||
Ok(HeaderValueString { value: val.clone() }) | ||
} else { | ||
Err(Error::invalid()) | ||
} | ||
} | ||
|
||
pub(crate) fn from_string(src: String) -> Option<Self> { | ||
// A valid `str` (the argument)... | ||
let bytes = Bytes::from(src); | ||
HeaderValue::from_maybe_shared(bytes) | ||
.ok() | ||
.map(|value| HeaderValueString { value }) | ||
} | ||
|
||
pub(crate) fn from_static(src: &'static str) -> HeaderValueString { | ||
// A valid `str` (the argument)... | ||
HeaderValueString { | ||
value: HeaderValue::from_static(src), | ||
} | ||
} | ||
|
||
pub(crate) fn as_str(&self) -> &str { | ||
// HeaderValueString is only created from HeaderValues | ||
// that have validated they are also UTF-8 strings. | ||
unsafe { str::from_utf8_unchecked(self.value.as_bytes()) } | ||
} | ||
} | ||
|
||
impl fmt::Debug for HeaderValueString { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
fmt::Debug::fmt(self.as_str(), f) | ||
} | ||
} | ||
|
||
impl fmt::Display for HeaderValueString { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
fmt::Display::fmt(self.as_str(), f) | ||
} | ||
} | ||
|
||
impl<'a> From<&'a HeaderValueString> for HeaderValue { | ||
fn from(src: &'a HeaderValueString) -> HeaderValue { | ||
src.value.clone() | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct FromStrError(()); | ||
|
||
impl FromStr for HeaderValueString { | ||
type Err = FromStrError; | ||
|
||
fn from_str(src: &str) -> Result<Self, Self::Err> { | ||
// A valid `str` (the argument)... | ||
src.parse() | ||
.map(|value| HeaderValueString { value }) | ||
.map_err(|_| FromStrError(())) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::headers::util::value_string::{FromStrError, HeaderValueString}; | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct CustomRule { | ||
key: HeaderValueString, | ||
value: Option<HeaderValueString>, | ||
} | ||
|
||
impl CustomRule { | ||
pub 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> { | ||
Ok(Self { | ||
key: key.parse()?, | ||
value: Some(value.parse()?), | ||
}) | ||
} | ||
|
||
pub fn key(&self) -> &HeaderValueString { | ||
&self.key | ||
} | ||
|
||
pub fn value(&self) -> Option<&HeaderValueString> { | ||
self.value.as_ref() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use rama_core::error::OpaqueError; | ||
use std::fmt::Formatter; | ||
use std::str::FromStr; | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub enum MaxImagePreviewSetting { | ||
None, | ||
Standard, | ||
Large, | ||
} | ||
|
||
impl std::fmt::Display for MaxImagePreviewSetting { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
MaxImagePreviewSetting::None => write!(f, "none"), | ||
MaxImagePreviewSetting::Standard => write!(f, "standard"), | ||
MaxImagePreviewSetting::Large => write!(f, "large"), | ||
} | ||
} | ||
} | ||
|
||
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( | ||
"failed to parse MaxImagePreviewSetting", | ||
)), | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub mod robots_tag; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. normally you would make the modules private (e.g. |
||
pub use robots_tag::RobotsTag; | ||
|
||
pub mod robots_tag_builder; | ||
pub use robots_tag_builder::RobotsTagBuilder; | ||
|
||
pub mod max_image_preview_setting; | ||
pub use max_image_preview_setting::MaxImagePreviewSetting; | ||
|
||
pub mod custom_rule; | ||
pub use custom_rule::CustomRule; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to add this to
./Cargo.toml
(the root file) and than use herechrome = { workspace = true }