-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.rs
898 lines (833 loc) · 28.5 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
//! Build script for the Catppuccin crate.
//! This script uses the palette JSON file from the catppuccin/palette github repo
//! in order to populate the `FlavorColors` struct as well as implement the various
//! iteration & indexing primitives offered by the crate.
use std::{
collections::HashMap,
env,
error::Error,
fs::File,
io::{BufReader, BufWriter, Write},
path::PathBuf,
};
use itertools::Itertools;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Palette {
#[allow(dead_code)]
version: String,
#[serde(flatten)]
flavors: HashMap<String, Flavor>,
}
#[derive(Debug, Deserialize)]
struct Flavor {
emoji: char,
order: u32,
dark: bool,
colors: HashMap<String, Color>,
#[serde(rename = "ansiColors")]
ansi_colors: HashMap<String, AnsiColorPair>,
}
#[derive(Debug, Deserialize)]
struct Color {
name: String,
order: u32,
rgb: Rgb,
hsl: Hsl,
accent: bool,
}
#[derive(Debug, Deserialize)]
struct Rgb {
r: u8,
g: u8,
b: u8,
}
#[derive(Debug, Deserialize)]
struct Hsl {
h: f64,
s: f64,
l: f64,
}
#[derive(Debug, Deserialize)]
struct AnsiColorPair {
name: String,
order: u32,
normal: AnsiColor,
bright: AnsiColor,
}
#[derive(Debug, Deserialize)]
struct AnsiColor {
name: String,
rgb: Rgb,
hsl: Hsl,
code: u8,
}
fn main() -> Result<(), Box<dyn Error>> {
let out_dir = PathBuf::from(&env::var("OUT_DIR")?);
let codegen_path = out_dir.join("generated_palette.rs");
let mut code_writer = BufWriter::new(File::create(codegen_path)?);
let palette: Palette =
serde_json::from_reader(BufReader::new(File::open("src/palette.json")?))?;
let sample_flavor = palette
.flavors
.values()
.next()
.expect("at least one flavor");
let flavor_tokens = [
// Colors
make_flavor_colors_struct(sample_flavor),
make_flavor_colors_all_impl(sample_flavor),
// ANSI Colors
make_flavor_ansi_colors_struct(sample_flavor),
make_flavor_ansi_colors_all_impl(sample_flavor),
// ANSI Color Pairs
make_flavor_ansi_color_pairs_struct(sample_flavor),
make_flavor_ansi_color_pairs_all_impl(sample_flavor),
];
let color_tokens = [
make_color_name_enum(sample_flavor),
make_color_name_index_impl(sample_flavor),
make_color_name_display_impl(sample_flavor),
make_color_name_identifier_impl(sample_flavor),
make_color_name_fromstr_impl_tokens(sample_flavor),
];
let ansi_color_tokens = [
make_ansi_color_name_enum(sample_flavor),
make_ansi_color_name_index_impl(sample_flavor),
make_ansi_color_name_display_impl(sample_flavor),
make_ansi_color_name_identifier_impl(sample_flavor),
make_ansi_color_name_fromstr_impl_tokens(sample_flavor),
];
let ansi_color_pair_tokens = [
make_ansi_color_pair_name_enum(sample_flavor),
make_ansi_color_pair_name_index_impl(sample_flavor),
make_ansi_color_pair_name_display_impl(sample_flavor),
make_ansi_color_pair_name_identifier_impl(sample_flavor),
make_ansi_color_pair_name_fromstr_impl_tokens(sample_flavor),
];
let palette_tokens = [make_palette_const(&palette)];
let ast = syn::parse2(
[
&flavor_tokens[..],
&color_tokens[..],
&ansi_color_tokens[..],
&ansi_color_pair_tokens[..],
&palette_tokens[..],
]
.concat()
.into_iter()
.collect(),
)?;
let code = prettyplease::unparse(&ast);
write!(&mut code_writer, "{code}")?;
Ok(())
}
fn palette_circle(filename: &str) -> String {
format!(
r#"<img width="23" height="23" src="https://raw.githubusercontent.com/catppuccin/catppuccin/95aae3360eb88fc1b6a89398be08ec6deae0bc9a/assets/palette/circles/{filename}.png">"#
)
}
fn color_palette_circles(color_key: &str) -> String {
["latte", "frappe", "macchiato", "mocha"]
.map(|n| palette_circle(format!("{n}_{color_key}").as_str()))
.into_iter()
.collect::<String>()
}
fn ansi_color_palette_circles(color_key: &str) -> String {
["latte", "frappe", "macchiato", "mocha"]
.map(|n| palette_circle(format!("ansi/{n}_ansi_{color_key}").as_str()))
.into_iter()
.collect::<String>()
}
fn titlecase<S: AsRef<str>>(s: S) -> String {
let mut chars = s.as_ref().chars();
chars.next().map_or_else(String::new, |first| {
first.to_uppercase().to_string() + chars.as_str()
})
}
fn remove_whitespace(s: &str) -> String {
s.replace(' ', "")
}
fn flavors_in_order(palette: &Palette) -> std::vec::IntoIter<(&String, &Flavor)> {
palette
.flavors
.iter()
.sorted_by(|(_, a), (_, b)| a.order.cmp(&b.order))
}
fn colors_in_order(flavor: &Flavor) -> std::vec::IntoIter<(&String, &Color)> {
flavor
.colors
.iter()
.sorted_by(|(_, a), (_, b)| a.order.cmp(&b.order))
}
fn ansi_color_pairs_in_order(flavor: &Flavor) -> std::vec::IntoIter<(&String, &AnsiColorPair)> {
flavor
.ansi_colors
.iter()
.sorted_by(|(_, a), (_, b)| a.order.cmp(&b.order))
}
fn ansi_colors_in_order(flavor: &Flavor) -> std::vec::IntoIter<(String, &AnsiColor)> {
flavor
.ansi_colors
.iter()
.flat_map(|(_, c)| [&c.normal, &c.bright])
.map(|c| (c.name.to_lowercase().replace(' ', "_"), c))
.sorted_by(|(_, a), (_, b)| a.code.cmp(&b.code))
}
fn make_flavor_colors_struct(sample_flavor: &Flavor) -> TokenStream {
let colors = colors_in_order(sample_flavor).map(|(k, _)| {
let ident = format_ident!("{k}");
let color_img = format!(" {}", color_palette_circles(k));
quote! {
#[doc = #color_img]
pub #ident: Color
}
});
quote! {
/// All of the colors for a particular flavor of Catppuccin.
/// Obtained via [`Flavor::colors`].
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FlavorColors {
#(#colors),*
}
}
}
fn make_flavor_ansi_colors_struct(sample_flavor: &Flavor) -> TokenStream {
let colors = ansi_colors_in_order(sample_flavor).map(|(k, _)| {
let ident = format_ident!("{k}");
let color_img = format!(" {}", ansi_color_palette_circles(&k));
quote! {
#[doc = #color_img]
pub #ident: AnsiColor
}
});
quote! {
/// All of the ANSI colors for a particular flavor of Catppuccin.
/// Obtained via [`Flavor::ansi_colors`].
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FlavorAnsiColors {
#(#colors),*
}
/// A single ANSI color.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnsiColor {
/// The [`AnsiColorName`] for this color.
pub name: AnsiColorName,
/// The color represented as a six-digit hex string with a leading hash (#).
pub hex: Hex,
/// The color represented as individual red, green, and blue channels.
pub rgb: Rgb,
/// The color represented as individual hue, saturation, and lightness channels.
pub hsl: Hsl,
/// The color's ANSI code.
pub code: u8,
}
}
}
fn make_flavor_ansi_color_pairs_struct(sample_flavor: &Flavor) -> TokenStream {
let color_pairs = ansi_color_pairs_in_order(sample_flavor).map(|(k, _)| {
let ident = format_ident!("{k}");
let doc = format!("The normal and bright {k} ANSI color pair.");
quote! {
#[doc = #doc]
pub #ident: AnsiColorPair
}
});
quote! {
/// All of the ANSI color pairs for a particular flavor of Catppuccin.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FlavorAnsiColorPairs {
#(#color_pairs),*
}
/// A pair of ANSI colors - normal and bright.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AnsiColorPair {
/// The [`AnsiColorPairName`] for this color.
pub name: AnsiColorPairName,
/// Order of the ANSI color in the palette spec.
pub order: u32,
/// The normal color.
pub normal: AnsiColor,
/// The bright color.
pub bright: AnsiColor,
}
}
}
fn make_flavor_colors_all_impl(sample_flavor: &Flavor) -> TokenStream {
let items = colors_in_order(sample_flavor).map(|(identifier, _)| {
let ident = format_ident!("{identifier}");
quote! { &self.#ident }
});
quote! {
impl FlavorColors {
/// Get an array of the colors in the flavor.
#[must_use]
pub const fn all_colors(&self) -> [&Color; 26] {
[
#(#items),*
]
}
}
}
}
fn make_flavor_ansi_colors_all_impl(sample_flavor: &Flavor) -> TokenStream {
let ansi_colors = ansi_colors_in_order(sample_flavor).map(|(identifier, _)| {
let ident = format_ident!("{identifier}");
quote! { &self.#ident }
});
let ansi_color_pairs = ansi_color_pairs_in_order(sample_flavor)
.map(|(identifier, color_pair)| make_ansi_color_pair_entry(identifier, color_pair));
quote! {
impl FlavorAnsiColors {
/// Get an array of the ANSI colors in the flavor.
#[must_use]
pub const fn all_ansi_colors(&self) -> [&AnsiColor; 16] {
[
#(#ansi_colors),*
]
}
/// Convert the 16 ANSI colors to 8 ANSI color pairs.
#[must_use]
#[allow(clippy::too_many_lines, clippy::unreadable_literal)]
pub const fn to_ansi_color_pairs(&self) -> FlavorAnsiColorPairs {
FlavorAnsiColorPairs {
#(#ansi_color_pairs),*
}
}
}
}
}
fn make_flavor_ansi_color_pairs_all_impl(sample_flavor: &Flavor) -> TokenStream {
let items = ansi_color_pairs_in_order(sample_flavor).map(|(identifier, _)| {
let ident = format_ident!("{identifier}");
quote! { &self.#ident }
});
quote! {
impl FlavorAnsiColorPairs {
/// Get an array of the ANSI color pairs in the flavor.
#[must_use]
pub const fn all_ansi_color_pairs(&self) -> [&AnsiColorPair; 8] {
[
#(#items),*
]
}
}
}
}
fn make_color_name_enum(sample_flavor: &Flavor) -> TokenStream {
let variants = colors_in_order(sample_flavor).map(|(name, _)| {
let ident = format_ident!("{}", titlecase(name));
let circles = format!(" {}", color_palette_circles(name));
quote! {
#[doc = #circles]
#ident
}
});
quote! {
/// Enum of all named Catppuccin colors. Can be used to index into a [`FlavorColors`].
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ColorName {
#(#variants),*
}
}
}
fn make_ansi_color_name_enum(sample_flavor: &Flavor) -> TokenStream {
let variants = ansi_colors_in_order(sample_flavor).map(|(identifier, color)| {
let name = remove_whitespace(&color.name);
let ident = format_ident!("{name}");
let circles = format!(" {}", ansi_color_palette_circles(&identifier));
quote! {
#[doc = #circles]
#ident
}
});
quote! {
/// Enum of all named ANSI colors. Can be used to index into a [`FlavorAnsiColors`]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AnsiColorName {
#(#variants),*
}
}
}
fn make_ansi_color_pair_name_enum(sample_flavor: &Flavor) -> TokenStream {
let variants = ansi_color_pairs_in_order(sample_flavor).map(|(name, _)| {
let ident = format_ident!("{}", titlecase(name));
let circles = format!(" {}", ansi_color_palette_circles(name));
quote! {
#[doc = #circles]
#ident
}
});
quote! {
/// Enum of all ANSI color pairs. Can be used to index into a [`FlavorAnsiColorPairs`].
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AnsiColorPairName {
#(#variants),*
}
}
}
fn make_color_name_index_impl(sample_flavor: &Flavor) -> TokenStream {
let first = colors_in_order(sample_flavor).map(|(identifier, _)| {
let variant = format_ident!("{}", titlecase(identifier));
let ident = format_ident!("{}", identifier);
quote! {
ColorName::#variant => &self.#ident
}
});
let second = first.clone();
quote! {
impl Index<ColorName> for FlavorColors {
type Output = Color;
fn index(&self, index: ColorName) -> &Self::Output {
match index {
#(#first),*
}
}
}
impl FlavorColors {
/// Get a color by name.
///
/// This is equivalent to using the index operator, but can also be used in
/// const contexts.
#[must_use]
pub const fn get_color(&self, name: ColorName) -> &Color {
match name {
#(#second),*
}
}
}
}
}
fn make_ansi_color_name_index_impl(sample_flavor: &Flavor) -> TokenStream {
let first = ansi_colors_in_order(sample_flavor).map(|(identifier, color)| {
let variant = format_ident!("{}", remove_whitespace(&color.name));
let ident = format_ident!("{}", identifier);
quote! {
AnsiColorName::#variant => &self.#ident
}
});
let second = first.clone();
quote! {
impl Index<AnsiColorName> for FlavorAnsiColors {
type Output = AnsiColor;
fn index(&self, index: AnsiColorName) -> &Self::Output {
match index {
#(#first),*
}
}
}
impl FlavorAnsiColors {
/// Get an ANSI color by name.
///
/// This is equivalent to using the index operator, but can also be used in
/// const contexts.
#[must_use]
pub const fn get_ansi_color(&self, name: AnsiColorName) -> &AnsiColor {
match name {
#(#second),*
}
}
}
}
}
fn make_ansi_color_pair_name_index_impl(sample_flavor: &Flavor) -> TokenStream {
let first = ansi_color_pairs_in_order(sample_flavor).map(|(identifier, _)| {
let variant = format_ident!("{}", titlecase(identifier));
let ident = format_ident!("{}", identifier);
quote! {
AnsiColorPairName::#variant => &self.#ident
}
});
let second = first.clone();
quote! {
impl Index<AnsiColorPairName> for FlavorAnsiColorPairs {
type Output = AnsiColorPair;
fn index(&self, index: AnsiColorPairName) -> &Self::Output {
match index {
#(#first),*
}
}
}
impl FlavorAnsiColorPairs {
/// Get an ANSI color pair by name.
///
/// This is equivalent to using the index operator, but can also be used in
/// const contexts.
#[must_use]
pub const fn get_ansi_color_pair(&self, name: AnsiColorPairName) -> &AnsiColorPair {
match name {
#(#second),*
}
}
}
}
}
fn make_color_name_display_impl(sample_flavor: &Flavor) -> TokenStream {
let match_arms = colors_in_order(sample_flavor).map(|(identifier, color)| {
let variant = format_ident!("{}", titlecase(identifier));
let name = &color.name;
quote! {
Self::#variant => write!(f, #name)
}
});
quote! {
impl std::fmt::Display for ColorName {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
#(#match_arms),*
}
}
}
}
}
fn make_ansi_color_name_display_impl(sample_flavor: &Flavor) -> TokenStream {
let match_arms = ansi_colors_in_order(sample_flavor).map(|(_, color)| {
let name = &color.name;
let variant = format_ident!("{}", remove_whitespace(name));
quote! {
Self::#variant => write!(f, #name)
}
});
quote! {
impl std::fmt::Display for AnsiColorName {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
#(#match_arms),*
}
}
}
}
}
fn make_ansi_color_pair_name_display_impl(sample_flavor: &Flavor) -> TokenStream {
let match_arms = ansi_color_pairs_in_order(sample_flavor).map(|(identifier, _)| {
let name = titlecase(identifier);
let variant = format_ident!("{name}");
quote! {
Self::#variant => write!(f, #name)
}
});
quote! {
impl std::fmt::Display for AnsiColorPairName {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
#(#match_arms),*
}
}
}
}
}
fn make_color_name_identifier_impl(sample_flavor: &Flavor) -> TokenStream {
let match_arms = colors_in_order(sample_flavor).map(|(identifier, _)| {
let variant = format_ident!("{}", titlecase(identifier));
quote! {
Self::#variant => #identifier
}
});
quote! {
impl ColorName {
/// Get the color's identifier; the lowercase key used to identify the color.
/// This differs from `to_string` in that it's intended for machine usage
/// rather than presentation.
///
/// Example:
///
/// ```rust
/// let surface0 = catppuccin::PALETTE.latte.colors.surface0;
/// assert_eq!(surface0.name.to_string(), "Surface 0");
/// assert_eq!(surface0.name.identifier(), "surface0");
/// ```
#[must_use]
pub const fn identifier(&self) -> &'static str {
match self {
#(#match_arms),*
}
}
}
}
}
fn make_ansi_color_name_identifier_impl(sample_flavor: &Flavor) -> TokenStream {
let match_arms = ansi_colors_in_order(sample_flavor).map(|(identifier, color)| {
let variant = format_ident!("{}", remove_whitespace(&color.name));
quote! {
Self::#variant => #identifier
}
});
quote! {
impl AnsiColorName {
/// Get the ANSI color's identifier; the lowercase key used to identify the color.
/// This differs from `to_string` in that it's intended for machine usage
/// rather than presentation.
///
/// Example:
///
/// ```rust
/// let bright_black = catppuccin::PALETTE.latte.ansi_colors.bright_black;
/// assert_eq!(bright_black.name.to_string(), "Bright Black");
/// assert_eq!(bright_black.name.identifier(), "bright_black");
/// ```
#[must_use]
pub const fn identifier(&self) -> &'static str {
match self {
#(#match_arms),*
}
}
}
}
}
fn make_ansi_color_pair_name_identifier_impl(sample_flavor: &Flavor) -> TokenStream {
let match_arms = ansi_color_pairs_in_order(sample_flavor).map(|(identifier, _)| {
let variant = format_ident!("{}", titlecase(identifier));
quote! {
Self::#variant => #identifier
}
});
quote! {
impl AnsiColorPairName {
/// Get the ANSI color pair's identifier; the lowercase key used to identify the color.
/// This differs from `to_string` in that it's intended for machine usage
/// rather than presentation.
///
/// Example:
///
/// ```rust
/// let black_ansi_pair = catppuccin::PALETTE.latte.ansi_colors.all_pairs().black;
/// assert_eq!(black_ansi_pair.name.to_string(), "Black");
/// assert_eq!(black_ansi_pair.name.identifier(), "black");
/// assert_eq!(black_ansi_pair.normal.name.to_string(), "Black");
/// assert_eq!(black_ansi_pair.normal.name.identifier(), "black");
/// assert_eq!(black_ansi_pair.bright.name.to_string(), "Bright Black");
/// assert_eq!(black_ansi_pair.bright.name.identifier(), "bright_black");
/// ```
#[must_use]
pub const fn identifier(&self) -> &'static str {
match self {
#(#match_arms),*
}
}
}
}
}
fn make_color_name_fromstr_impl_tokens(sample_flavor: &Flavor) -> TokenStream {
let match_arms = colors_in_order(sample_flavor)
.map(|(identifier, _)| {
let variant = format_ident!("{}", titlecase(identifier));
quote! {
#identifier => Ok(Self::#variant)
}
})
.collect::<Vec<_>>();
quote! {
impl std::str::FromStr for ColorName {
type Err = ParseColorNameError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
#(#match_arms),*,
_ => Err(ParseColorNameError),
}
}
}
}
}
fn make_ansi_color_name_fromstr_impl_tokens(sample_flavor: &Flavor) -> TokenStream {
let match_arms = ansi_colors_in_order(sample_flavor)
.map(|(identifier, color)| {
let variant = format_ident!("{}", remove_whitespace(&color.name));
quote! {
#identifier => Ok(Self::#variant)
}
})
.collect::<Vec<_>>();
quote! {
impl std::str::FromStr for AnsiColorName {
type Err = ParseColorNameError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
#(#match_arms),*,
_ => Err(ParseColorNameError),
}
}
}
}
}
fn make_ansi_color_pair_name_fromstr_impl_tokens(sample_flavor: &Flavor) -> TokenStream {
let match_arms = ansi_color_pairs_in_order(sample_flavor)
.map(|(identifier, _)| {
let variant = format_ident!("{}", titlecase(identifier));
quote! {
#identifier => Ok(Self::#variant)
}
})
.collect::<Vec<_>>();
quote! {
impl std::str::FromStr for AnsiColorPairName {
type Err = ParseColorNameError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
#(#match_arms),*,
_ => Err(ParseColorNameError),
}
}
}
}
}
fn make_palette_const(palette: &Palette) -> TokenStream {
let flavors =
flavors_in_order(palette).map(|(identifier, flavor)| make_flavor_entry(identifier, flavor));
let tokens = quote! {
/// The Catppuccin palette. This constant will generally be your entrypoint
/// into using the crate.
#[allow(clippy::unreadable_literal)]
pub const PALETTE: Palette = Palette {
#(#flavors),*
};
};
tokens
}
fn make_flavor_entry(identifier: &str, flavor: &Flavor) -> TokenStream {
let Flavor {
emoji, order, dark, ..
} = flavor;
let colors =
colors_in_order(flavor).map(|(identifier, color)| make_color_entry(identifier, color));
let ansi_colors = ansi_colors_in_order(flavor)
.map(|(identifier, ansi_color_pair)| make_ansi_color_entry(&identifier, ansi_color_pair));
let flavorname_variant = format_ident!("{}", titlecase(identifier));
let ident = format_ident!("{}", identifier);
quote! {
#ident: Flavor {
name: FlavorName::#flavorname_variant,
emoji: #emoji,
order: #order,
dark: #dark,
colors: FlavorColors {
#(#colors),*
},
ansi_colors: FlavorAnsiColors {
#(#ansi_colors),*
}
}
}
}
fn make_color_entry(identifier: &str, color: &Color) -> TokenStream {
let ident = format_ident!("{}", identifier);
let colorname_variant = format_ident!("{}", titlecase(identifier));
let Color {
order,
accent,
rgb: Rgb { r, g, b },
hsl: Hsl { h, s, l },
..
} = color;
let rgb = quote! { Rgb { r: #r, g: #g, b: #b } };
let hsl = quote! { Hsl { h: #h, s: #s, l: #l } };
quote! {
#ident: Color {
name: ColorName::#colorname_variant,
order: #order,
accent: #accent,
hex: Hex(#rgb),
rgb: #rgb,
hsl: #hsl,
}
}
}
fn make_ansi_color_entry(identifier: &str, ansi_color: &AnsiColor) -> TokenStream {
let ident = format_ident!("{identifier}");
let AnsiColor {
name,
code,
rgb: Rgb { r, g, b },
hsl: Hsl { h, s, l },
} = ansi_color;
let name_variant = format_ident!("{}", remove_whitespace(name));
let rgb = quote! { Rgb { r: #r, g: #g, b: #b } };
let hsl = quote! { Hsl { h: #h, s: #s, l: #l } };
quote! {
#ident: AnsiColor {
name: AnsiColorName::#name_variant,
hex: Hex(#rgb),
rgb: #rgb,
hsl: #hsl,
code: #code,
}
}
}
fn make_ansi_color_pair_entry(identifier: &str, ansi_color_pair: &AnsiColorPair) -> TokenStream {
let ident = format_ident!("{}", identifier);
let AnsiColorPair {
name,
order,
normal:
AnsiColor {
name: normal_name,
code: normal_code,
rgb:
Rgb {
r: normal_r,
g: normal_g,
b: normal_b,
},
hsl:
Hsl {
h: normal_h,
s: normal_s,
l: normal_l,
},
},
bright:
AnsiColor {
name: bright_name,
code: bright_code,
rgb:
Rgb {
r: bright_r,
g: bright_g,
b: bright_b,
},
hsl:
Hsl {
h: bright_h,
s: bright_s,
l: bright_l,
},
..
},
} = ansi_color_pair;
let ansi_name_variant = format_ident!("{}", name);
let normal_name_variant = format_ident!("{}", normal_name);
let bright_name_variant = format_ident!("{}", remove_whitespace(bright_name));
let normal_rgb = quote! { Rgb { r: #normal_r, g: #normal_g, b: #normal_b } };
let normal_hsl = quote! { Hsl { h: #normal_h, s: #normal_s, l: #normal_l } };
let bright_rgb = quote! { Rgb { r: #bright_r, g: #bright_g, b: #bright_b } };
let bright_hsl = quote! { Hsl { h: #bright_h, s: #bright_s, l: #bright_l } };
quote! {
#ident: AnsiColorPair {
name: AnsiColorPairName::#ansi_name_variant,
order: #order,
normal: AnsiColor {
name: AnsiColorName::#normal_name_variant,
hex: Hex(#normal_rgb),
rgb: #normal_rgb,
hsl: #normal_hsl,
code: #normal_code,
},
bright: AnsiColor {
name: AnsiColorName::#bright_name_variant,
hex: Hex(#bright_rgb),
rgb: #bright_rgb,
hsl: #bright_hsl,
code: #bright_code,
}
}
}
}