Skip to content

Commit

Permalink
cargo fmt and cargo clippy fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsky committed Jun 4, 2022
1 parent c14cd10 commit e6d033a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 37 deletions.
102 changes: 78 additions & 24 deletions egui-theme/src/tests/ser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui::{Style, TextStyle, FontId, FontFamily, FontData, FontDefinitions};
use egui::{FontData, FontDefinitions, FontFamily, FontId, Style, TextStyle};

use crate::EguiTheme;
#[test]
Expand Down Expand Up @@ -148,42 +148,96 @@ fn test_text_style() {
vec![FONT_NAME.to_owned()],
);

fonts.families.insert(FontFamily::Name("NacelleFontFamily".into()), vec![FONT_NAME.to_owned()]);
style.text_styles.insert(TextStyle::Name("NacelleStyle".into()), FontId::new(12.0, FontFamily::Name("NacelleFontFamily".into())));

fonts.families.insert(
FontFamily::Name("NacelleFontFamily".into()),
vec![FONT_NAME.to_owned()],
);
style.text_styles.insert(
TextStyle::Name("NacelleStyle".into()),
FontId::new(12.0, FontFamily::Name("NacelleFontFamily".into())),
);

let theme = EguiTheme::new(style, fonts);
let serialized = serde_json::to_string(&theme).expect("serialization failed");
assert!(serialized.contains("NacelleFontData"), "Nacelle Font Data was not serialized correctly");
assert!(serialized.contains("NacelleFontFamily"), "FontFamily was not serialized correctly");
assert!(serialized.contains("NacelleStyle"), "TextStyle was not serialized correctly");

let deserialized = serde_json::from_str::<EguiTheme>(serialized.as_str()).expect("deserialization failed");
assert!(
serialized.contains("NacelleFontData"),
"Nacelle Font Data was not serialized correctly"
);
assert!(
serialized.contains("NacelleFontFamily"),
"FontFamily was not serialized correctly"
);
assert!(
serialized.contains("NacelleStyle"),
"TextStyle was not serialized correctly"
);

let deserialized =
serde_json::from_str::<EguiTheme>(serialized.as_str()).expect("deserialization failed");
let (de_style, _fonts) = deserialized.extract();
assert!(de_style.text_styles().contains(&TextStyle::Body), "text style `Body` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Small), "text style `Small` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Monospace), "text style `Monospace` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Button), "text style `Button` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Heading), "text style `Heading` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Name("NacelleStyle".into())), "text style `NacelleStyle` does not exist");
assert!(de_style.text_styles.get(&TextStyle::Name("NacelleStyle".into())).is_some(), "could not get the text_style");
assert_eq!(*de_style.text_styles.get(&TextStyle::Name("NacelleStyle".into())).unwrap(), FontId::new(12.0, FontFamily::Name("NacelleFontFamily".into())), "FontStyle not deserialized");
assert!(
de_style.text_styles().contains(&TextStyle::Body),
"text style `Body` does not exist"
);
assert!(
de_style.text_styles().contains(&TextStyle::Small),
"text style `Small` does not exist"
);
assert!(
de_style.text_styles().contains(&TextStyle::Monospace),
"text style `Monospace` does not exist"
);
assert!(
de_style.text_styles().contains(&TextStyle::Button),
"text style `Button` does not exist"
);
assert!(
de_style.text_styles().contains(&TextStyle::Heading),
"text style `Heading` does not exist"
);
assert!(
de_style
.text_styles()
.contains(&TextStyle::Name("NacelleStyle".into())),
"text style `NacelleStyle` does not exist"
);
assert!(
de_style
.text_styles
.get(&TextStyle::Name("NacelleStyle".into()))
.is_some(),
"could not get the text_style"
);
assert_eq!(
*de_style
.text_styles
.get(&TextStyle::Name("NacelleStyle".into()))
.unwrap(),
FontId::new(12.0, FontFamily::Name("NacelleFontFamily".into())),
"FontStyle not deserialized"
);
}
#[test]
fn test_colors() {
let mut style = Style::default();
let fg_stroke = egui::Stroke::new(1f32, egui::Color32::TRANSPARENT);
style.visuals.widgets.noninteractive.fg_stroke = fg_stroke.clone();
style.visuals.widgets.inactive.bg_fill = egui::Color32::LIGHT_RED;

let theme = EguiTheme::new(style, FontDefinitions::default());
let serialized = serde_json::to_string(&theme).expect("serialization failed");
let deserialized = serde_json::from_str::<EguiTheme>(serialized.as_str()).expect("deserialization failed");
let serialized = serde_json::to_string(&theme).expect("serialization failed");
let deserialized =
serde_json::from_str::<EguiTheme>(serialized.as_str()).expect("deserialization failed");
let (de_style, _fonts) = deserialized.extract();

assert_eq!(
de_style.visuals.widgets.noninteractive.fg_stroke, fg_stroke.clone(), "stroke doesn't match"
de_style.visuals.widgets.noninteractive.fg_stroke,
fg_stroke.clone(),
"stroke doesn't match"
);
assert_eq!(
de_style.visuals.widgets.inactive.bg_fill, egui::Color32::LIGHT_RED, "Color doesn't match"
de_style.visuals.widgets.inactive.bg_fill,
egui::Color32::LIGHT_RED,
"Color doesn't match"
);
}
}
38 changes: 25 additions & 13 deletions egui-theme/src/theme/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ use std::collections::HashMap;

