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,
"{}",
match self {
Self::Required { message } => message.as_deref().unwrap_or(REQUIRED.1),
Self::NotAuthorized { message } => message.as_deref().unwrap_or(NOT_AUTHORIZED.1),
Self::NotValid { message } => message.as_deref().unwrap_or(NOT_VALID.1),
Self::Required { .. } => REQUIRED,
Self::NotAuthorized { .. } => NOT_AUTHORIZED,
Self::NotValid { .. } => NOT_VALID,
}
.1
)
}
}
@ -104,6 +105,7 @@ fn test_from_str() {
assert_eq!(required.message(), Some("Message"));
assert_eq!(required.to_code(), REQUIRED.0);
assert_eq!(required.to_string(), REQUIRED.1);
let required = Certificate::from_str("60\r\n").unwrap();

View file

@ -64,13 +64,13 @@ impl std::fmt::Display for Permanent {
f,
"{}",
match self {
Self::Default { message } => message.as_deref().unwrap_or(DEFAULT.1),
Self::NotFound { message } => message.as_deref().unwrap_or(NOT_FOUND.1),
Self::Gone { message } => message.as_deref().unwrap_or(GONE.1),
Self::ProxyRequestRefused { message } =>
message.as_deref().unwrap_or(PROXY_REQUEST_REFUSED.1),
Self::BadRequest { message } => message.as_deref().unwrap_or(BAD_REQUEST.1),
Self::Default { .. } => DEFAULT,
Self::NotFound { .. } => NOT_FOUND,
Self::Gone { .. } => GONE,
Self::ProxyRequestRefused { .. } => PROXY_REQUEST_REFUSED,
Self::BadRequest { .. } => BAD_REQUEST,
}
.1
)
}
}
@ -126,44 +126,54 @@ fn test_from_str() {
let default = Permanent::from_str("50 Message\r\n").unwrap();
assert_eq!(default.message(), Some("Message"));
assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
let default = Permanent::from_str("50\r\n").unwrap();
assert_eq!(default.message(), None);
assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
// 51
let not_found = Permanent::from_str("51 Message\r\n").unwrap();
assert_eq!(not_found.message(), Some("Message"));
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();
assert_eq!(not_found.message(), None);
assert_eq!(not_found.to_code(), NOT_FOUND.0);
assert_eq!(not_found.to_string(), NOT_FOUND.1);
// 52
let gone = Permanent::from_str("52 Message\r\n").unwrap();
assert_eq!(gone.message(), Some("Message"));
assert_eq!(gone.to_code(), GONE.0);
assert_eq!(gone.to_string(), GONE.1);
let gone = Permanent::from_str("52\r\n").unwrap();
assert_eq!(gone.message(), None);
assert_eq!(gone.to_code(), GONE.0);
assert_eq!(gone.to_string(), GONE.1);
// 53
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.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();
assert_eq!(proxy_request_refused.message(), None);
assert_eq!(proxy_request_refused.to_code(), PROXY_REQUEST_REFUSED.0);
assert_eq!(proxy_request_refused.to_string(), PROXY_REQUEST_REFUSED.1);
// 59
let bad_request = Permanent::from_str("59 Message\r\n").unwrap();
assert_eq!(bad_request.message(), Some("Message"));
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();
assert_eq!(bad_request.message(), None);
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,
"{}",
match self {
Self::Default { message } => message.as_deref().unwrap_or(DEFAULT.1),
Self::ServerUnavailable { message } =>
message.as_deref().unwrap_or(SERVER_UNAVAILABLE.1),
Self::CgiError { message } => message.as_deref().unwrap_or(CGI_ERROR.1),
Self::ProxyError { message } => message.as_deref().unwrap_or(PROXY_ERROR.1),
Self::SlowDown { message } => message.as_deref().unwrap_or(SLOW_DOWN.1),
Self::Default { .. } => DEFAULT,
Self::ServerUnavailable { .. } => SERVER_UNAVAILABLE,
Self::CgiError { .. } => CGI_ERROR,
Self::ProxyError { .. } => PROXY_ERROR,
Self::SlowDown { .. } => SLOW_DOWN,
}
.1
)
}
}
@ -126,44 +126,54 @@ fn test_from_str() {
let default = Temporary::from_str("40 Message\r\n").unwrap();
assert_eq!(default.message(), Some("Message"));
assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
let default = Temporary::from_str("40\r\n").unwrap();
assert_eq!(default.message(), None);
assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
// 41
let server_unavailable = Temporary::from_str("41 Message\r\n").unwrap();
assert_eq!(server_unavailable.message(), Some("Message"));
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();
assert_eq!(server_unavailable.message(), None);
assert_eq!(server_unavailable.to_code(), SERVER_UNAVAILABLE.0);
assert_eq!(server_unavailable.to_string(), SERVER_UNAVAILABLE.1);
// 42
let cgi_error = Temporary::from_str("42 Message\r\n").unwrap();
assert_eq!(cgi_error.message(), Some("Message"));
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();
assert_eq!(cgi_error.message(), None);
assert_eq!(cgi_error.to_code(), CGI_ERROR.0);
assert_eq!(cgi_error.to_string(), CGI_ERROR.1);
// 43
let proxy_error = Temporary::from_str("43 Message\r\n").unwrap();
assert_eq!(proxy_error.message(), Some("Message"));
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();
assert_eq!(proxy_error.message(), None);
assert_eq!(proxy_error.to_code(), PROXY_ERROR.0);
assert_eq!(proxy_error.to_string(), PROXY_ERROR.1);
// 44
let slow_down = Temporary::from_str("44 Message\r\n").unwrap();
assert_eq!(slow_down.message(), Some("Message"));
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();
assert_eq!(slow_down.message(), None);
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,
"{}",
match self {
Self::Default { message } => message.as_deref().unwrap_or(DEFAULT.1),
Self::Sensitive { message } => message.as_deref().unwrap_or(SENSITIVE.1),
Self::Default { .. } => DEFAULT,
Self::Sensitive { .. } => SENSITIVE,
}
.1
)
}
}
@ -87,23 +88,23 @@ fn test_from_str() {
// 10
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.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();
assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.message(), None);
assert_eq!(default.to_code(), DEFAULT.0);
assert_eq!(default.to_string(), DEFAULT.1);
// 11
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.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();
assert_eq!(sensitive.to_code(), SENSITIVE.0);
assert_eq!(sensitive.message(), None);
assert_eq!(sensitive.to_code(), SENSITIVE.0);
assert_eq!(sensitive.to_string(), SENSITIVE.1);
}

View file

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