From a9ee3c20fe09aec8bdd7092d4b16c83dac12ccd6 Mon Sep 17 00:00:00 2001 From: Bauke Date: Thu, 18 Jan 2024 14:32:14 +0100 Subject: [PATCH] Derive Clone and implement From for automatic Clap parsing. --- hooked-config/source/config/noise_level.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/hooked-config/source/config/noise_level.rs b/hooked-config/source/config/noise_level.rs index afc3b7f..659d0d1 100644 --- a/hooked-config/source/config/noise_level.rs +++ b/hooked-config/source/config/noise_level.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; /// The noise level Hooked should output logs with. -#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(rename_all = "snake_case")] pub enum NoiseLevel { /// Output only errors. @@ -22,3 +22,16 @@ impl Default for NoiseLevel { Self::Standard } } + +// Implement `From` so we can use Clap's automatic parsing in the CLI. +impl From for NoiseLevel { + fn from(value: String) -> Self { + match value.to_lowercase().as_str() { + "quiet" => Self::Quiet, + "loud" => Self::Loud, + "standard" => Self::Standard, + "minimal" => Self::Minimal, + _ => NoiseLevel::default(), + } + } +}