Rust: Add methods to Flavour to get each colour

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
This commit is contained in:
Carsten Kragelund 2022-10-26 23:34:04 +02:00
parent 468da426f5
commit 8f885d4339
No known key found for this signature in database
3 changed files with 23 additions and 2 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -22,7 +22,7 @@
//! fn confirm(text: String) -> Button {
//! Button {
//! text,
//! background_colour: Flavour::Mocha.colours().green.hex(),
//! background_colour: Flavour::Mocha.green().hex(),
//! }
//! }
//! ```