diff --git a/src/client/connection/response/input.rs b/src/client/connection/response/input.rs index 549b13e..0cd9857 100644 --- a/src/client/connection/response/input.rs +++ b/src/client/connection/response/input.rs @@ -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 { match self { Self::Default(default) => default.as_str(), @@ -55,6 +65,7 @@ impl Input { } } + /// Get header bytes of `Self` pub fn as_bytes(&self) -> &[u8] { match self { Self::Default(default) => default.as_bytes(), diff --git a/src/client/connection/response/input/default.rs b/src/client/connection/response/input/default.rs index 93143f7..9b9f096 100644 --- a/src/client/connection/response/input/default.rs +++ b/src/client/connection/response/input/default.rs @@ -4,6 +4,11 @@ pub use error::Error; /// [Input Expected](https://geminiprotocol.net/docs/protocol-specification.gmi#status-10) status code 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 /// * this response type does not contain body data /// * 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()) } + /// 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 { &self.0 } + /// Get header bytes of `Self` pub fn as_bytes(&self) -> &[u8] { self.0.as_bytes() } @@ -48,14 +61,17 @@ fn test() { // ok let default = Default::from_utf8("10 Default\r\n".as_bytes()).unwrap(); 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_bytes(), "10 Default\r\n".as_bytes()); let default = Default::from_utf8("10\r\n".as_bytes()).unwrap(); 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_bytes(), "10\r\n".as_bytes()); // 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("Fail\r\n".as_bytes()).is_err()); assert!(Default::from_utf8("Fail".as_bytes()).is_err()); diff --git a/src/client/connection/response/input/sensitive.rs b/src/client/connection/response/input/sensitive.rs index 9219d39..4646776 100644 --- a/src/client/connection/response/input/sensitive.rs +++ b/src/client/connection/response/input/sensitive.rs @@ -4,6 +4,11 @@ pub use error::Error; /// [Sensitive Input](https://geminiprotocol.net/docs/protocol-specification.gmi#status-11-sensitive-input) status code 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 /// * this response type does not contain body data /// * 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()) } + /// 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 { &self.0 } + /// Get header bytes of `Self` pub fn as_bytes(&self) -> &[u8] { self.0.as_bytes() } @@ -48,14 +61,17 @@ fn test() { // ok let sensitive = Sensitive::from_utf8("11 Sensitive\r\n".as_bytes()).unwrap(); 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_bytes(), "11 Sensitive\r\n".as_bytes()); let sensitive = Sensitive::from_utf8("11\r\n".as_bytes()).unwrap(); 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_bytes(), "11\r\n".as_bytes()); // 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("Fail\r\n".as_bytes()).is_err()); assert!(Sensitive::from_utf8("Fail".as_bytes()).is_err());