hashavatar 1.1.0

Stable deterministic procedural avatars in Rust with configurable identity hashing, WebP, optional PNG/JPEG/GIF, and SVG export
Documentation
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
use super::*;

/// Trait for renderers that can draw reusable avatar styles onto an image buffer.
pub trait AvatarRenderer {
    fn render(&self, spec: AvatarSpec) -> Result<RgbaImage, AvatarSpecError>;
}

/// Export formats for encoded avatar assets.
///
/// `WebP` is the default because it is the more modern distribution format and
/// is usually smaller than PNG for generated avatar art.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AvatarOutputFormat {
    /// Lossless WebP output.
    #[default]
    WebP,
    /// Optional lossless PNG output.
    #[cfg(feature = "png")]
    Png,
    /// Optional JPEG output with transparent pixels composited over white.
    #[cfg(feature = "jpeg")]
    Jpeg,
    /// Optional GIF output.
    ///
    /// # Warning
    ///
    /// GIF encoding performs 256-color quantization inside the `image` crate.
    /// Those internal quantization buffers are not accessible to `hashavatar`
    /// and are not sanitized by this crate. For high-assurance deployments,
    /// prefer `AvatarOutputFormat::WebP` or PNG output.
    #[cfg(feature = "gif")]
    Gif,
}

impl AvatarOutputFormat {
    pub const ALL: &'static [Self] = &[
        Self::WebP,
        #[cfg(feature = "png")]
        Self::Png,
        #[cfg(feature = "jpeg")]
        Self::Jpeg,
        #[cfg(feature = "gif")]
        Self::Gif,
    ];

    pub fn from_byte(value: u8) -> Self {
        Self::ALL[usize::from(value) % Self::ALL.len()]
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::WebP => "webp",
            #[cfg(feature = "png")]
            Self::Png => "png",
            #[cfg(feature = "jpeg")]
            Self::Jpeg => "jpg",
            #[cfg(feature = "gif")]
            Self::Gif => "gif",
        }
    }
}

impl FromStr for AvatarOutputFormat {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_ascii_lowercase().as_str() {
            "webp" => Ok(Self::WebP),
            #[cfg(feature = "png")]
            "png" => Ok(Self::Png),
            #[cfg(feature = "jpeg")]
            "jpg" | "jpeg" => Ok(Self::Jpeg),
            #[cfg(feature = "gif")]
            "gif" => Ok(Self::Gif),
            _ => Err("unsupported avatar output format"),
        }
    }
}

impl std::fmt::Display for AvatarOutputFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Avatar family selection.
///
/// Values can be round-tripped through [`AvatarKind::as_str`] and
/// [`FromStr`]. `Icecream` also accepts `ice-cream` and `ice_cream` when
/// parsing user input.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AvatarKind {
    /// Cat face avatar.
    #[default]
    Cat,
    /// Dog face avatar.
    Dog,
    /// Robot head avatar.
    Robot,
    /// Fox face avatar.
    Fox,
    /// Alien face avatar.
    Alien,
    /// Monster face avatar.
    Monster,
    /// Ghost avatar.
    Ghost,
    /// Slime creature avatar.
    Slime,
    /// Bird avatar.
    Bird,
    /// Wizard avatar.
    Wizard,
    /// Skull avatar.
    Skull,
    /// Paw-print avatar.
    Paws,
    /// Ringed planet avatar.
    Planet,
    /// Rocket avatar.
    Rocket,
    /// Mushroom avatar.
    Mushroom,
    /// Cactus avatar.
    Cactus,
    /// Frog face avatar.
    Frog,
    /// Panda face avatar.
    Panda,
    /// Cupcake avatar.
    Cupcake,
    /// Pizza slice avatar.
    Pizza,
    /// Ice cream cone avatar.
    Icecream,
    /// Octopus avatar.
    Octopus,
    /// Knight helmet avatar.
    Knight,
    /// Bear face avatar.
    Bear,
    /// Penguin avatar.
    Penguin,
    /// Dragon avatar.
    Dragon,
    /// Ninja avatar.
    Ninja,
    /// Astronaut avatar.
    Astronaut,
    /// Diamond avatar.
    Diamond,
    /// Coffee cup avatar.
    CoffeeCup,
    /// Shield avatar.
    Shield,
}

