Skip to content

Commit

Permalink
refactor: preserve order with indexmap
Browse files Browse the repository at this point in the history
  • Loading branch information
sgoudham committed Dec 16, 2023
1 parent a3c1c1f commit 3876f71
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
7 changes: 5 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion whiskers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ path = "src/main.rs"
[dependencies]
base64 = "0.21"
catppuccin = { version = "1.3", features = ["css"] }
indexmap = { version = "2.1.0", features = ["serde", "std"] }
clap = { version = "4.4", features = ["derive"] }
clap-stdin = "0.2"
color-eyre = { version = "0.6", default-features = false }
css-colors = "1.0"
handlebars = "4.5"
regex = "1.10"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_json = { version = "1.0", features = ["preserve_order"] }
serde_yaml = "0.9"
tempfile = "3.8"
thiserror = "1.0"
Expand Down
10 changes: 6 additions & 4 deletions whiskers/examples/nested.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
---
accent: {{mauve}}
accent: {{flavors.frappe.mauve}}
---

How do we want to handle frontmatter?
It makes sense when a single flavour is being rendered, but not when multiple are... at the minute anyways.

{{ #each flavors as | flavor flavour | }}
## {{titlecase flavour}}
Base: {{base}}
{{#each flavors as | flavor flavour | }}
- **{{flavour}}**
{{ #each colors as | color colorName | }}
- **{{colorName}}**
{{/each}}
{{/each}}
60 changes: 53 additions & 7 deletions whiskers/src/template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use handlebars::{Handlebars, HelperDef};
use handlebars::Handlebars;
use handlebars::HelperDef;
use indexmap::IndexMap;

use crate::helper;

Expand Down Expand Up @@ -182,25 +182,71 @@ pub fn make_registry() -> Handlebars<'static> {
reg
}

fn flavor_priority(flavor: &str) -> u32 {
match flavor {
"latte" => 1,
"frappe" => 2,
"macchiato" => 3,
"mocha" => 4,
_ => unreachable!(),
}
}

fn color_priority(color: &str) -> u32 {
match color {
"rosewater" => 1,
"flamingo" => 2,
"pink" => 3,
"mauve" => 4,
"red" => 5,
"maroon" => 6,
"peach" => 7,
"yellow" => 8,
"green" => 9,
"teal" => 10,
"sky" => 11,
"sapphire" => 12,
"blue" => 13,
"lavender" => 14,
"text" => 15,
"subtext1" => 16,
"subtext0" => 17,
"overlay2" => 18,
"overlay1" => 19,
"overlay0" => 20,
"surface2" => 21,
"surface1" => 22,
"surface0" => 23,
"base" => 24,
"mantle" => 25,
"crust" => 26,
_ => unreachable!(),
}
}

#[must_use]
#[allow(clippy::missing_panics_doc)] // panic here implies an internal issue
pub fn make_context_all() -> serde_json::Value {
let flavours: HashMap<&str, serde_json::Value> = catppuccin::Flavour::into_iter()
let mut flavours: IndexMap<&str, serde_json::Value> = catppuccin::Flavour::into_iter()
.map(|f| (f.name(), make_context(f)))
.collect();
let flavours_map = HashMap::from([("flavors", flavours)]);
serde_json::to_value(flavours_map).expect("flavours can be serialized")
flavours.sort_by(|a, _, b, _| flavor_priority(a).cmp(&flavor_priority(b)));

let context = IndexMap::from([("flavors", flavours)]);

serde_json::to_value(context).expect("flavours can be serialized")
}

#[must_use]
#[allow(clippy::missing_panics_doc)] // panic here implies an internal issue
pub fn make_context(flavor: catppuccin::Flavour) -> serde_json::Value {
let colors = flavor.colours();

let color_map: HashMap<String, String> = colors
let mut color_map: IndexMap<String, String> = colors
.into_fields_iter()
.map(|(name, c)| (name.to_string(), c.hex().to_ascii_lowercase()))
.collect();
color_map.sort_by(|a, _, b, _| color_priority(a).cmp(&color_priority(b)));

let mut context =
serde_json::to_value(color_map.clone()).expect("color names & hexcodes can be serialized");
Expand Down

0 comments on commit 3876f71

Please sign in to comment.