Skip to content

Commit

Permalink
feat: add method to convert a FlavourColours into a fields iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
backwardspy committed Jul 31, 2023
1 parent 24e5a0b commit 5124eb9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "catppuccin"
version = "1.2.2"
authors = ["backwardspy <backwardspy@gmail.com>"]
version = "1.3.0"
authors = ["Catppuccin Org <releases@catppuccin.com>"]
edition = "2021"
description = "🦀 Soothing pastel theme for Rust."
repository = "https://github.com/catppuccin/rust"
Expand Down
12 changes: 11 additions & 1 deletion src/flavour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,12 @@ impl Flavour {

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use indoc::indoc;

use super::Flavour;
use crate::flavour_colours::validate_colours;
use indoc::indoc;

#[test]
fn verify_colour_methods() {
Expand Down Expand Up @@ -210,6 +213,13 @@ mod tests {
let _crust = Flavour::Latte.crust();
}

#[test]
fn create_hashmap_from_flavour() {
let colours: HashMap<_, _> = Flavour::Mocha.colours().into_fields_iter().collect();
assert_eq!(colours.len(), 26);
assert_eq!(colours["red"], Flavour::Mocha.red());
}

#[test]
fn validate_latte_colours() {
validate_colours(
Expand Down
46 changes: 45 additions & 1 deletion src/flavour_colours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl IntoIterator for FlavourColours {
type IntoIter = std::array::IntoIter<Self::Item, 26>;

/// Returns an iterator over the colours in the flavour.
fn into_iter(self) -> std::array::IntoIter<Colour, 26> {
fn into_iter(self) -> Self::IntoIter {
[
self.rosewater,
self.flamingo,
Expand Down Expand Up @@ -69,6 +69,50 @@ impl IntoIterator for FlavourColours {
}
}

impl FlavourColours {
/// Returns an iterator over (name, colour) pairs in the flavour.
/// This can be useful for constructing a map of the colours:
///
/// ```rust
/// use std::collections::HashMap;
/// use catppuccin::Flavour;
///
/// let colours: HashMap<_, _> = Flavour::Mocha.colours().into_fields_iter().collect();
/// println!("Red: {}", colours["red"].hex());
/// ```
pub fn into_fields_iter(self) -> std::array::IntoIter<(&'static str, Colour), 26> {
[
("rosewater", self.rosewater),
("flamingo", self.flamingo),
("pink", self.pink),
("mauve", self.mauve),
("red", self.red),
("maroon", self.maroon),
("peach", self.peach),
("yellow", self.yellow),
("green", self.green),
("teal", self.teal),
("sky", self.sky),
("sapphire", self.sapphire),
("blue", self.blue),
("lavender", self.lavender),
("text", self.text),
("subtext1", self.subtext1),
("subtext0", self.subtext0),
("overlay2", self.overlay2),
("overlay1", self.overlay1),
("overlay0", self.overlay0),
("surface2", self.surface2),
("surface1", self.surface1),
("surface0", self.surface0),
("base", self.base),
("mantle", self.mantle),
("crust", self.crust),
]
.into_iter()
}
}

/// Given a colour table taken from the main Catppuccin readme, validate each
/// colour in the given flavour implementation.
/// Copy the table in question from the following URL:
Expand Down

0 comments on commit 5124eb9

Please sign in to comment.