impl AvatarKind {
    pub const ALL: &'static [Self] = &[
        Self::Cat,
        Self::Dog,
        Self::Robot,
        Self::Fox,
        Self::Alien,
        Self::Monster,
        Self::Ghost,
        Self::Slime,
        Self::Bird,
        Self::Wizard,
        Self::Skull,
        Self::Paws,
        Self::Planet,
        Self::Rocket,
        Self::Mushroom,
        Self::Cactus,
        Self::Frog,
        Self::Panda,
        Self::Cupcake,
        Self::Pizza,
        Self::Icecream,
        Self::Octopus,
        Self::Knight,
        Self::Bear,
        Self::Penguin,
        Self::Dragon,
        Self::Ninja,
        Self::Astronaut,
        Self::Diamond,
        Self::CoffeeCup,
        Self::Shield,
    ];

    pub fn from_byte(value: u8) -> Self {
        Self::ALL[usize::from(value) % Self::ALL.len()]
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Cat => "cat",
            Self::Dog => "dog",
            Self::Robot => "robot",
            Self::Fox => "fox",
            Self::Alien => "alien",
            Self::Monster => "monster",
            Self::Ghost => "ghost",
            Self::Slime => "slime",
            Self::Bird => "bird",
            Self::Wizard => "wizard",
            Self::Skull => "skull",
            Self::Paws => "paws",
            Self::Planet => "planet",
            Self::Rocket => "rocket",
            Self::Mushroom => "mushroom",
            Self::Cactus => "cactus",
            Self::Frog => "frog",
            Self::Panda => "panda",
            Self::Cupcake => "cupcake",
            Self::Pizza => "pizza",
            Self::Icecream => "icecream",
            Self::Octopus => "octopus",
            Self::Knight => "knight",
            Self::Bear => "bear",
            Self::Penguin => "penguin",
            Self::Dragon => "dragon",
            Self::Ninja => "ninja",
            Self::Astronaut => "astronaut",
            Self::Diamond => "diamond",
            Self::CoffeeCup => "coffee-cup",
            Self::Shield => "shield",
        }
    }

    /// Returns whether this family has face anchors for accessories and
    /// expressions.
    ///
    /// Families without face anchors still support canvas-level color and
    /// frame-shape layers. Accessory and expression choices are accepted but
    /// skipped deterministically for those families.
    pub const fn supports_face_layers(self) -> bool {
        !matches!(
            self,
            Self::Paws
                | Self::Planet
                | Self::Rocket
                | Self::Mushroom
                | Self::Cactus
                | Self::Cupcake
                | Self::Pizza
                | Self::Icecream
                | Self::Diamond
                | Self::CoffeeCup
                | Self::Shield
        )
    }
}

impl FromStr for AvatarKind {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_ascii_lowercase().as_str() {
            "cat" => Ok(Self::Cat),
            "dog" => Ok(Self::Dog),
            "robot" => Ok(Self::Robot),
            "fox" => Ok(Self::Fox),
            "alien" => Ok(Self::Alien),
            "monster" => Ok(Self::Monster),
            "ghost" => Ok(Self::Ghost),
            "slime" => Ok(Self::Slime),
            "bird" => Ok(Self::Bird),
            "wizard" => Ok(Self::Wizard),
            "skull" => Ok(Self::Skull),
            "paws" => Ok(Self::Paws),
            "planet" => Ok(Self::Planet),
            "rocket" => Ok(Self::Rocket),
            "mushroom" => Ok(Self::Mushroom),
            "cactus" => Ok(Self::Cactus),
            "frog" => Ok(Self::Frog),
            "panda" => Ok(Self::Panda),
            "cupcake" => Ok(Self::Cupcake),
            "pizza" => Ok(Self::Pizza),
            "icecream" | "ice-cream" | "ice_cream" => Ok(Self::Icecream),
            "octopus" => Ok(Self::Octopus),
            "knight" => Ok(Self::Knight),
            "bear" => Ok(Self::Bear),
            "penguin" => Ok(Self::Penguin),
            "dragon" => Ok(Self::Dragon),
            "ninja" => Ok(Self::Ninja),
            "astronaut" => Ok(Self::Astronaut),
            "diamond" => Ok(Self::Diamond),
            "coffee-cup" | "coffee_cup" | "coffeecup" => Ok(Self::CoffeeCup),
            "shield" => Ok(Self::Shield),
            _ => Err("unsupported avatar kind"),
        }
    }
}

