exclude message from string trait

This commit is contained in:
yggverse 2025-02-02 23:08:42 +02:00
parent 5358e43697
commit 8df7af44b5
5 changed files with 56 additions and 29 deletions

View file

@ -55,10 +55,11 @@ impl std::fmt::Display for Certificate {
f, f,
"{}", "{}",
match self { match self {
Self::Required { message } => message.as_deref().unwrap_or(REQUIRED.1), Self::Required { .. } => REQUIRED,
Self::NotAuthorized { message } => message.as_deref().unwrap_or(NOT_AUTHORIZED.1), Self::NotAuthorized { .. } => NOT_AUTHORIZED,
Self::NotValid { message } => message.as_deref().unwrap_or(NOT_VALID.1), Self::NotValid { .. } => NOT_VALID,
} }
.1
) )
} }
} }
@ -104,6 +105,7 @@ fn test_from_str() {
assert_eq!(required.message(), Some("Message")); assert_eq!(required.message(), Some("Message"));
assert_eq!(required.to_code(), REQUIRED.0); assert_eq!(required.to_code(), REQUIRED.0);
assert_eq!(required.to_string(), REQUIRED.1);
let required = Certificate::from_str("60\r\n").unwrap(); let required = Certificate::from_str("60\r\n").unwrap();

View file

@ -64,13 +64,13 @@ impl std::fmt::Display for Permanent {
f, f,
"{}", "{}",
match self { match self {
Self::Default { message } => message.as_deref().unwrap_or(DEFAULT.1), Self::Default { .. } => DEFAULT,
Self::NotFound { message } => message.as_deref().unwrap_or(NOT_FOUND.1), Self::NotFound { .. } => NOT_FOUND,
Self::Gone { message } => message.as_deref().unwrap_or(GONE.1), Self::Gone { .. } => GONE,
Self::ProxyRequestRefused { message } => Self::ProxyRequestRefused { .. } => PROXY_REQUEST_REFUSED,
message.as_deref().unwrap_or(PROXY_REQUEST_REFUSED.1), Self::BadRequest { .. } => BAD_REQUEST,
Self::BadRequest { message } => message.as_deref().unwrap_or(BAD_REQUEST.1),
} }
.1
) )
} }
} }
@ -126,44 +126,54 @@ fn test_from_str() {
let default = Permanent::from_str("50 Message\r\n").unwrap(); let default = Permanent::from_str("50 Message\r\n").unwrap();
assert_eq!(default.message(), Some("Message")); assert_eq!(default.message(), Some("Message"));
assert_eq!(default.to_code(), DEFAULT.0); assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
let default = Permanent::from_str("50\r\n").unwrap(); let default = Permanent::from_str("50\r\n").unwrap();
assert_eq!(default.message(), None); assert_eq!(default.message(), None);
assert_eq!(default.to_code(), DEFAULT.0); assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
// 51 // 51
let not_found = Permanent::from_str("51 Message\r\n").unwrap(); let not_found = Permanent::from_str("51 Message\r\n").unwrap();
assert_eq!(not_found.message(), Some("Message")); assert_eq!(not_found.message(), Some("Message"));
assert_eq!(not_found.to_code(), NOT_FOUND.0); assert_eq!(not_found.to_code(), NOT_FOUND.0);
assert_eq!(not_found.to_string(), NOT_FOUND.1);
let not_found = Permanent::from_str("51\r\n").unwrap(); let not_found = Permanent::from_str("51\r\n").unwrap();
assert_eq!(not_found.message(), None); assert_eq!(not_found.message(), None);
assert_eq!(not_found.to_code(), NOT_FOUND.0); assert_eq!(not_found.to_code(), NOT_FOUND.0);
assert_eq!(not_found.to_string(), NOT_FOUND.1);
// 52 // 52
let gone = Permanent::from_str("52 Message\r\n").unwrap(); let gone = Permanent::from_str("52 Message\r\n").unwrap();
assert_eq!(gone.message(), Some("Message")); assert_eq!(gone.message(), Some("Message"));
assert_eq!(gone.to_code(), GONE.0); assert_eq!(gone.to_code(), GONE.0);
assert_eq!(gone.to_string(), GONE.1);
let gone = Permanent::from_str("52\r\n").unwrap(); let gone = Permanent::from_str("52\r\n").unwrap();
assert_eq!(gone.message(), None); assert_eq!(gone.message(), None);
assert_eq!(gone.to_code(), GONE.0); assert_eq!(gone.to_code(), GONE.0);
assert_eq!(gone.to_string(), GONE.1);
// 53 // 53
let proxy_request_refused = Permanent::from_str("53 Message\r\n").unwrap(); let proxy_request_refused = Permanent::from_str("53 Message\r\n").unwrap();
assert_eq!(proxy_request_refused.message(), Some("Message")); assert_eq!(proxy_request_refused.message(), Some("Message"));
assert_eq!(proxy_request_refused.to_code(), PROXY_REQUEST_REFUSED.0); assert_eq!(proxy_request_refused.to_code(), PROXY_REQUEST_REFUSED.0);
assert_eq!(proxy_request_refused.to_string(), PROXY_REQUEST_REFUSED.1);
let proxy_request_refused = Permanent::from_str("53\r\n").unwrap(); let proxy_request_refused = Permanent::from_str("53\r\n").unwrap();
assert_eq!(proxy_request_refused.message(), None); assert_eq!(proxy_request_refused.message(), None);
assert_eq!(proxy_request_refused.to_code(), PROXY_REQUEST_REFUSED.0); assert_eq!(proxy_request_refused.to_code(), PROXY_REQUEST_REFUSED.0);
assert_eq!(proxy_request_refused.to_string(), PROXY_REQUEST_REFUSED.1);
// 59 // 59
let bad_request = Permanent::from_str("59 Message\r\n").unwrap(); let bad_request = Permanent::from_str("59 Message\r\n").unwrap();
assert_eq!(bad_request.message(), Some("Message")); assert_eq!(bad_request.message(), Some("Message"));
assert_eq!(bad_request.to_code(), BAD_REQUEST.0); assert_eq!(bad_request.to_code(), BAD_REQUEST.0);
assert_eq!(bad_request.to_string(), BAD_REQUEST.1);
let bad_request = Permanent::from_str("59\r\n").unwrap(); let bad_request = Permanent::from_str("59\r\n").unwrap();
assert_eq!(bad_request.message(), None); assert_eq!(bad_request.message(), None);
assert_eq!(bad_request.to_code(), BAD_REQUEST.0); assert_eq!(bad_request.to_code(), BAD_REQUEST.0);
assert_eq!(bad_request.to_string(), BAD_REQUEST.1);
} }

