(De)serialize Reset color (#824)

This commit is contained in:
Przemek Pawlas 2024-01-06 13:02:38 +01:00 committed by GitHub
parent cd1780c2d8
commit 94fdd586e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 19 deletions

View File

@ -1,3 +1,8 @@
# Version 0.27.1
## Added ⭐
- Add support for (de)serializing `Reset` `Color`
# Version 0.27 # Version 0.27
## Added ⭐ ## Added ⭐

View File

@ -173,6 +173,7 @@ impl TryFrom<&str> for Color {
let src = src.to_lowercase(); let src = src.to_lowercase();
match src.as_ref() { match src.as_ref() {
"reset" => Ok(Color::Reset),
"black" => Ok(Color::Black), "black" => Ok(Color::Black),
"dark_grey" => Ok(Color::DarkGrey), "dark_grey" => Ok(Color::DarkGrey),
"red" => Ok(Color::Red), "red" => Ok(Color::Red),
@ -223,6 +224,7 @@ impl serde::ser::Serialize for Color {
S: serde::ser::Serializer, S: serde::ser::Serializer,
{ {
let str = match *self { let str = match *self {
Color::Reset => "reset",
Color::Black => "black", Color::Black => "black",
Color::DarkGrey => "dark_grey", Color::DarkGrey => "dark_grey",
Color::Red => "red", Color::Red => "red",
@ -271,7 +273,7 @@ impl<'de> serde::de::Deserialize<'de> for Color {
type Value = Color; type Value = Color;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str( formatter.write_str(
"`black`, `blue`, `dark_blue`, `cyan`, `dark_cyan`, `green`, `dark_green`, `grey`, `dark_grey`, `magenta`, `dark_magenta`, `red`, `dark_red`, `white`, `yellow`, `dark_yellow`, `ansi_(value)`, or `rgb_(r,g,b)` or `#rgbhex`", "`reset`, `black`, `blue`, `dark_blue`, `cyan`, `dark_cyan`, `green`, `dark_green`, `grey`, `dark_grey`, `magenta`, `dark_magenta`, `red`, `dark_red`, `white`, `yellow`, `dark_yellow`, `ansi_(value)`, or `rgb_(r,g,b)` or `#rgbhex`",
) )
} }
fn visit_str<E>(self, value: &str) -> Result<Color, E> fn visit_str<E>(self, value: &str) -> Result<Color, E>
@ -343,6 +345,7 @@ mod tests {
#[test] #[test]
fn test_known_color_conversion() { fn test_known_color_conversion() {
assert_eq!("reset".parse(), Ok(Color::Reset));
assert_eq!("grey".parse(), Ok(Color::Grey)); assert_eq!("grey".parse(), Ok(Color::Grey));
assert_eq!("dark_grey".parse(), Ok(Color::DarkGrey)); assert_eq!("dark_grey".parse(), Ok(Color::DarkGrey));
assert_eq!("red".parse(), Ok(Color::Red)); assert_eq!("red".parse(), Ok(Color::Red));
@ -388,6 +391,14 @@ mod serde_tests {
#[test] #[test]
fn test_deserial_known_color_conversion() { fn test_deserial_known_color_conversion() {
assert_eq!(
serde_json::from_str::<Color>("\"Reset\"").unwrap(),
Color::Reset
);
assert_eq!(
serde_json::from_str::<Color>("\"reset\"").unwrap(),
Color::Reset
);
assert_eq!( assert_eq!(
serde_json::from_str::<Color>("\"Red\"").unwrap(), serde_json::from_str::<Color>("\"Red\"").unwrap(),
Color::Red Color::Red