use std::{
borrow::{Borrow, Cow},
fmt,
string::String as StdString,
};
usecrate::{error, Error};
/// An owned structured field value [string]. /// /// Strings may only contain printable ASCII characters (i.e. the range /// `0x20 ..= 0x7e`). /// /// [string]: <https://httpwg.org/specs/rfc9651.html#string> #[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] pubstruct String(StdString);
/// A borrowed structured field value [string]. /// /// Strings may only contain printable ASCII characters (i.e. the range /// `0x20 ..= 0x7e`). /// /// This type is to [`String`] as [`str`] is to [`std::string::String`]. /// /// [string]: <https://httpwg.org/specs/rfc9651.html#string> #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, ref_cast::RefCastCustom)] #[repr(transparent)] pubstruct StringRef(str);
/// Creates a `&StringRef` from a `&str`. /// /// # Errors /// The error reports the cause of any failed string validation. #[allow(clippy::should_implement_trait)] pubfn from_str(v: &str) -> Result<&Self, Error> {
validate(v.as_bytes())?;
Ok(Self::cast(v))
}
/// Creates a `&StringRef`, panicking if the value is invalid. /// /// This method is intended to be called from `const` contexts in which the /// value is known to be valid. Use [`StringRef::from_str`] for non-panicking /// conversions. #[must_use] pubconstfn constant(v: &str) -> &Self { match validate(v.as_bytes()) {
Ok(()) => Self::cast(v),
Err(_) => panic!("invalid character for string"),
}
}
/// Returns the string as a `&str`. #[must_use] pubfn as_str(&self) -> &str {
&self.0
}
}
impl String { /// Creates a `String` from a `std::string::String`. /// /// Returns the original value if the conversion failed. /// /// # Errors /// The error reports any problems from failed validation. pubfn from_string(v: StdString) -> Result<Self, (Error, StdString)> { match validate(v.as_bytes()) {
Ok(()) => Ok(Self(v)),
Err(err) => Err((err.into(), v)),
}
}
}
/// Creates a `&StringRef`, panicking if the value is invalid. /// /// This is a convenience free function for [`StringRef::constant`]. /// /// This method is intended to be called from `const` contexts in which the /// value is known to be valid. Use [`StringRef::from_str`] for non-panicking /// conversions. #[must_use] pubconstfn string_ref(v: &str) -> &StringRef {
StringRef::constant(v)
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.