implement message_or_default method, add comments

This commit is contained in:
yggverse 2025-03-25 03:01:19 +02:00
parent f513747e86
commit 3b24625d66
4 changed files with 56 additions and 3 deletions

View file

@ -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(),

View file

@ -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()
}

View file

@ -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()
}

View file

@ -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()
}