View file

@ -64,13 +64,13 @@ impl std::fmt::Display for Temporary {
f, f,
"{}", "{}",
match self { match self {
Self::Default { message } => message.as_deref().unwrap_or(DEFAULT.1), Self::Default { .. } => DEFAULT,
Self::ServerUnavailable { message } => Self::ServerUnavailable { .. } => SERVER_UNAVAILABLE,
message.as_deref().unwrap_or(SERVER_UNAVAILABLE.1), Self::CgiError { .. } => CGI_ERROR,
Self::CgiError { message } => message.as_deref().unwrap_or(CGI_ERROR.1), Self::ProxyError { .. } => PROXY_ERROR,
Self::ProxyError { message } => message.as_deref().unwrap_or(PROXY_ERROR.1), Self::SlowDown { .. } => SLOW_DOWN,
Self::SlowDown { message } => message.as_deref().unwrap_or(SLOW_DOWN.1),
} }
.1
) )
} }
} }
@ -126,44 +126,54 @@ fn test_from_str() {
let default = Temporary::from_str("40 Message\r\n").unwrap(); let default = Temporary::from_str("40 Message\r\n").unwrap();
assert_eq!(default.message(), Some("Message")); assert_eq!(default.message(), Some("Message"));
assert_eq!(default.to_code(), DEFAULT.0); assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
let default = Temporary::from_str("40\r\n").unwrap(); let default = Temporary::from_str("40\r\n").unwrap();
assert_eq!(default.message(), None); assert_eq!(default.message(), None);
assert_eq!(default.to_code(), DEFAULT.0); assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
// 41 // 41
let server_unavailable = Temporary::from_str("41 Message\r\n").unwrap(); let server_unavailable = Temporary::from_str("41 Message\r\n").unwrap();
assert_eq!(server_unavailable.message(), Some("Message")); assert_eq!(server_unavailable.message(), Some("Message"));
assert_eq!(server_unavailable.to_code(), SERVER_UNAVAILABLE.0); assert_eq!(server_unavailable.to_code(), SERVER_UNAVAILABLE.0);
assert_eq!(server_unavailable.to_string(), SERVER_UNAVAILABLE.1);
let server_unavailable = Temporary::from_str("41\r\n").unwrap(); let server_unavailable = Temporary::from_str("41\r\n").unwrap();
assert_eq!(server_unavailable.message(), None); assert_eq!(server_unavailable.message(), None);
assert_eq!(server_unavailable.to_code(), SERVER_UNAVAILABLE.0); assert_eq!(server_unavailable.to_code(), SERVER_UNAVAILABLE.0);
assert_eq!(server_unavailable.to_string(), SERVER_UNAVAILABLE.1);
// 42 // 42
let cgi_error = Temporary::from_str("42 Message\r\n").unwrap(); let cgi_error = Temporary::from_str("42 Message\r\n").unwrap();
assert_eq!(cgi_error.message(), Some("Message")); assert_eq!(cgi_error.message(), Some("Message"));
assert_eq!(cgi_error.to_code(), CGI_ERROR.0); assert_eq!(cgi_error.to_code(), CGI_ERROR.0);
assert_eq!(cgi_error.to_string(), CGI_ERROR.1);
let cgi_error = Temporary::from_str("42\r\n").unwrap(); let cgi_error = Temporary::from_str("42\r\n").unwrap();
assert_eq!(cgi_error.message(), None); assert_eq!(cgi_error.message(), None);
assert_eq!(cgi_error.to_code(), CGI_ERROR.0); assert_eq!(cgi_error.to_code(), CGI_ERROR.0);
assert_eq!(cgi_error.to_string(), CGI_ERROR.1);
// 43 // 43
let proxy_error = Temporary::from_str("43 Message\r\n").unwrap(); let proxy_error = Temporary::from_str("43 Message\r\n").unwrap();
assert_eq!(proxy_error.message(), Some("Message")); assert_eq!(proxy_error.message(), Some("Message"));
assert_eq!(proxy_error.to_code(), PROXY_ERROR.0); assert_eq!(proxy_error.to_code(), PROXY_ERROR.0);
assert_eq!(proxy_error.to_string(), PROXY_ERROR.1);
let proxy_error = Temporary::from_str("43\r\n").unwrap(); let proxy_error = Temporary::from_str("43\r\n").unwrap();
assert_eq!(proxy_error.message(), None); assert_eq!(proxy_error.message(), None);
assert_eq!(proxy_error.to_code(), PROXY_ERROR.0); assert_eq!(proxy_error.to_code(), PROXY_ERROR.0);
assert_eq!(proxy_error.to_string(), PROXY_ERROR.1);
// 44 // 44
let slow_down = Temporary::from_str("44 Message\r\n").unwrap(); let slow_down = Temporary::from_str("44 Message\r\n").unwrap();
assert_eq!(slow_down.message(), Some("Message")); assert_eq!(slow_down.message(), Some("Message"));
assert_eq!(slow_down.to_code(), SLOW_DOWN.0); assert_eq!(slow_down.to_code(), SLOW_DOWN.0);
assert_eq!(slow_down.to_string(), SLOW_DOWN.1);
let slow_down = Temporary::from_str("44\r\n").unwrap(); let slow_down = Temporary::from_str("44\r\n").unwrap();
assert_eq!(slow_down.message(), None); assert_eq!(slow_down.message(), None);
assert_eq!(slow_down.to_code(), SLOW_DOWN.0); assert_eq!(slow_down.to_code(), SLOW_DOWN.0);
assert_eq!(slow_down.to_string(), SLOW_DOWN.1);
} }

View file

@ -46,9 +46,10 @@ impl std::fmt::Display for Input {
f, f,
"{}", "{}",
match self { match self {
Self::Default { message } => message.as_deref().unwrap_or(DEFAULT.1), Self::Default { .. } => DEFAULT,
Self::Sensitive { message } => message.as_deref().unwrap_or(SENSITIVE.1), Self::Sensitive { .. } => SENSITIVE,
} }
.1
) )
} }
} }
@ -87,23 +88,23 @@ fn test_from_str() {
// 10 // 10
let default = Input::from_str("10 Default\r\n").unwrap(); let default = Input::from_str("10 Default\r\n").unwrap();
assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.message(), Some("Default")); assert_eq!(default.message(), Some("Default"));
assert_eq!(default.to_string(), "Default"); assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
let default = Input::from_str("10\r\n").unwrap(); let default = Input::from_str("10\r\n").unwrap();
assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.message(), None); assert_eq!(default.message(), None);
assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1); assert_eq!(default.to_string(), DEFAULT.1);
// 11 // 11
let sensitive = Input::from_str("11 Sensitive\r\n").unwrap(); let sensitive = Input::from_str("11 Sensitive\r\n").unwrap();
assert_eq!(sensitive.to_code(), SENSITIVE.0);
assert_eq!(sensitive.message(), Some("Sensitive")); assert_eq!(sensitive.message(), Some("Sensitive"));
assert_eq!(sensitive.to_string(), "Sensitive"); assert_eq!(sensitive.to_code(), SENSITIVE.0);
assert_eq!(sensitive.to_string(), SENSITIVE.1);
let sensitive = Input::from_str("11\r\n").unwrap(); let sensitive = Input::from_str("11\r\n").unwrap();
assert_eq!(sensitive.to_code(), SENSITIVE.0);
assert_eq!(sensitive.message(), None); assert_eq!(sensitive.message(), None);
assert_eq!(sensitive.to_code(), SENSITIVE.0);
assert_eq!(sensitive.to_string(), SENSITIVE.1); assert_eq!(sensitive.to_string(), SENSITIVE.1);
} }

