From e9db65e1ae2992edf7e296ede8efe0f1aa20e323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carsten=20Kragelund=20J=C3=B8rgensen?= Date: Thu, 27 Oct 2022 18:37:48 +0200 Subject: [PATCH] Rust: Add methods to `Flavour` to get each colour (#32) This adds method to get each of the colors directly from the `Flavour` variants like so ```rust Flavour::Latte.teal() ``` rather than needing to go through the `FlavourColours` struct This closes #27 Co-authored-by: Hamothy <58985301+sgoudham@users.noreply.github.com> --- rust/src/colour.rs | 2 +- rust/src/flavour.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++ rust/src/lib.rs | 2 +- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/rust/src/colour.rs b/rust/src/colour.rs index f922591..99ef26e 100644 --- a/rust/src/colour.rs +++ b/rust/src/colour.rs @@ -13,7 +13,7 @@ impl Colour { /// ``` /// use catppuccin::Flavour; /// - /// let hex = Flavour::Mocha.colours().teal.hex(); + /// let hex = Flavour::Mocha.teal().hex(); /// assert_eq!(hex, "94E2D5"); /// ``` pub fn hex(&self) -> String { diff --git a/rust/src/flavour.rs b/rust/src/flavour.rs index d957a62..3793d47 100644 --- a/rust/src/flavour.rs +++ b/rust/src/flavour.rs @@ -8,6 +8,21 @@ pub enum Flavour { Mocha, } +macro_rules! impl_colour_method { + ($x:ident) => ( + pub fn $x(self) -> $crate::Colour { + self.colours().$x + } + ); + ($x:ident, $($y:ident),+ $(,)?) => ( + pub fn $x(self) -> $crate::Colour { + self.colours().$x + } + + impl_colour_method!($($y),+); + ); +} + impl Flavour { pub fn name(self) -> &'static str { match self { @@ -18,6 +33,12 @@ impl Flavour { } } + impl_colour_method!( + rosewater, flamingo, pink, mauve, red, maroon, peach, yellow, green, teal, sky, sapphire, + blue, lavender, text, subtext1, subtext0, overlay2, overlay1, overlay0, surface2, surface1, + surface0, base, mantle, crust, + ); + pub fn colours(self) -> FlavourColours { match self { Self::Latte => FlavourColours { @@ -145,6 +166,38 @@ mod tests { use crate::flavour_colours::validate_colours; use indoc::indoc; + #[test] + fn verify_colour_methods() { + // We only need to test one flavour, as we just need to make sure the methods exists + // Because if the correct method exists, it is guaranteed access to the correctly named field + let _rosewater = Flavour::Latte.rosewater(); + let _flamingo = Flavour::Latte.flamingo(); + let _pink = Flavour::Latte.pink(); + let _mauve = Flavour::Latte.mauve(); + let _red = Flavour::Latte.red(); + let _maroon = Flavour::Latte.maroon(); + let _peach = Flavour::Latte.peach(); + let _yellow = Flavour::Latte.yellow(); + let _green = Flavour::Latte.green(); + let _teal = Flavour::Latte.teal(); + let _sky = Flavour::Latte.sky(); + let _sapphire = Flavour::Latte.sapphire(); + let _blue = Flavour::Latte.blue(); + let _lavender = Flavour::Latte.lavender(); + let _text = Flavour::Latte.text(); + let _subtext1 = Flavour::Latte.subtext1(); + let _subtext0 = Flavour::Latte.subtext0(); + let _overlay2 = Flavour::Latte.overlay2(); + let _overlay1 = Flavour::Latte.overlay1(); + let _overlay0 = Flavour::Latte.overlay0(); + let _surface2 = Flavour::Latte.surface2(); + let _surface1 = Flavour::Latte.surface1(); + let _surface0 = Flavour::Latte.surface0(); + let _base = Flavour::Latte.base(); + let _mantle = Flavour::Latte.mantle(); + let _crust = Flavour::Latte.crust(); + } + #[test] fn validate_latte_colours() { validate_colours( diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 099f1c6..d932327 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -22,7 +22,7 @@ //! fn confirm(text: String) -> Button { //! Button { //! text, -//! background_colour: Flavour::Mocha.colours().green.hex(), +//! background_colour: Flavour::Mocha.green().hex(), //! } //! } //! ```