Use serde default on struct!

This commit is contained in:
Mo 2023-02-26 16:47:44 +01:00
parent db01d33b0f
commit de869a7378

View file

@ -18,11 +18,10 @@ pub struct Email {
} }
/// UTC offset for time formatting. /// UTC offset for time formatting.
#[derive(Deserialize)] #[derive(Deserialize, Default)]
#[serde(default)]
pub struct UtcOffset { pub struct UtcOffset {
#[serde(default)]
pub hours: i8, pub hours: i8,
#[serde(default)]
pub minutes: i8, pub minutes: i8,
} }
@ -42,105 +41,103 @@ pub struct CustomField {
pub field_type: CustomFieldType, pub field_type: CustomFieldType,
} }
fn default_captcha_error() -> String {
"You did enter the wrong code at the end of the form. Please try again.".to_string()
}
fn default_email_error() -> String {
"An internal error occurred while sending your request. Please try again later.".to_string()
}
/// Error messages for localization. /// Error messages for localization.
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(default)]
pub struct ErrorMessages { pub struct ErrorMessages {
#[serde(default = "default_captcha_error")]
pub captcha_error: String, pub captcha_error: String,
#[serde(default = "default_email_error")]
pub email_error: String, pub email_error: String,
} }
impl Default for ErrorMessages {
fn default() -> Self {
Self {
captcha_error: "You did enter the wrong code at the end of the form. Please try again."
.to_string(),
email_error:
"An internal error occurred while sending your request. Please try again later."
.to_string(),
}
}
}
fn default_name_label() -> String {
"Name".to_string()
}
fn default_name_required_feedback() -> String {
"Please enter your name".to_string()
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(default)]
pub struct NameField { pub struct NameField {
#[serde(default = "default_name_label")]
pub label: String, pub label: String,
#[serde(default = "default_name_required_feedback")]
pub required_feedback: String, pub required_feedback: String,
} }
impl Default for NameField {
fn default() -> Self {
Self {
label: "Name".to_string(),
required_feedback: "Please enter your name".to_string(),
}
}
}
fn default_email_label() -> String {
"Email".to_string()
}
fn default_email_required_feedback() -> String {
"Please enter your email".to_string()
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(default)]
pub struct EmailField { pub struct EmailField {
#[serde(default = "default_email_label")]
pub label: String, pub label: String,
#[serde(default = "default_email_required_feedback")]
pub required_feedback: String, pub required_feedback: String,
} }
impl Default for EmailField {
fn default() -> Self {
Self {
label: "Email".to_string(),
required_feedback: "Please enter your email".to_string(),
}
}
}
fn default_captcha_label() -> String {
"Enter the code above".to_string()
}
fn default_captcha_required_feedback() -> String {
"Please enter the code from the image above".to_string()
}
#[derive(Deserialize)] #[derive(Deserialize)]
#[serde(default)]
pub struct CaptchaField { pub struct CaptchaField {
#[serde(default = "default_captcha_label")]
pub label: String, pub label: String,
#[serde(default = "default_captcha_required_feedback")]
pub required_feedback: String, pub required_feedback: String,
} }
impl Default for CaptchaField {
fn default() -> Self {
Self {
label: "Enter the code above".to_string(),
required_feedback: "Please enter the code from the image above".to_string(),
}
}
}
fn default_title() -> String {
"Contact form".to_string()
}
fn default_optional() -> String {
"optional".to_string()
}
fn default_submit() -> String {
"Submit".to_string()
}
fn default_success() -> String {
"Your request has been successfully submitted. We will get back to you soon.".to_string()
}
fn default_message_from() -> String {
"Message from".to_string()
}
/// Localization strings. /// Localization strings.
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Strings {
#[serde(default)] #[serde(default)]
pub struct Strings {
pub description: String, pub description: String,
#[serde(default = "default_title")]
pub title: String, pub title: String,
#[serde(default = "default_optional")]
pub optional: String, pub optional: String,
#[serde(default = "default_submit")]
pub submit: String, pub submit: String,
#[serde(default = "default_success")]
pub success: String, pub success: String,
#[serde(default = "default_message_from")]
pub message_from: String, pub message_from: String,
pub name_field: NameField, pub name_field: NameField,
pub email_field: EmailField, pub email_field: EmailField,
pub captcha_field: CaptchaField, pub captcha_field: CaptchaField,
pub error_messages: ErrorMessages, pub error_messages: ErrorMessages,
} }
impl Default for Strings {
fn default() -> Self {
Self {
description: String::default(),
title: "Contact form".to_string(),
optional: "optional".to_string(),
submit: "Submit".to_string(),
success: "Your request has been successfully submitted. We will get back to you soon."
.to_string(),
message_from: "Message from".to_string(),
name_field: NameField::default(),
email_field: EmailField::default(),
captcha_field: CaptchaField::default(),
error_messages: ErrorMessages::default(),
}
}
}
fn default_path_prefix() -> String {
"/".to_string()
}
fn default_lang() -> String {
"en".to_string()
}
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct StateConfig { pub struct StateConfig {
/// The path prefix of all routes. /// The path prefix of all routes.
@ -152,10 +149,13 @@ pub struct StateConfig {
pub lang: String, pub lang: String,
pub strings: Strings, pub strings: Strings,
} }
fn default_path_prefix() -> String {
fn default_socket_address() -> String { "/".to_string()
"0.0.0.0:80".to_string()
} }
fn default_lang() -> String {
"en".to_string()
}
/// Configuration. /// Configuration.
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Config { pub struct Config {
@ -167,6 +167,9 @@ pub struct Config {
#[serde(flatten)] #[serde(flatten)]
pub state_config: StateConfig, pub state_config: StateConfig,
} }
fn default_socket_address() -> String {
"0.0.0.0:80".to_string()
}
impl Config { impl Config {
/// Parses the configuration in the given data directory. /// Parses the configuration in the given data directory.