Permalink
Browse files

refactor(settings): implement Default in enum_boilerplate!

  • Loading branch information...
philbooth committed Nov 29, 2018
1 parent 0a02f99 commit a9c9a1bf6fbde83c257f808bc42b73d567cd6ab9
Showing with 11 additions and 29 deletions.
  1. +1 −7 src/types/env/mod.rs
  2. +2 −14 src/types/logging/mod.rs
  3. +7 −1 src/types/mod.rs
  4. +1 −7 src/types/provider/mod.rs
@@ -11,14 +11,8 @@ use serde::de::Error;

use types::error::{AppError, AppErrorKind};

enum_boilerplate!(Env ("env", InvalidEnv) {
enum_boilerplate!(Env ("env", Dev, InvalidEnv) {
Dev => "dev",
Prod => "production",
Test => "test",
});

impl Default for Env {
fn default() -> Self {
Env::Dev
}
}
@@ -11,26 +11,14 @@ use serde::de::Error;

use types::error::{AppError, AppErrorKind};

enum_boilerplate!(LogLevel ("log level", InvalidLogLevel) {
enum_boilerplate!(LogLevel ("log level", Normal, InvalidLogLevel) {
Normal => "normal",
Debug => "debug",
Critical => "critical",
Off => "off",
});

impl Default for LogLevel {
fn default() -> Self {
LogLevel::Normal
}
}

enum_boilerplate!(LogFormat ("log format", InvalidLogFormat) {
enum_boilerplate!(LogFormat ("log format", Mozlog, InvalidLogFormat) {
Mozlog => "mozlog",
Pretty => "pretty",
});

impl Default for LogFormat {
fn default() -> Self {
LogFormat::Mozlog
}
}
@@ -7,7 +7,7 @@
//! miscellaneous generally-used types.

macro_rules! enum_boilerplate {
($name:ident ($description:expr, $error:ident) {
($name:ident ($description:expr, $default:ident, $error:ident) {
$($variant:ident => $serialization:expr,)+
}) => {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
@@ -25,6 +25,12 @@ macro_rules! enum_boilerplate {
}
}

impl std::default::Default for $name {
fn default() -> Self {
$name::$default
}
}

impl std::fmt::Display for $name {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "{}", self.as_ref())
@@ -11,16 +11,10 @@ use serde::de::Error;

use types::error::{AppError, AppErrorKind};

enum_boilerplate!(Provider ("env", InvalidPayload) {
enum_boilerplate!(Provider ("env", Ses, InvalidPayload) {
Mock => "mock",
Sendgrid => "sendgrid",
Ses => "ses",
Smtp => "smtp",
SocketLabs => "socketlabs",
});

impl Default for Provider {
fn default() -> Self {
Provider::Ses
}
}

0 comments on commit a9c9a1b

Please sign in to comment.