Skip to content

Commit db213d2

Browse files
committed
Implement display for Scheme and Color structs
1 parent 688217e commit db213d2

File tree

6 files changed

+74
-28
lines changed

6 files changed

+74
-28
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tinted-builder-rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ strip-ansi-escapes = "0.2.0"
2424

2525
[dependencies.tinted-builder]
2626
path = "../tinted-builder"
27-
version = "0.4.0"
27+
version = "0.4.1"
2828

2929
[[bin]]
3030
name = "tinted-builder-rust"

tinted-builder/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 0.4.1 - 2024-06-15
4+
5+
## Fixed
6+
7+
- Implement `Display` trait for `Scheme` and `Color`
8+
`Scheme` themselves.
9+
310
## 0.4.0 - 2024-06-15
411

512
## Added

tinted-builder/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "tinted-builder"
33
description = "A Tinted Theming template builder which uses yaml color schemes to generate theme files."
4-
version = "0.4.0"
4+
version = "0.4.1"
55
edition = "2021"
66
license = "MIT OR Apache-2.0"
77
readme = "README.md"

tinted-builder/src/scheme.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod color;
22

33
use regex::Regex;
44
use serde::{Deserialize, Deserializer};
5-
use std::collections::HashMap;
5+
use std::{collections::HashMap, fmt};
66

77
use crate::constants::{REQUIRED_BASE16_PALETTE_KEYS, REQUIRED_BASE24_PALETTE_KEYS};
88

@@ -30,6 +30,33 @@ pub struct Scheme {
3030
pub palette: HashMap<String, Color>,
3131
}
3232

33+
impl fmt::Display for Scheme {
34+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35+
writeln!(f, "author: {}", self.author)?;
36+
if let Some(ref desc) = self.description {
37+
writeln!(f, "description: {}", desc)?;
38+
}
39+
writeln!(f, "name: {}", self.name)?;
40+
writeln!(f, "slug: {}", self.slug)?;
41+
writeln!(f, "system: {}", self.system)?;
42+
writeln!(f, "variant: {}", self.variant)?;
43+
writeln!(f, "palette:")?;
44+
45+
let mut palette_vec: Vec<(String, Color)> = self
46+
.palette
47+
.clone()
48+
.iter()
49+
.map(|(k, v)| (k.to_string(), v.clone()))
50+
.collect();
51+
palette_vec.sort_by_key(|k| k.0.clone());
52+
53+
for (key, value) in palette_vec {
54+
writeln!(f, " {}: {}", key, value)?;
55+
}
56+
Ok(())
57+
}
58+
}
59+
3360
pub(crate) fn slugify(input: &str) -> String {
3461
let char_map: HashMap<char, &str> = [
3562
('á', "a"),

tinted-builder/src/scheme/color.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
1+
use std::fmt;
2+
13
use anyhow::{Context, Result};
24

3-
#[derive(Debug)]
5+
#[derive(Debug, Clone)]
46
pub struct Color {
57
pub hex: (String, String, String),
68
pub rgb: (u8, u8, u8),
79
pub dec: (f32, f32, f32),
810
}
911

12+
impl Color {
13+
pub fn new(hex_color: String) -> Result<Color> {
14+
let hex_full = match process_hex_input(&hex_color) {
15+
Some(valid_hex) => valid_hex,
16+
None => {
17+
anyhow::bail!("Provided hex value is not formatted correctly");
18+
}
19+
};
20+
21+
let hex: (String, String, String) = (
22+
hex_full[0..2].to_lowercase(),
23+
hex_full[2..4].to_lowercase(),
24+
hex_full[4..6].to_lowercase(),
25+
);
26+
let rgb = hex_to_rgb(&hex)
27+
.unwrap_or_else(|_| panic!("Unable to convert hex value to rgb: {}", hex_full));
28+
let (r, g, b) = rgb;
29+
let dec: (f32, f32, f32) = (r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0);
30+
31+
Ok(Color { hex, rgb, dec })
32+
}
33+
34+
pub fn to_hex(&self) -> String {
35+
format!("#{}{}{}", &self.hex.0, &self.hex.1, &self.hex.2)
36+
}
37+
}
38+
39+
impl fmt::Display for Color {
40+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41+
write!(f, "{}", &self.to_hex())
42+
}
43+
}
44+
1045
fn hex_to_rgb(hex: &(String, String, String)) -> Result<(u8, u8, u8)> {
1146
let r = u8::from_str_radix(hex.0.as_str(), 16)
1247
.context("Invalid hex character for red component")?;
@@ -48,26 +83,3 @@ fn process_hex_input(input: &str) -> Option<String> {
4883
_ => None,
4984
}
5085
}
51-
52-
impl Color {
53-
pub fn new(hex_color: String) -> Result<Color> {
54-
let hex_full = match process_hex_input(&hex_color) {
55-
Some(valid_hex) => valid_hex,
56-
None => {
57-
anyhow::bail!("Provided hex value is not formatted correctly");
58-
}
59-
};
60-
61-
let hex: (String, String, String) = (
62-
hex_full[0..2].to_lowercase(),
63-
hex_full[2..4].to_lowercase(),
64-
hex_full[4..6].to_lowercase(),
65-
);
66-
let rgb = hex_to_rgb(&hex)
67-
.unwrap_or_else(|_| panic!("Unable to convert hex value to rgb: {}", hex_full));
68-
let (r, g, b) = rgb;
69-
let dec: (f32, f32, f32) = (r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0);
70-
71-
Ok(Color { hex, rgb, dec })
72-
}
73-
}

0 commit comments

Comments
 (0)