-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
Info.environment_names
and Info.color_schemes
- Loading branch information
1 parent
7491e88
commit 60cf30c
Showing
3 changed files
with
137 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::fmt; | ||
|
||
use serde::{ | ||
de::{Unexpected, Visitor}, | ||
Deserializer, Serializer, | ||
}; | ||
|
||
const EXPECTING: &str = "an RGBA hex code string"; | ||
|
||
struct HexVisitor; | ||
|
||
impl<'de> Visitor<'de> for HexVisitor { | ||
type Value = u32; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str(EXPECTING) | ||
} | ||
|
||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
let without_hash = v.trim_start_matches('#'); | ||
let parsed = u32::from_str_radix(without_hash, 16) | ||
.map_err(|_| E::invalid_value(Unexpected::Str(&v), &EXPECTING))?; | ||
|
||
Ok(parsed) | ||
} | ||
} | ||
|
||
pub(super) fn serialize<S: Serializer>(v: &u32, serializer: S) -> Result<S::Ok, S::Error> { | ||
let with_0x = format!("{v:#08X}"); | ||
let with_hash = format!("#{}", with_0x.trim_start_matches("0x")); | ||
|
||
serializer.serialize_str(&with_hash) | ||
} | ||
|
||
pub(super) fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u32, D::Error> { | ||
deserializer.deserialize_str(HexVisitor) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn serializes_correctly() { | ||
let mut writer = Vec::new(); | ||
let mut serializer = serde_json::Serializer::new(&mut writer); | ||
|
||
serialize(&0xC81414FF, &mut serializer).unwrap(); | ||
assert_eq!(String::from_utf8(writer).unwrap(), "\"#C81414FF\""); | ||
} | ||
|
||
#[test] | ||
fn deserializes_correctly() { | ||
let mut deserializer = serde_json::Deserializer::from_str("\"#288ED2FF\""); | ||
|
||
assert_eq!(deserialize(&mut deserializer).unwrap(), 0x288ED2FF); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
#![warn(missing_docs)] | ||
|
||
mod hex; | ||
pub mod info; | ||
|
||
pub use self::info::Info; | ||
|