Use serde default on struct!
This commit is contained in:
parent
db01d33b0f
commit
de869a7378
1 changed files with 68 additions and 65 deletions
133
src/config.rs
133
src/config.rs
|
@ -18,11 +18,10 @@ pub struct Email {
|
|||
}
|
||||
|
||||
/// UTC offset for time formatting.
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Default)]
|
||||
#[serde(default)]
|
||||
pub struct UtcOffset {
|
||||
#[serde(default)]
|
||||
pub hours: i8,
|
||||
#[serde(default)]
|
||||
pub minutes: i8,
|
||||
}
|
||||
|
||||
|
@ -42,105 +41,103 @@ pub struct CustomField {
|
|||
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.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct ErrorMessages {
|
||||
#[serde(default = "default_captcha_error")]
|
||||
pub captcha_error: String,
|
||||
#[serde(default = "default_email_error")]
|
||||
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)]
|
||||
#[serde(default)]
|
||||
pub struct NameField {
|
||||
#[serde(default = "default_name_label")]
|
||||
pub label: String,
|
||||
#[serde(default = "default_name_required_feedback")]
|
||||
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)]
|
||||
#[serde(default)]
|
||||
pub struct EmailField {
|
||||
#[serde(default = "default_email_label")]
|
||||
pub label: String,
|
||||
#[serde(default = "default_email_required_feedback")]
|
||||
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)]
|
||||
#[serde(default)]
|
||||
pub struct CaptchaField {
|
||||
#[serde(default = "default_captcha_label")]
|
||||
pub label: String,
|
||||
#[serde(default = "default_captcha_required_feedback")]
|
||||
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.
|
||||
#[derive(Deserialize)]
|
||||
pub struct Strings {
|
||||
#[serde(default)]
|
||||
pub struct Strings {
|
||||
pub description: String,
|
||||
#[serde(default = "default_title")]
|
||||
pub title: String,
|
||||
#[serde(default = "default_optional")]
|
||||
pub optional: String,
|
||||
#[serde(default = "default_submit")]
|
||||
pub submit: String,
|
||||
#[serde(default = "default_success")]
|
||||
pub success: String,
|
||||
#[serde(default = "default_message_from")]
|
||||
pub message_from: String,
|
||||
pub name_field: NameField,
|
||||
pub email_field: EmailField,
|
||||
pub captcha_field: CaptchaField,
|
||||
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)]
|
||||
pub struct StateConfig {
|
||||
/// The path prefix of all routes.
|
||||
|
@ -152,10 +149,13 @@ pub struct StateConfig {
|
|||
pub lang: String,
|
||||
pub strings: Strings,
|
||||
}
|
||||
|
||||
fn default_socket_address() -> String {
|
||||
"0.0.0.0:80".to_string()
|
||||
fn default_path_prefix() -> String {
|
||||
"/".to_string()
|
||||
}
|
||||
fn default_lang() -> String {
|
||||
"en".to_string()
|
||||
}
|
||||
|
||||
/// Configuration.
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
|
@ -167,6 +167,9 @@ pub struct Config {
|
|||
#[serde(flatten)]
|
||||
pub state_config: StateConfig,
|
||||
}
|
||||
fn default_socket_address() -> String {
|
||||
"0.0.0.0:80".to_string()
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Parses the configuration in the given data directory.
|
||||
|
|
Loading…
Reference in a new issue