impl std::fmt::Display for AvatarKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Canvas background mode for raster and SVG avatar output.
///
/// `Themed` is identity and family aware. The fixed modes are useful for
/// predictable compositing, while `Transparent` leaves the SVG background out
/// and uses a fully transparent raster canvas.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AvatarBackground {
    /// Identity-derived background chosen by the selected avatar family.
    #[default]
    Themed,
    /// Pure white background.
    White,
    /// Pure black background.
    Black,
    /// Charcoal background, useful for dark UI previews.
    Dark,
    /// Subtle off-white background.
    Light,
    /// Fully transparent background.
    Transparent,
    /// Light dotted pattern.
    PolkaDot,
    /// Subtle diagonal stripe pattern.
    Striped,
    /// Small checkerboard pattern.
    Checkerboard,
    /// Fine grid pattern.
    Grid,
    /// Warm sunrise gradient.
    Sunrise,
    /// Cool ocean gradient.
    Ocean,
    /// Dark star-field background.
    Starry,
}

impl AvatarBackground {
    pub const ALL: &'static [Self] = &[
        Self::Themed,
        Self::White,
        Self::Black,
        Self::Dark,
        Self::Light,
        Self::Transparent,
        Self::PolkaDot,
        Self::Striped,
        Self::Checkerboard,
        Self::Grid,
        Self::Sunrise,
        Self::Ocean,
        Self::Starry,
    ];

    pub fn from_byte(value: u8) -> Self {
        Self::ALL[usize::from(value) % Self::ALL.len()]
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Themed => "themed",
            Self::White => "white",
            Self::Black => "black",
            Self::Dark => "dark",
            Self::Light => "light",
            Self::Transparent => "transparent",
            Self::PolkaDot => "polka-dot",
            Self::Striped => "striped",
            Self::Checkerboard => "checkerboard",
            Self::Grid => "grid",
            Self::Sunrise => "sunrise",
            Self::Ocean => "ocean",
            Self::Starry => "starry",
        }
    }
}

impl FromStr for AvatarBackground {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_ascii_lowercase().as_str() {
            "themed" => Ok(Self::Themed),
            "white" => Ok(Self::White),
            "black" => Ok(Self::Black),
            "dark" => Ok(Self::Dark),
            "light" => Ok(Self::Light),
            "transparent" => Ok(Self::Transparent),
            "polka-dot" | "polka_dot" | "polkadot" => Ok(Self::PolkaDot),
            "striped" | "stripes" => Ok(Self::Striped),
            "checkerboard" | "checker-board" | "checker_board" => Ok(Self::Checkerboard),
            "grid" => Ok(Self::Grid),
            "sunrise" => Ok(Self::Sunrise),
            "ocean" => Ok(Self::Ocean),
            "starry" | "stars" => Ok(Self::Starry),
            _ => Err("unsupported avatar background"),
        }
    }
}

impl std::fmt::Display for AvatarBackground {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Optional avatar accessory layer.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AvatarAccessory {
    /// No accessory layer.
    #[default]
    None,
    /// Simple glasses overlay.
    Glasses,
    /// Hat overlay.
    Hat,
    /// Headphones overlay.
    Headphones,
    /// Crown overlay.
    Crown,
    /// Bowtie overlay.
    Bowtie,
    /// Eyepatch overlay.
    Eyepatch,
    /// Scarf overlay.
    Scarf,
    /// Halo overlay.
    Halo,
    /// Horn overlay.
    Horns,
}

impl AvatarAccessory {
    pub const ALL: &'static [Self] = &[
        Self::None,
        Self::Glasses,
        Self::Hat,
        Self::Headphones,
        Self::Crown,
        Self::Bowtie,
        Self::Eyepatch,
        Self::Scarf,
        Self::Halo,
        Self::Horns,
    ];

    pub fn from_byte(value: u8) -> Self {
        Self::ALL[usize::from(value) % Self::ALL.len()]
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Glasses => "glasses",
            Self::Hat => "hat",
            Self::Headphones => "headphones",
            Self::Crown => "crown",
            Self::Bowtie => "bowtie",
            Self::Eyepatch => "eyepatch",
            Self::Scarf => "scarf",
            Self::Halo => "halo",
            Self::Horns => "horns",
        }
    }
}

impl FromStr for AvatarAccessory {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_ascii_lowercase().as_str() {
            "none" => Ok(Self::None),
            "glasses" => Ok(Self::Glasses),
            "hat" => Ok(Self::Hat),
            "headphones" => Ok(Self::Headphones),
            "crown" => Ok(Self::Crown),
            "bowtie" | "bow-tie" | "bow_tie" => Ok(Self::Bowtie),
            "eyepatch" | "eye-patch" | "eye_patch" => Ok(Self::Eyepatch),
            "scarf" => Ok(Self::Scarf),
            "halo" => Ok(Self::Halo),
            "horns" => Ok(Self::Horns),
            _ => Err("unsupported avatar accessory"),
        }
    }
}

