Skip to content

Commit bb75303

Browse files
committed
feat!: [#591] lowercase for auth::enail_on_signup emnum variants
1 parent eb987e9 commit bb75303

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

src/config/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ mod tests {
339339
port = 3001
340340
341341
[auth]
342-
email_on_signup = "Optional"
342+
email_on_signup = "optional"
343343
min_password_length = 6
344344
max_password_length = 64
345345
secret_key = "MaxVerstappenWC2021"

src/config/v1/auth.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt;
2+
use std::str::FromStr;
23

34
use serde::{Deserialize, Serialize};
45

@@ -50,6 +51,7 @@ impl Auth {
5051

5152
/// Whether the email is required on signup or not.
5253
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
54+
#[serde(rename_all = "lowercase")]
5355
pub enum EmailOnSignup {
5456
/// The email is required on signup.
5557
Required,
@@ -65,6 +67,30 @@ impl Default for EmailOnSignup {
6567
}
6668
}
6769

70+
impl fmt::Display for EmailOnSignup {
71+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72+
let display_str = match self {
73+
EmailOnSignup::Required => "required",
74+
EmailOnSignup::Optional => "optional",
75+
EmailOnSignup::None => "none",
76+
};
77+
write!(f, "{display_str}")
78+
}
79+
}
80+
81+
impl FromStr for EmailOnSignup {
82+
type Err = String;
83+
84+
fn from_str(s: &str) -> Result<Self, Self::Err> {
85+
match s.to_lowercase().as_str() {
86+
"required" => Ok(EmailOnSignup::Required),
87+
"optional" => Ok(EmailOnSignup::Optional),
88+
"none" => Ok(EmailOnSignup::None),
89+
_ => Err(format!("Unknown config 'email_on_signup' option (required, optional, none): {s}")),
90+
}
91+
}
92+
}
93+
6894
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
6995
pub struct SecretKey(String);
7096

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
//! port = 3001
181181
//!
182182
//! [auth]
183-
//! email_on_signup = "Optional"
183+
//! email_on_signup = "optional"
184184
//! min_password_length = 6
185185
//! max_password_length = 64
186186
//! secret_key = "MaxVerstappenWC2021"

src/web/api/server/v1/contexts/settings/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
//! "base_url": null
4646
//! },
4747
//! "auth": {
48-
//! "email_on_signup": "Optional",
48+
//! "email_on_signup": "optional",
4949
//! "min_password_length": 6,
5050
//! "max_password_length": 64,
5151
//! "secret_key": "MaxVerstappenWC2021"
@@ -102,7 +102,7 @@
102102
//! --header "Content-Type: application/json" \
103103
//! --header "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI" \
104104
//! --request POST \
105-
//! --data '{"website":{"name":"Torrust"},"tracker":{"url":"udp://localhost:6969","mode":"public","api_url":"http://localhost:1212/","token":"MyAccessToken","token_valid_seconds":7257600},"net":{"port":3001,"base_url":null},"auth":{"email_on_signup":"Optional","min_password_length":6,"max_password_length":64,"secret_key":"MaxVerstappenWC2021"},"database":{"connect_url":"sqlite://./storage/database/data.db?mode=rwc"},"mail":{"email_verification_enabled":false,"from":"example@email.com","reply_to":"noreply@email.com","username":"","password":"","server":"","port":25},"image_cache":{"max_request_timeout_ms":1000,"capacity":128000000,"entry_size_limit":4000000,"user_quota_period_seconds":3600,"user_quota_bytes":64000000},"api":{"default_torrent_page_size":10,"max_torrent_page_size":30},"tracker_statistics_importer":{"torrent_info_update_interval":3600}}' \
105+
//! --data '{"website":{"name":"Torrust"},"tracker":{"url":"udp://localhost:6969","mode":"public","api_url":"http://localhost:1212/","token":"MyAccessToken","token_valid_seconds":7257600},"net":{"port":3001,"base_url":null},"auth":{"email_on_signup":"optional","min_password_length":6,"max_password_length":64,"secret_key":"MaxVerstappenWC2021"},"database":{"connect_url":"sqlite://./storage/database/data.db?mode=rwc"},"mail":{"email_verification_enabled":false,"from":"example@email.com","reply_to":"noreply@email.com","username":"","password":"","server":"","port":25},"image_cache":{"max_request_timeout_ms":1000,"capacity":128000000,"entry_size_limit":4000000,"user_quota_period_seconds":3600,"user_quota_bytes":64000000},"api":{"default_torrent_page_size":10,"max_torrent_page_size":30},"tracker_statistics_importer":{"torrent_info_update_interval":3600}}' \
106106
//! "http://127.0.0.1:3001/v1/settings"
107107
//! ```
108108
//!
@@ -159,7 +159,7 @@
159159
//! "website_name": "Torrust",
160160
//! "tracker_url": "udp://localhost:6969",
161161
//! "tracker_mode": "public",
162-
//! "email_on_signup": "Optional"
162+
//! "email_on_signup": "optional"
163163
//! }
164164
//! }
165165
//! ```

src/web/api/server/v1/contexts/user/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
//!
4646
//! ```toml
4747
//! [auth]
48-
//! email_on_signup = "Optional"
48+
//! email_on_signup = "optional"
4949
//! min_password_length = 6
5050
//! max_password_length = 64
5151
//! ```

tests/common/contexts/settings/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl From<DomainNetwork> for Network {
133133
impl From<DomainAuth> for Auth {
134134
fn from(auth: DomainAuth) -> Self {
135135
Self {
136-
email_on_signup: format!("{:?}", auth.email_on_signup),
136+
email_on_signup: auth.email_on_signup.to_string(),
137137
min_password_length: auth.min_password_length,
138138
max_password_length: auth.max_password_length,
139139
secret_key: auth.secret_key.to_string(),

0 commit comments

Comments
 (0)