View file

@ -3,8 +3,8 @@ pub use error::Error;
use glib::GStringPtr; use glib::GStringPtr;
const TEMPORARY: u8 = 30; const TEMPORARY: (u8, &str) = (30, "Temporary redirect");
const PERMANENT: u8 = 31; const PERMANENT: (u8, &str) = (31, "Permanent redirect");
pub enum Redirect { pub enum Redirect {
/// https://geminiprotocol.net/docs/protocol-specification.gmi#status-30-temporary-redirection /// https://geminiprotocol.net/docs/protocol-specification.gmi#status-30-temporary-redirection
@ -32,6 +32,7 @@ impl Redirect {
Self::Permanent { .. } => PERMANENT, Self::Permanent { .. } => PERMANENT,
Self::Temporary { .. } => TEMPORARY, Self::Temporary { .. } => TEMPORARY,
} }
.0
} }
// Getters // Getters
@ -50,9 +51,10 @@ impl std::fmt::Display for Redirect {
f, f,
"{}", "{}",
match self { match self {
Self::Permanent { target } => format!("Permanent redirection to `{target}`"), Self::Permanent { .. } => PERMANENT,
Self::Temporary { target } => format!("Temporary redirection to `{target}`"), Self::Temporary { .. } => TEMPORARY,
} }
.1
) )
} }
} }
@ -104,9 +106,11 @@ fn test_from_str() {
let temporary = Redirect::from_str("30 /uri\r\n").unwrap(); let temporary = Redirect::from_str("30 /uri\r\n").unwrap();
assert_eq!(temporary.target(), "/uri"); assert_eq!(temporary.target(), "/uri");
assert_eq!(temporary.to_code(), TEMPORARY); assert_eq!(temporary.to_code(), TEMPORARY.0);
assert_eq!(temporary.to_string(), TEMPORARY.1);
let permanent = Redirect::from_str("31 /uri\r\n").unwrap(); let permanent = Redirect::from_str("31 /uri\r\n").unwrap();
assert_eq!(permanent.target(), "/uri"); assert_eq!(permanent.target(), "/uri");
assert_eq!(permanent.to_code(), PERMANENT); assert_eq!(permanent.to_code(), PERMANENT.0);
assert_eq!(permanent.to_string(), PERMANENT.1);
} }