impl std::fmt::Display for AvatarAccessory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Optional avatar accent color palette.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AvatarColor {
    /// Family default colors.
    #[default]
    Default,
    /// Bright mint accent.
    NeonMint,
    /// Soft pink accent.
    PastelPink,
    /// Deep red accent.
    Crimson,
    /// Warm gold accent.
    Gold,
    /// Blue-green accent.
    DeepSeaBlue,
}

impl AvatarColor {
    pub const ALL: &'static [Self] = &[
        Self::Default,
        Self::NeonMint,
        Self::PastelPink,
        Self::Crimson,
        Self::Gold,
        Self::DeepSeaBlue,
    ];

    pub fn from_byte(value: u8) -> Self {
        Self::ALL[usize::from(value) % Self::ALL.len()]
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::NeonMint => "neon-mint",
            Self::PastelPink => "pastel-pink",
            Self::Crimson => "crimson",
            Self::Gold => "gold",
            Self::DeepSeaBlue => "deep-sea-blue",
        }
    }
}

impl FromStr for AvatarColor {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_ascii_lowercase().as_str() {
            "default" => Ok(Self::Default),
            "neon-mint" | "neon_mint" | "neonmint" => Ok(Self::NeonMint),
            "pastel-pink" | "pastel_pink" | "pastelpink" => Ok(Self::PastelPink),
            "crimson" => Ok(Self::Crimson),
            "gold" => Ok(Self::Gold),
            "deep-sea-blue" | "deep_sea_blue" | "deepseablue" => Ok(Self::DeepSeaBlue),
            _ => Err("unsupported avatar color"),
        }
    }
}

impl std::fmt::Display for AvatarColor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Optional avatar expression layer.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AvatarExpression {
    /// Family default expression.
    #[default]
    Default,
    /// Happy expression overlay.
    Happy,
    /// Grumpy expression overlay.
    Grumpy,
    /// Surprised expression overlay.
    Surprised,
    /// Sleepy expression overlay.
    Sleepy,
    /// Winking expression overlay.
    Winking,
    /// Cool expression overlay.
    Cool,
    /// Crying expression overlay.
    Crying,
}

impl AvatarExpression {
    pub const ALL: &'static [Self] = &[
        Self::Default,
        Self::Happy,
        Self::Grumpy,
        Self::Surprised,
        Self::Sleepy,
        Self::Winking,
        Self::Cool,
        Self::Crying,
    ];

    pub fn from_byte(value: u8) -> Self {
        Self::ALL[usize::from(value) % Self::ALL.len()]
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Happy => "happy",
            Self::Grumpy => "grumpy",
            Self::Surprised => "surprised",
            Self::Sleepy => "sleepy",
            Self::Winking => "winking",
            Self::Cool => "cool",
            Self::Crying => "crying",
        }
    }
}

impl FromStr for AvatarExpression {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_ascii_lowercase().as_str() {
            "default" => Ok(Self::Default),
            "happy" => Ok(Self::Happy),
            "grumpy" => Ok(Self::Grumpy),
            "surprised" => Ok(Self::Surprised),
            "sleepy" => Ok(Self::Sleepy),
            "winking" | "wink" => Ok(Self::Winking),
            "cool" => Ok(Self::Cool),
            "crying" => Ok(Self::Crying),
            _ => Err("unsupported avatar expression"),
        }
    }
}

impl std::fmt::Display for AvatarExpression {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Optional frame shape for the generated avatar.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum AvatarShape {
    /// Default square canvas.
    #[default]
    Square,
    /// Circular frame.
    Circle,
    /// Rounded rectangle frame.
    Squircle,
    /// Hexagonal frame.
    Hexagon,
    /// Octagonal frame.
    Octagon,
}

impl AvatarShape {
    pub const ALL: &'static [Self] = &[
        Self::Square,
        Self::Circle,
        Self::Squircle,
        Self::Hexagon,
        Self::Octagon,
    ];

    pub fn from_byte(value: u8) -> Self {
        Self::ALL[usize::from(value) % Self::ALL.len()]
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Square => "square",
            Self::Circle => "circle",
            Self::Squircle => "squircle",
            Self::Hexagon => "hexagon",
            Self::Octagon => "octagon",
        }
    }
}

