From b73982a6fc824eed02c8d8345a887f070eaf807a Mon Sep 17 00:00:00 2001 From: InioX Date: Tue, 27 Aug 2024 22:39:03 +0200 Subject: [PATCH] chore: format code --- benches/template.rs | 45 +++++++++++++++++++---------------- src/scheme.rs | 12 +++------- src/template.rs | 10 ++++++-- src/template_util/mod.rs | 2 +- src/template_util/template.rs | 38 ++++++++++++++++++++++++----- src/util/color.rs | 2 +- 6 files changed, 69 insertions(+), 40 deletions(-) diff --git a/benches/template.rs b/benches/template.rs index a9e349c..f52abb0 100644 --- a/benches/template.rs +++ b/benches/template.rs @@ -1,7 +1,10 @@ -use std::hint::black_box; use criterion::{criterion_group, criterion_main, Criterion}; +use std::hint::black_box; -use matugen::{scheme::{get_custom_color_schemes, get_schemes, SchemesEnum}, template_util::template::{self, get_render_data, render_template}}; +use matugen::{ + scheme::{get_custom_color_schemes, get_schemes, SchemesEnum}, + template_util::template::{self, get_render_data, render_template}, +}; use template::add_engine_filters; use upon::{Engine, Syntax}; @@ -14,37 +17,37 @@ fn parse_template(data: &str) { add_engine_filters(&mut engine); let (scheme_dark, scheme_light) = get_schemes(source_color, &None, &None); - let schemes = get_custom_color_schemes( - source_color, - scheme_dark, - scheme_light, - &None, - &None, - &None - ); - let render_data = get_render_data(&schemes, &source_color,&SchemesEnum::Dark, &None, None).unwrap(); - - engine.add_template("a", data.repeat(50)).expect("failed to add template"); - render_template(&engine, &"a".to_string(), &render_data, None).expect("failed to render template"); + let schemes = + get_custom_color_schemes(source_color, scheme_dark, scheme_light, &None, &None, &None); + let render_data = + get_render_data(&schemes, &source_color, &SchemesEnum::Dark, &None, None).unwrap(); + + engine + .add_template("a", data.repeat(50)) + .expect("failed to add template"); + render_template(&engine, &"a".to_string(), &render_data, None) + .expect("failed to render template"); } fn criterion_benchmark(c: &mut Criterion) { - let data = - r#" + let data = r#" <* for name, value in colors *> {{name}} {{value.default.rgba}}; <* endfor *> "#; - let data_filter = - r#" + let data_filter = r#" <* for name, value in colors *> {{name | replace: "_", "-" }} {{value.default.rgba | set_alpha: 0.7 | set_hue: -180.0 }}; <* endfor *> "#; - c.bench_function("parse 20", |b| b.iter(|| parse_template(black_box(&data.repeat(20))))); - c.bench_function("parse 20 filters", |b| b.iter(|| parse_template(black_box(&data_filter.repeat(20))))); + c.bench_function("parse 20", |b| { + b.iter(|| parse_template(black_box(&data.repeat(20)))) + }); + c.bench_function("parse 20 filters", |b| { + b.iter(|| parse_template(black_box(&data_filter.repeat(20)))) + }); } criterion_group!(benches, criterion_benchmark); -criterion_main!(benches); \ No newline at end of file +criterion_main!(benches); diff --git a/src/scheme.rs b/src/scheme.rs index 6d34f12..01a6633 100644 --- a/src/scheme.rs +++ b/src/scheme.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; -use std::collections::BTreeSet; use material_colors::scheme::Scheme; +use std::collections::BTreeSet; use crate::color::color::{generate_dynamic_scheme, make_custom_color, OwnCustomColor}; @@ -115,7 +115,6 @@ pub fn get_schemes( (scheme_dark, scheme_light) } - #[cfg(test)] mod tests { use super::*; @@ -125,12 +124,7 @@ mod tests { fn schemes_eq() { let source_color = material_colors::color::Argb::new(255, 255, 0, 0); assert_eq!( - Scheme::from(generate_dynamic_scheme( - &None, - source_color, - true, - None, - )).primary, + Scheme::from(generate_dynamic_scheme(&None, source_color, true, None,)).primary, Argb { alpha: 255, red: 255, @@ -139,4 +133,4 @@ mod tests { } ); } -} \ No newline at end of file +} diff --git a/src/template.rs b/src/template.rs index 1dd21ff..0bb64c4 100644 --- a/src/template.rs +++ b/src/template.rs @@ -90,7 +90,13 @@ impl Template { Source::Color { .. } => None, }; - let mut render_data = get_render_data(schemes, source_color, default_scheme, custom_keywords, image)?; + let mut render_data = get_render_data( + schemes, + source_color, + default_scheme, + custom_keywords, + image, + )?; for (i, (name, template)) in templates.iter().enumerate() { let (input_path_absolute, output_path_absolute) = @@ -256,4 +262,4 @@ fn export_template( ); Ok(()) -} \ No newline at end of file +} diff --git a/src/template_util/mod.rs b/src/template_util/mod.rs index 01806cb..612b5b9 100644 --- a/src/template_util/mod.rs +++ b/src/template_util/mod.rs @@ -1 +1 @@ -pub mod template; \ No newline at end of file +pub mod template; diff --git a/src/template_util/template.rs b/src/template_util/template.rs index 49baded..65cb39f 100644 --- a/src/template_util/template.rs +++ b/src/template_util/template.rs @@ -5,13 +5,16 @@ use colorsys::{ColorAlpha, Hsl}; use material_colors::color::Argb; use upon::{Engine, Value}; +use crate::color::format::{ + format_hex, format_hex_stripped, format_hsl, format_hsla, format_rgb, format_rgba, + rgb_from_argb, +}; use crate::filters::alpha::set_alpha; use crate::filters::grayscale::grayscale; use crate::filters::hue::set_hue; use crate::filters::invert::invert; use crate::filters::lightness::set_lightness; use crate::scheme::{Schemes, SchemesEnum}; -use crate::color::format::{format_hex, format_hex_stripped, format_hsl, format_hsla, format_rgb, format_rgba, rgb_from_argb}; #[derive(serde::Serialize, serde::Deserialize, Debug)] pub struct Color { @@ -53,7 +56,12 @@ pub fn add_engine_filters(engine: &mut Engine) { }); } -pub fn render_template(engine: &Engine, name: &String, render_data: &Value, path: Option<&str>) -> Result { +pub fn render_template( + engine: &Engine, + name: &String, + render_data: &Value, + path: Option<&str>, +) -> Result { let data = engine .template(name) .render(render_data) @@ -71,7 +79,13 @@ pub fn render_template(engine: &Engine, name: &String, render_data: &Value, path Ok(data) } -pub fn get_render_data(schemes: &Schemes, source_color: &Argb, default_scheme: &SchemesEnum, custom_keywords: &Option>, image: Option<&String>) -> Result { +pub fn get_render_data( + schemes: &Schemes, + source_color: &Argb, + default_scheme: &SchemesEnum, + custom_keywords: &Option>, + image: Option<&String>, +) -> Result { let colors = generate_colors(schemes, source_color, default_scheme)?; let mut custom: HashMap = Default::default(); for entry in custom_keywords.iter() { @@ -93,12 +107,24 @@ pub fn generate_colors( for ((field, color_light), (_, color_dark)) in std::iter::zip(&schemes.light, &schemes.dark) { hashmap.insert( field.to_string(), - generate_single_color(field, source_color, default_scheme, *color_light, *color_dark)?, + generate_single_color( + field, + source_color, + default_scheme, + *color_light, + *color_dark, + )?, ); } hashmap.insert( String::from("source_color"), - generate_single_color("source_color", source_color, default_scheme, *source_color, *source_color)?, + generate_single_color( + "source_color", + source_color, + default_scheme, + *source_color, + *source_color, + )?, ); Ok(hashmap) } @@ -148,4 +174,4 @@ fn generate_color_strings(color: Argb) -> Color { lightness: format!("{:?}", &hsl_color.lightness()), saturation: format!("{:?}", &hsl_color.saturation()), } -} \ No newline at end of file +} diff --git a/src/util/color.rs b/src/util/color.rs index 68fb9a6..0d57c38 100644 --- a/src/util/color.rs +++ b/src/util/color.rs @@ -57,7 +57,7 @@ pub fn dump_json(schemes: &Schemes, source_color: &Argb, format: &Format) { let mut colors_normal_light: HashMap<&str, String> = HashMap::new(); let mut colors_normal_dark: HashMap<&str, String> = HashMap::new(); - + for ((field, color_light), (_, color_dark)) in std::iter::zip(&schemes.light, &schemes.dark) { let color_light: Rgb = rgb_from_argb(*color_light); let color_dark: Rgb = rgb_from_argb(*color_dark);