const TEXT_STYLES_KEY: &str = "text_styles";

// TODO: Change the println! to a proper logging crate.
macro_rules! ser {
($collection:ident, $style:ident, $prop:ident) => {
match serde_json::to_value($style.$prop.to_owned()) {
Ok(value) => { let _ = $collection.insert(stringify!($prop).to_owned(), value); },
Err(error) => { println!("{}", error); },
}
Ok(value) => {
let _ = $collection.insert(stringify!($prop).to_owned(), value);
}
Err(error) => {
println!("{}", error);
}
}
};
($collection:ident, $style:ident, $prop:ident, $sub_prop:ident) => {
match serde_json::to_value($style.$prop.$sub_prop.to_owned()) {
Ok(value) => { let _ = $collection.insert(stringify!($prop.$sub_prop).to_owned(), value); },
Err(error) => { println!("{}", error); },
match serde_json::to_value($style.$prop.$sub_prop.to_owned()) {
Ok(value) => {
let _ = $collection.insert(stringify!($prop.$sub_prop).to_owned(), value);
}
Err(error) => {
println!("{}", error);
}
}
};
}
Expand Down Expand Up @@ -42,21 +51,22 @@ pub fn from_style(style: Style) -> HashMap<String, super::ThemeValue> {
let mut hash_map = HashMap::new();

{
let text_styles = style.text_styles.iter()
.map(|(k, v)| (k.to_owned(), v.to_owned() ))
let text_styles = style
.text_styles
.iter()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect::<Vec<_>>();
if let Ok(value) = serde_json::to_value(text_styles) {
hash_map.insert(TEXT_STYLES_KEY.to_owned(), value);
}
}
// Text Styles are a special case due to being a map that must serialize to a string.
// ser!(hash_map, style, text_styles);

ser!(hash_map, style, override_text_style);
ser!(hash_map, style, override_font_id);
ser!(hash_map, style, wrap);


ser!(hash_map, style, animation_time);
ser!(hash_map, style, explanation_tooltips);

Expand Down Expand Up @@ -103,13 +113,15 @@ pub fn to_style(hash_map: HashMap<String, super::ThemeValue>) -> Style {
let mut style = Style::default();
// Special case due to json requiring String keys
{
hash_map.get(TEXT_STYLES_KEY).map(|value| {
if let Ok(values) = serde_json::from_value::<Vec<(egui::TextStyle, egui::FontId)>>(value.to_owned()) {
if let Some(value) = hash_map.get(TEXT_STYLES_KEY) {
if let Ok(values) =
serde_json::from_value::<Vec<(egui::TextStyle, egui::FontId)>>(value.to_owned())
{
for (key, value) in values {
style.text_styles.insert(key, value);
}
}
});
}
}

de!(hash_map, style, override_text_style);
Expand Down

0 comments on commit e6d033a

Please sign in to comment.