impl FromStr for AvatarShape {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim().to_ascii_lowercase().as_str() {
            "square" => Ok(Self::Square),
            "circle" => Ok(Self::Circle),
            "squircle" => Ok(Self::Squircle),
            "hexagon" => Ok(Self::Hexagon),
            "octagon" => Ok(Self::Octagon),
            _ => Err("unsupported avatar shape"),
        }
    }
}

impl std::fmt::Display for AvatarShape {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

#[cfg(feature = "serde")]
macro_rules! impl_serde_string_enum {
    ($ty:ty) => {
        impl serde::Serialize for $ty {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                serializer.serialize_str(self.as_str())
            }
        }

        impl<'de> serde::Deserialize<'de> for $ty {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                let value = <&str as serde::Deserialize>::deserialize(deserializer)?;
                value.parse().map_err(serde::de::Error::custom)
            }
        }
    };
}

#[cfg(feature = "serde")]
impl_serde_string_enum!(AvatarOutputFormat);
#[cfg(feature = "serde")]
impl_serde_string_enum!(AvatarKind);
#[cfg(feature = "serde")]
impl_serde_string_enum!(AvatarBackground);
#[cfg(feature = "serde")]
impl_serde_string_enum!(AvatarAccessory);
#[cfg(feature = "serde")]
impl_serde_string_enum!(AvatarColor);
#[cfg(feature = "serde")]
impl_serde_string_enum!(AvatarExpression);
#[cfg(feature = "serde")]
impl_serde_string_enum!(AvatarShape);

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct AvatarOptions {
    pub kind: AvatarKind,
    pub background: AvatarBackground,
}

impl AvatarOptions {
    pub const fn new(kind: AvatarKind, background: AvatarBackground) -> Self {
        Self { kind, background }
    }
}

/// Full avatar style selection including the baseline kind/background and
/// optional visual layers.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct AvatarStyleOptions {
    pub kind: AvatarKind,
    pub background: AvatarBackground,
    pub accessory: AvatarAccessory,
    pub color: AvatarColor,
    pub expression: AvatarExpression,
    pub shape: AvatarShape,
}

impl AvatarStyleOptions {
    pub const fn new(
        kind: AvatarKind,
        background: AvatarBackground,
        accessory: AvatarAccessory,
        color: AvatarColor,
        expression: AvatarExpression,
        shape: AvatarShape,
    ) -> Self {
        Self {
            kind,
            background,
            accessory,
            color,
            expression,
            shape,
        }
    }

    pub const fn from_options(options: AvatarOptions) -> Self {
        Self {
            kind: options.kind,
            background: options.background,
            accessory: AvatarAccessory::None,
            color: AvatarColor::Default,
            expression: AvatarExpression::Default,
            shape: AvatarShape::Square,
        }
    }

    pub fn from_identity(identity: &AvatarIdentity) -> Self {
        Self {
            kind: AvatarKind::from_byte(identity.byte(AVATAR_STYLE_KIND_BYTE)),
            background: AvatarBackground::from_byte(identity.byte(AVATAR_STYLE_BACKGROUND_BYTE)),
            accessory: AvatarAccessory::from_byte(identity.byte(AVATAR_STYLE_ACCESSORY_BYTE)),
            color: AvatarColor::from_byte(identity.byte(AVATAR_STYLE_COLOR_BYTE)),
            expression: AvatarExpression::from_byte(identity.byte(AVATAR_STYLE_EXPRESSION_BYTE)),
            shape: AvatarShape::from_byte(identity.byte(AVATAR_STYLE_SHAPE_BYTE)),
        }
    }

    pub const fn legacy_options(self) -> AvatarOptions {
        AvatarOptions::new(self.kind, self.background)
    }

    pub fn summary(self) -> String {
        self.to_string()
    }

    pub(crate) const fn has_extra_layers(self) -> bool {
        !matches!(self.accessory, AvatarAccessory::None)
            || !matches!(self.color, AvatarColor::Default)
            || !matches!(self.expression, AvatarExpression::Default)
            || !matches!(self.shape, AvatarShape::Square)
    }
}

impl From<AvatarOptions> for AvatarStyleOptions {
    fn from(options: AvatarOptions) -> Self {
        Self::from_options(options)
    }
}

impl From<AvatarStyleOptions> for AvatarOptions {
    fn from(options: AvatarStyleOptions) -> Self {
        options.legacy_options()
    }
}

impl std::fmt::Display for AvatarStyleOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{} / {} / {} / {} / {} / {}",
            self.kind, self.background, self.accessory, self.color, self.expression, self.shape
        )
    }
}