Skip to content

Commit

Permalink
feat: clean up codegen & include output in repo
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy committed Oct 24, 2023
1 parent 53b3bda commit 492a2b2
Show file tree
Hide file tree
Showing 3 changed files with 866 additions and 41 deletions.
91 changes: 52 additions & 39 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
collections::HashMap,
env,
error::Error,
fs::{self, File},
io::{BufReader, BufWriter, Write},
Expand All @@ -12,7 +11,7 @@ use serde::Deserialize;
const PALETTE_URL: &str =
"https://raw.githubusercontent.com/catppuccin/palette/v0.2.0/palette-porcelain.json";
const PALETTE_PATH: &str = ".cache/palette.json";
const CODEGEN_PATH: &str = "codegen.rs";
const CODEGEN_PATH: &str = "./src/palette.rs";

#[derive(Debug, Deserialize)]
struct Color {
Expand Down Expand Up @@ -47,7 +46,16 @@ fn is_accent(name: &str) -> bool {
.contains(&name)
}

const HEADER: &str = "// DO NOT MODIFY THIS FILE!
// The contents of this file are generated by build.rs automatically.
// Any changes you make here WILL be overwritten.
#![allow(clippy::unreadable_literal)]
use crate::{Palette, Flavor, Color};
";

fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed={}", PALETTE_PATH);

let palette_path = Path::new(PALETTE_PATH);

if !palette_path.exists() {
Expand All @@ -57,53 +65,24 @@ fn main() -> Result<(), Box<dyn Error>> {

let palette: Palette = serde_json::from_reader(BufReader::new(File::open(palette_path)?))?;

let codegen_path = Path::new(&env::var("OUT_DIR")?).join(CODEGEN_PATH);
let codegen_file = BufWriter::new(File::create(&codegen_path)?);
let codegen_file = BufWriter::new(File::create(CODEGEN_PATH)?);
let mut code_writer = BufWriter::new(codegen_file);

write!(&mut code_writer, "{HEADER}")?;

let mut flavors_map = phf_codegen::Map::new();
for (flavor_name, flavor) in palette.into_iter() {
let mut colors_map = phf_codegen::Map::new();
for (color_name, color) in flavor.into_iter() {
colors_map.entry(
color_name.clone(),
&format!(
r#"Color {{
name: "{}",
is_accent: {},
hex: "{}",
rgb: &[{}, {}, {}],
hsl: &[{}.0, {}, {}],
}}"#,
color_name,
is_accent(&color_name),
color.hex,
color.rgb[0],
color.rgb[1],
color.rgb[2],
color.hsl[0],
color.hsl[1],
color.hsl[2]
),
);
let color_entry = make_color_entry(&color_name, color);
colors_map.entry(color_name, &color_entry);
}
flavors_map.entry(
flavor_name.clone(),
&format!(
r#"Flavor {{
name: "{}",
dark: {},
colors: {},
}}"#,
flavor_name,
is_dark(&flavor_name),
colors_map.build()
),
);
let flavor_entry = make_flavor_entry(&flavor_name, colors_map);
flavors_map.entry(flavor_name, &flavor_entry);
}
writeln!(
&mut code_writer,
"pub static PALETTE: Palette = Palette {{ flavors: {} }};",
"pub static PALETTE: Palette = Palette {{\n flavors: {}\n}};",
flavors_map.build()
)?;

Expand All @@ -118,3 +97,37 @@ fn download_palette(path: &Path) -> Result<(), Box<dyn Error>> {
fs::write(path, contents)?;
Ok(())
}

fn make_color_entry(name: &str, color: Color) -> String {
format!(
r#"Color {{
name: "{}",
is_accent: {},
hex: "{}",
rgb: &[{}, {}, {}],
hsl: &[{}.0, {}, {}],
}}"#,
name,
is_accent(name),
color.hex,
color.rgb[0],
color.rgb[1],
color.rgb[2],
color.hsl[0],
color.hsl[1],
color.hsl[2]
)
}

fn make_flavor_entry(name: &str, colors_map: phf_codegen::Map<String>) -> String {
format!(
r#"Flavor {{
name: "{}",
dark: {},
colors: {},
}}"#,
name,
is_dark(name),
colors_map.build()
)
}
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::unwrap_used)]

mod palette;
pub use palette::PALETTE;

#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
pub struct Color {
pub name: &'static str,
Expand All @@ -21,8 +24,6 @@ pub struct Palette {
pub flavors: phf::Map<&'static str, Flavor>,
}

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

static FLAVORS_ORDER: &[&str] = &["latte", "frappe", "macchiato", "mocha"];
static COLOURS_ORDER: &[&str] = &[
"rosewater",
Expand Down
Loading

0 comments on commit 492a2b2

Please sign in to comment.