Skip to content

Commit

Permalink
feat: make colors/flavors accessible in const context (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliuskreutz authored Feb 6, 2024
1 parent f4cf96b commit c78194b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use catppuccin::Flavour;

fn main() {
// iterate over the four Catppuccin flavours.
for flavour in Flavour::into_iter() {
for flavour in Flavour::all() {
println!("{}", flavour.name());

// iterate over the 26 colours in the flavour.
Expand Down
17 changes: 11 additions & 6 deletions src/flavour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ pub enum Flavour {

macro_rules! impl_colour_method {
($x:ident) => (
pub fn $x(self) -> $crate::Colour {
pub const fn $x(self) -> $crate::Colour {
self.colours().$x
}
);
($x:ident, $($y:ident),+ $(,)?) => (
pub fn $x(self) -> $crate::Colour {
pub const fn $x(self) -> $crate::Colour {
self.colours().$x
}

Expand All @@ -26,7 +26,7 @@ macro_rules! impl_colour_method {

impl Flavour {
/// Returns the name of the flavour in lowercase.
pub fn name(self) -> &'static str {
pub const fn name(self) -> &'static str {
match self {
Self::Latte => "latte",
Self::Frappe => "frappe",
Expand All @@ -35,7 +35,7 @@ impl Flavour {
}
}

pub fn colours(self) -> FlavourColours {
pub const fn colours(self) -> FlavourColours {
match self {
Self::Latte => FlavourColours {
rosewater: Colour(220, 138, 120),
Expand Down Expand Up @@ -153,8 +153,13 @@ impl Flavour {
}

/// Returns an iterator over the four delicious Catppuccin flavours.
pub fn into_iter() -> std::array::IntoIter<Flavour, 4> {
[Self::Latte, Self::Frappe, Self::Macchiato, Self::Mocha].into_iter()
pub fn into_iter() -> std::array::IntoIter<Self, 4> {
Self::all().into_iter()
}

/// Returns an array of the four delicious Catppuccin flavours.
pub const fn all() -> [Self; 4] {
[Self::Latte, Self::Frappe, Self::Macchiato, Self::Mocha]
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod tests {
/// Ensures flavours are iterated in the correct order.
#[test]
fn test_flavours_iter() {
let mut flavours = Flavour::into_iter();
let mut flavours = Flavour::all().into_iter();
assert_eq!(flavours.next(), Some(Flavour::Latte));
assert_eq!(flavours.next(), Some(Flavour::Frappe));
assert_eq!(flavours.next(), Some(Flavour::Macchiato));
Expand All @@ -88,7 +88,7 @@ mod tests {
/// Ensures colours within each flavour are iterated in the correct order.
#[test]
fn test_colours_iter() {
for flavour in Flavour::into_iter() {
for flavour in Flavour::all() {
let colours = flavour.colours();
let mut colours_iter = colours.into_iter();
assert_eq!(colours_iter.next(), Some(colours.rosewater));
Expand Down

0 comments on commit c78194b

Please sign in to comment.