implement message_or_default method, add comments, add missed members test

This commit is contained in:
yggverse 2025-03-25 03:20:40 +02:00
parent 3b24625d66
commit d565d56c17
3 changed files with 45 additions and 2 deletions

View file

@ -48,6 +48,16 @@ impl Input {
} }
} }
/// Get optional message for `Self`
/// * if the optional message not provided by the server, return children `DEFAULT_MESSAGE`
pub fn message_or_default(&self) -> &str {
match self {
Self::Default(default) => default.message_or_default(),
Self::Sensitive(sensitive) => sensitive.message_or_default(),
}
}
/// Get header string of `Self`
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
match self { match self {
Self::Default(default) => default.as_str(), Self::Default(default) => default.as_str(),
@ -55,6 +65,7 @@ impl Input {
} }
} }
/// Get header bytes of `Self`
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
match self { match self {
Self::Default(default) => default.as_bytes(), Self::Default(default) => default.as_bytes(),

View file

@ -4,6 +4,11 @@ pub use error::Error;
/// [Input Expected](https://geminiprotocol.net/docs/protocol-specification.gmi#status-10) status code /// [Input Expected](https://geminiprotocol.net/docs/protocol-specification.gmi#status-10) status code
pub const CODE: &[u8] = b"10"; pub const CODE: &[u8] = b"10";
/// Default message if the optional value was not provided by the server
/// * useful to skip match cases in external applications,
/// by using `super::message_or_default` method.
pub const DEFAULT_MESSAGE: &str = "Input expected";
/// Hold header `String` for [Input Expected](https://geminiprotocol.net/docs/protocol-specification.gmi#status-10) status code /// Hold header `String` for [Input Expected](https://geminiprotocol.net/docs/protocol-specification.gmi#status-10) status code
/// * this response type does not contain body data /// * this response type does not contain body data
/// * the header member is closed to require valid construction /// * the header member is closed to require valid construction
@ -34,10 +39,18 @@ impl Default {
self.0.get(2..).map(|s| s.trim()).filter(|x| !x.is_empty()) self.0.get(2..).map(|s| s.trim()).filter(|x| !x.is_empty())
} }
/// Get optional message for `Self`
/// * if the optional message not provided by the server, return `DEFAULT_MESSAGE`
pub fn message_or_default(&self) -> &str {
self.message().unwrap_or(DEFAULT_MESSAGE)
}
/// Get header string of `Self`
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
&self.0 &self.0
} }
/// Get header bytes of `Self`
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes() self.0.as_bytes()
} }
@ -48,14 +61,17 @@ fn test() {
// ok // ok
let default = Default::from_utf8("10 Default\r\n".as_bytes()).unwrap(); let default = Default::from_utf8("10 Default\r\n".as_bytes()).unwrap();
assert_eq!(default.message(), Some("Default")); assert_eq!(default.message(), Some("Default"));
assert_eq!(default.message_or_default(), "Default");
assert_eq!(default.as_str(), "10 Default\r\n"); assert_eq!(default.as_str(), "10 Default\r\n");
assert_eq!(default.as_bytes(), "10 Default\r\n".as_bytes());
let default = Default::from_utf8("10\r\n".as_bytes()).unwrap(); let default = Default::from_utf8("10\r\n".as_bytes()).unwrap();
assert_eq!(default.message(), None); assert_eq!(default.message(), None);
assert_eq!(default.message_or_default(), DEFAULT_MESSAGE);
assert_eq!(default.as_str(), "10\r\n"); assert_eq!(default.as_str(), "10\r\n");
assert_eq!(default.as_bytes(), "10\r\n".as_bytes());
// err // err
// @TODO assert!(Default::from_utf8("10Fail\r\n".as_bytes()).is_err());
assert!(Default::from_utf8("13 Fail\r\n".as_bytes()).is_err()); assert!(Default::from_utf8("13 Fail\r\n".as_bytes()).is_err());
assert!(Default::from_utf8("Fail\r\n".as_bytes()).is_err()); assert!(Default::from_utf8("Fail\r\n".as_bytes()).is_err());
assert!(Default::from_utf8("Fail".as_bytes()).is_err()); assert!(Default::from_utf8("Fail".as_bytes()).is_err());

View file

@ -4,6 +4,11 @@ pub use error::Error;
/// [Sensitive Input](https://geminiprotocol.net/docs/protocol-specification.gmi#status-11-sensitive-input) status code /// [Sensitive Input](https://geminiprotocol.net/docs/protocol-specification.gmi#status-11-sensitive-input) status code
pub const CODE: &[u8] = b"11"; pub const CODE: &[u8] = b"11";
/// Default message if the optional value was not provided by the server
/// * useful to skip match cases in external applications,
/// by using `super::message_or_default` method.
pub const DEFAULT_MESSAGE: &str = "Sensitive input expected";
/// Hold header `String` for [Sensitive Input](https://geminiprotocol.net/docs/protocol-specification.gmi#status-11-sensitive-input) status code /// Hold header `String` for [Sensitive Input](https://geminiprotocol.net/docs/protocol-specification.gmi#status-11-sensitive-input) status code
/// * this response type does not contain body data /// * this response type does not contain body data
/// * the header member is closed to require valid construction /// * the header member is closed to require valid construction
@ -34,10 +39,18 @@ impl Sensitive {
self.0.get(2..).map(|s| s.trim()).filter(|x| !x.is_empty()) self.0.get(2..).map(|s| s.trim()).filter(|x| !x.is_empty())
} }
/// Get optional message for `Self`
/// * if the optional message not provided by the server, return `DEFAULT_MESSAGE`
pub fn message_or_default(&self) -> &str {
self.message().unwrap_or(DEFAULT_MESSAGE)
}
/// Get header string of `Self`
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
&self.0 &self.0
} }
/// Get header bytes of `Self`
pub fn as_bytes(&self) -> &[u8] { pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes() self.0.as_bytes()
} }
@ -48,14 +61,17 @@ fn test() {
// ok // ok
let sensitive = Sensitive::from_utf8("11 Sensitive\r\n".as_bytes()).unwrap(); let sensitive = Sensitive::from_utf8("11 Sensitive\r\n".as_bytes()).unwrap();
assert_eq!(sensitive.message(), Some("Sensitive")); assert_eq!(sensitive.message(), Some("Sensitive"));
assert_eq!(sensitive.message_or_default(), "Sensitive");
assert_eq!(sensitive.as_str(), "11 Sensitive\r\n"); assert_eq!(sensitive.as_str(), "11 Sensitive\r\n");
assert_eq!(sensitive.as_bytes(), "11 Sensitive\r\n".as_bytes());
let sensitive = Sensitive::from_utf8("11\r\n".as_bytes()).unwrap(); let sensitive = Sensitive::from_utf8("11\r\n".as_bytes()).unwrap();
assert_eq!(sensitive.message(), None); assert_eq!(sensitive.message(), None);
assert_eq!(sensitive.message_or_default(), DEFAULT_MESSAGE);
assert_eq!(sensitive.as_str(), "11\r\n"); assert_eq!(sensitive.as_str(), "11\r\n");
assert_eq!(sensitive.as_bytes(), "11\r\n".as_bytes());
// err // err
// @TODO assert!(Sensitive::from_utf8("11Fail\r\n".as_bytes()).is_err());
assert!(Sensitive::from_utf8("13 Fail\r\n".as_bytes()).is_err()); assert!(Sensitive::from_utf8("13 Fail\r\n".as_bytes()).is_err());
assert!(Sensitive::from_utf8("Fail\r\n".as_bytes()).is_err()); assert!(Sensitive::from_utf8("Fail\r\n".as_bytes()).is_err());
assert!(Sensitive::from_utf8("Fail".as_bytes()).is_err()); assert!(Sensitive::from_utf8("Fail".as_bytes()).is_err());