-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make 'cli_colors' an enum, add "always" option.
Resolves #2649.
- Loading branch information
1 parent
1b089bd
commit f4e8987
Showing
6 changed files
with
212 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use core::fmt; | ||
use serde::{ | ||
de::{self, Unexpected::{Signed, Str}}, | ||
Deserialize, Serialize | ||
}; | ||
|
||
/// Configure color output for logging. | ||
#[derive(Clone, Serialize, PartialEq, Debug, Default)] | ||
pub enum CliColors { | ||
/// Always use colors in logs. | ||
Always, | ||
|
||
/// Use colors in logs if the terminal supports it. | ||
#[default] | ||
Auto, | ||
|
||
/// Never use colors in logs. | ||
Never | ||
} | ||
|
||
impl fmt::Display for CliColors { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match *self { | ||
CliColors::Always => write!(f, "always"), | ||
CliColors::Auto => write!(f, "auto"), | ||
CliColors::Never => write!(f, "never") | ||
} | ||
} | ||
} | ||
|
||
|
||
impl<'de> Deserialize<'de> for CliColors { | ||
fn deserialize<D: de::Deserializer<'de>>(de: D) -> Result<Self, D::Error> { | ||
struct Visitor; | ||
|
||
impl<'de> de::Visitor<'de> for Visitor { | ||
type Value = CliColors; | ||
|
||
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str("0, 1, false, true, always, auto, never") | ||
} | ||
|
||
fn visit_str<E: de::Error>(self, val: &str) -> Result<CliColors, E> { | ||
match val.to_lowercase().as_str() { | ||
"true" => Ok(CliColors::Auto), | ||
"false" => Ok(CliColors::Never), | ||
"1" => Ok(CliColors::Auto), | ||
"0" => Ok(CliColors::Never), | ||
"auto" => Ok(CliColors::Auto), | ||
"never" => Ok(CliColors::Never), | ||
"always" => Ok(CliColors::Always), | ||
_ => Err(E::invalid_value( | ||
Str(val), | ||
&"0, 1, false, true, always, auto, never", | ||
)) | ||
} | ||
} | ||
|
||
fn visit_bool<E: de::Error>(self, val: bool) -> Result<CliColors, E> { | ||
match val { | ||
true => Ok(CliColors::Auto), | ||
false => Ok(CliColors::Never) | ||
} | ||
} | ||
|
||
fn visit_i64<E: de::Error>(self, val: i64) -> Result<CliColors, E> { | ||
match val { | ||
0 => Ok(CliColors::Never), | ||
1 => Ok(CliColors::Auto), | ||
_ => Err(E::invalid_value( | ||
Signed(val), | ||
&"0, 1, false, true, always, auto, never", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
de.deserialize_any(Visitor) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters