-
Notifications
You must be signed in to change notification settings - Fork 0
v0.2.0 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v0.2.0 #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| pub mod attribute_serde; | ||
| pub mod content_style_serde; | ||
| mod convert; | ||
| pub mod event_serde; | ||
| pub mod event_set_serde; | ||
| pub mod option_content_style_serde; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| use serde::{Deserialize, Deserializer, Serializer}; | ||
|
|
||
| use crate::{ | ||
| crossterm::style::Attribute, | ||
| crossterm_config::convert::{attribute_to_token, parse_attribute_token}, | ||
| error::StyleError, | ||
| }; | ||
|
|
||
| pub fn serialize<S>(attribute: &Attribute, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| { | ||
| let token = attribute_to_token(*attribute).ok_or_else(|| { | ||
| serde::ser::Error::custom(StyleError::UnsupportedAttributeToken { | ||
| token: format!("{attribute:?}"), | ||
| }) | ||
| })?; | ||
|
|
||
| serializer.serialize_str(token) | ||
| } | ||
|
|
||
| pub fn deserialize<'de, D>(deserializer: D) -> Result<Attribute, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| let encoded = String::deserialize(deserializer)?; | ||
| parse_attribute_token(&encoded).map_err(serde::de::Error::custom) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[derive(Deserialize, serde::Serialize)] | ||
| struct Config { | ||
| #[serde(with = "crate::crossterm_config::attribute_serde")] | ||
| attr: Attribute, | ||
| } | ||
|
|
||
| mod deserialize { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn deserializes_lowercase_attribute() { | ||
| let content = r#"attr = "underlined""#; | ||
| let config: Config = toml::from_str(content).unwrap(); | ||
| assert_eq!(config.attr, Attribute::Underlined); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deserializes_case_insensitive_attribute() { | ||
| let content = r#"attr = "NoBold""#; | ||
| let config: Config = toml::from_str(content).unwrap(); | ||
| assert_eq!(config.attr, Attribute::NoBold); | ||
| } | ||
| } | ||
|
|
||
| mod serialize { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn serializes_attribute_to_lowercase_token() { | ||
| let config = Config { | ||
| attr: Attribute::NoBold, | ||
| }; | ||
| let serialized = toml::to_string(&config).unwrap(); | ||
| assert!(serialized.contains("attr = \"nobold\"")); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| mod content_style; | ||
| pub(crate) use content_style::{attribute_to_token, parse_attribute_token}; | ||
| mod event; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -125,7 +125,7 @@ fn color_to_def(color: Color) -> Result<ColorDef, StyleError> { | |||||
| Ok(color_def) | ||||||
| } | ||||||
|
|
||||||
| fn parse_attribute_token(token: &str) -> Result<Attribute, StyleError> { | ||||||
| pub fn parse_attribute_token(token: &str) -> Result<Attribute, StyleError> { | ||||||
| let token = token.to_ascii_lowercase(); | ||||||
|
|
||||||
| let attribute = match token.as_str() { | ||||||
|
|
@@ -167,7 +167,7 @@ fn parse_attribute_token(token: &str) -> Result<Attribute, StyleError> { | |||||
| Ok(attribute) | ||||||
| } | ||||||
|
|
||||||
| fn attribute_to_token(attribute: Attribute) -> Option<&'static str> { | ||||||
| pub fn attribute_to_token(attribute: Attribute) -> Option<&'static str> { | ||||||
|
||||||
| pub fn attribute_to_token(attribute: Attribute) -> Option<&'static str> { | |
| pub(crate) fn attribute_to_token(attribute: Attribute) -> Option<&'static str> { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
|
||
| use crate::{ | ||
| crossterm::style::ContentStyle, | ||
| style::{ | ||
| format::content_style_to_string, parse::parse_content_style, style_def::ContentStyleDef, | ||
| }, | ||
| }; | ||
|
|
||
| pub fn serialize<S>(style: &Option<ContentStyle>, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| { | ||
| let encoded = style | ||
| .as_ref() | ||
| .map(|style| { | ||
| let style_def = ContentStyleDef::try_from(style).map_err(serde::ser::Error::custom)?; | ||
| Ok::<_, S::Error>(content_style_to_string(&style_def)) | ||
| }) | ||
| .transpose()?; | ||
|
|
||
| encoded.serialize(serializer) | ||
| } | ||
|
|
||
| pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<ContentStyle>, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| let encoded = Option::<String>::deserialize(deserializer)?; | ||
|
|
||
| encoded | ||
| .map(|encoded| { | ||
| let style_def = parse_content_style(&encoded).map_err(serde::de::Error::custom)?; | ||
| ContentStyle::try_from(style_def).map_err(serde::de::Error::custom) | ||
| }) | ||
| .transpose() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::crossterm::style::Color; | ||
|
|
||
| #[derive(Deserialize, serde::Serialize)] | ||
| struct Config { | ||
| #[serde(default)] | ||
| #[serde(with = "crate::crossterm_config::option_content_style_serde")] | ||
| style: Option<ContentStyle>, | ||
| } | ||
|
|
||
| mod deserialize { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn deserializes_some_style_string() { | ||
| let content = r#"style = "fg=red,bg=#102030,attr=bold""#; | ||
| let config: Config = toml::from_str(content).unwrap(); | ||
|
|
||
| let style = config.style.expect("style should be Some"); | ||
| assert_eq!(style.foreground_color, Some(Color::Red)); | ||
| assert_eq!( | ||
| style.background_color, | ||
| Some(Color::Rgb { | ||
| r: 0x10, | ||
| g: 0x20, | ||
| b: 0x30, | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deserializes_none_when_field_is_missing() { | ||
| let content = r#""#; | ||
| let config: Config = toml::from_str(content).unwrap(); | ||
| assert!(config.style.is_none()); | ||
| } | ||
| } | ||
|
|
||
| mod serialize { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn serializes_some_style_string() { | ||
| let style = ContentStyle { | ||
| foreground_color: Some(Color::Blue), | ||
| ..ContentStyle::default() | ||
| }; | ||
|
|
||
| let config = Config { style: Some(style) }; | ||
| let serialized = toml::to_string(&config).unwrap(); | ||
|
|
||
| assert!(serialized.contains("fg=blue")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serializes_none_as_absent_value() { | ||
| let config = Config { style: None }; | ||
| let serialized = toml::to_string(&config).unwrap(); | ||
| assert!(!serialized.contains("style")); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||||||||||||||||||||
| use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| use crate::{ | ||||||||||||||||||||||||
| style::{ | ||||||||||||||||||||||||
| format::content_style_to_string, parse::parse_content_style, style_def::ContentStyleDef, | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| termion_config::content_style_serde::ContentStyle, | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| pub fn serialize<S>(style: &Option<ContentStyle>, serializer: S) -> Result<S::Ok, S::Error> | ||||||||||||||||||||||||
| where | ||||||||||||||||||||||||
| S: Serializer, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| let encoded = style | ||||||||||||||||||||||||
| .as_ref() | ||||||||||||||||||||||||
| .map(|style| { | ||||||||||||||||||||||||
| let style_def = ContentStyleDef::try_from(style).map_err(serde::ser::Error::custom)?; | ||||||||||||||||||||||||
| Ok::<_, S::Error>(content_style_to_string(&style_def)) | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
| .transpose()?; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| encoded.serialize(serializer) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<ContentStyle>, D::Error> | ||||||||||||||||||||||||
| where | ||||||||||||||||||||||||
| D: Deserializer<'de>, | ||||||||||||||||||||||||
| { | ||||||||||||||||||||||||
| let encoded = Option::<String>::deserialize(deserializer)?; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| encoded | ||||||||||||||||||||||||
| .map(|encoded| { | ||||||||||||||||||||||||
| let style_def = parse_content_style(&encoded).map_err(serde::de::Error::custom)?; | ||||||||||||||||||||||||
| ContentStyle::try_from(style_def).map_err(serde::de::Error::custom) | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
| .transpose() | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[cfg(test)] | ||||||||||||||||||||||||
| mod tests { | ||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||
| use crate::termion_config::content_style_serde::{Color, Style}; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[derive(Deserialize, serde::Serialize)] | ||||||||||||||||||||||||
| struct Config { | ||||||||||||||||||||||||
| #[serde(default)] | ||||||||||||||||||||||||
| #[serde(with = "crate::termion_config::option_content_style_serde")] | ||||||||||||||||||||||||
| style: Option<ContentStyle>, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| mod deserialize { | ||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||
| fn deserializes_some_style_string() { | ||||||||||||||||||||||||
| let content = r#"style = "fg=red,bg=#102030,attr=bold""#; | ||||||||||||||||||||||||
| let config: Config = toml::from_str(content).unwrap(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let style = config.style.expect("style should be Some"); | ||||||||||||||||||||||||
| assert!(matches!( | ||||||||||||||||||||||||
| style.fg, | ||||||||||||||||||||||||
| Some(crate::termion::color::Fg(Color::Red)) | ||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||
| assert!(matches!( | ||||||||||||||||||||||||
| style.bg, | ||||||||||||||||||||||||
| Some(crate::termion::color::Bg(Color::Rgb(0x10, 0x20, 0x30))) | ||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||
| assert!(style.attributes.iter().any(|s| matches!(s, Style::Bold))); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||
| fn deserializes_none_when_field_is_missing() { | ||||||||||||||||||||||||
| let content = r#""#; | ||||||||||||||||||||||||
| let config: Config = toml::from_str(content).unwrap(); | ||||||||||||||||||||||||
| assert!(config.style.is_none()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| mod serialize { | ||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||||
| fn serializes_some_style_string() { | ||||||||||||||||||||||||
| let style = ContentStyle { | ||||||||||||||||||||||||
| fg: Some(crate::termion::color::Fg(Color::LightBlue)), | ||||||||||||||||||||||||
| bg: None, | ||||||||||||||||||||||||
| attributes: vec![Style::Bold], | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let config = Config { style: Some(style) }; | ||||||||||||||||||||||||
| let serialized = toml::to_string(&config).unwrap(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| assert!(serialized.contains("fg=lightblue")); | ||||||||||||||||||||||||
| assert!(serialized.contains("attr=bold")); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| } | |
| } | |
| #[test] | |
| fn serializes_none_without_style_key() { | |
| let config = Config { style: None }; | |
| let serialized = toml::to_string(&config).unwrap(); | |
| // Ensure that when style is None, the field is omitted entirely | |
| assert!(!serialized.contains("style")); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parse_attribute_tokenis re-exported aspub(crate)fromcrossterm_config::convert. Consider making this functionpub(crate)as well (instead ofpub) to keep the visibility as narrow as possible and avoid accidentally expanding the API surface later.