diff --git a/src/client/connection/response/certificate.rs b/src/client/connection/response/certificate.rs index e67ded4..07e2891 100644 --- a/src/client/connection/response/certificate.rs +++ b/src/client/connection/response/certificate.rs @@ -51,6 +51,8 @@ impl Certificate { // Getters + /// Get optional message for `Self` + /// * return `None` if the message is empty (not provided by server) pub fn message(&self) -> Option<&str> { match self { Self::Required(required) => required.message(), @@ -59,6 +61,17 @@ impl Certificate { } } + /// 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::Required(required) => required.message_or_default(), + Self::NotAuthorized(not_authorized) => not_authorized.message_or_default(), + Self::NotValid(not_valid) => not_valid.message_or_default(), + } + } + + /// Get header string of `Self` pub fn as_str(&self) -> &str { match self { Self::Required(required) => required.as_str(), @@ -67,6 +80,7 @@ impl Certificate { } } + /// Get header bytes of `Self` pub fn as_bytes(&self) -> &[u8] { match self { Self::Required(required) => required.as_bytes(), diff --git a/src/client/connection/response/certificate/not_authorized.rs b/src/client/connection/response/certificate/not_authorized.rs index 980d9ac..0c73ae7 100644 --- a/src/client/connection/response/certificate/not_authorized.rs +++ b/src/client/connection/response/certificate/not_authorized.rs @@ -4,6 +4,11 @@ pub use error::Error; /// [Not Authorized](https://geminiprotocol.net/docs/protocol-specification.gmi#status-61) status code pub const CODE: &[u8] = b"61"; +/// 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 = "Certificate is not authorized"; + /// Hold header `String` for [Not Authorized](https://geminiprotocol.net/docs/protocol-specification.gmi#status-61) status code /// * this response type does not contain body data /// * the header member is closed to require valid construction @@ -29,15 +34,23 @@ impl NotAuthorized { // Getters /// Get optional message for `Self` - /// * return `None` if the message is empty + /// * return `None` if the message is empty (not provided by server) pub fn message(&self) -> Option<&str> { 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() } diff --git a/src/client/connection/response/certificate/not_valid.rs b/src/client/connection/response/certificate/not_valid.rs index 933b694..58198a8 100644 --- a/src/client/connection/response/certificate/not_valid.rs +++ b/src/client/connection/response/certificate/not_valid.rs @@ -4,6 +4,11 @@ pub use error::Error; /// [Not Valid](https://geminiprotocol.net/docs/protocol-specification.gmi#status-62) status code pub const CODE: &[u8] = b"62"; +/// 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 = "Certificate is not valid"; + /// Hold header `String` for [Not Valid](https://geminiprotocol.net/docs/protocol-specification.gmi#status-62) status code /// * this response type does not contain body data /// * the header member is closed to require valid construction @@ -29,15 +34,23 @@ impl NotValid { // Getters /// Get optional message for `Self` - /// * return `None` if the message is empty + /// * return `None` if the message is empty (not provided by server) pub fn message(&self) -> Option<&str> { 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() } diff --git a/src/client/connection/response/certificate/required.rs b/src/client/connection/response/certificate/required.rs index 3d8f48d..105185a 100644 --- a/src/client/connection/response/certificate/required.rs +++ b/src/client/connection/response/certificate/required.rs @@ -4,6 +4,11 @@ pub use error::Error; /// [Certificate Required](https://geminiprotocol.net/docs/protocol-specification.gmi#status-60) status code pub const CODE: &[u8] = b"60"; +/// 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 = "Certificate required"; + /// Hold header `String` for [Certificate Required](https://geminiprotocol.net/docs/protocol-specification.gmi#status-60) status code /// * this response type does not contain body data /// * the header member is closed to require valid construction @@ -29,15 +34,23 @@ impl Required { // Getters /// Get optional message for `Self` - /// * return `None` if the message is empty + /// * return `None` if the message is empty (not provided by server) pub fn message(&self) -> Option<&str> { 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() }