fluent_ansi/effect/
underline.rs

1use core::fmt::{Display, Formatter, Result};
2
3use enum_iterator::Sequence;
4
5use crate::{
6    Effect,
7    impl_macros::{additive_styling::impl_additive_styling_type, from_to::impl_from_to},
8    impl_styling_atribute_for, impl_styling_element_for,
9};
10
11pub(crate) type AllUnderlineEffects = enum_iterator::All<UnderlineEffect>;
12
13/// An enumeration of all supported underline effects.
14///
15/// The values correspond to a subset of [`Effect`].
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Sequence)]
17pub enum UnderlineEffect {
18    /// Solid underline styling.
19    #[default]
20    Solid,
21    /// Curly underline styling.
22    Curly,
23    /// Dotted underline styling.
24    Dotted,
25    /// Dashed underline styling.
26    Dashed,
27    /// Double underline styling.
28    Double,
29}
30
31impl UnderlineEffect {
32    #[must_use]
33    pub(crate) fn all() -> AllUnderlineEffects {
34        enum_iterator::all()
35    }
36}
37
38impl_additive_styling_type!(UnderlineEffect {
39    args: [self];
40    to_style: { self.to_effect().to_style() }
41});
42
43impl_from_to!(
44    #[doc = r"Converts the type into an [`Effect`]."]
45    fn to_effect(self: UnderlineEffect) -> Effect {
46        match self {
47            UnderlineEffect::Solid => Effect::SolidUnderline,
48            UnderlineEffect::Curly => Effect::CurlyUnderline,
49            UnderlineEffect::Dotted => Effect::DottedUnderline,
50            UnderlineEffect::Dashed => Effect::DashedUnderline,
51            UnderlineEffect::Double => Effect::DoubleUnderline,
52        }
53    }
54);
55
56impl Display for UnderlineEffect {
57    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
58        self.to_effect().fmt(f)
59    }
60}
61
62impl_styling_element_for! { UnderlineEffect {
63    args: [self, composed_styling];
64    add_to: {
65        composed_styling.set_underline_effect(Some(self))
66    }
67}}
68
69impl_styling_atribute_for! { UnderlineEffect {
70    type Value = bool;
71    args: [self, composed_styling, value];
72
73    set_in: {
74        composed_styling.set_effect(self.to_effect(), value)
75    }
76
77    get_from: {
78        composed_styling.get_effect(self.to_effect())
79    }
80}}
81
82/// The underline attribute.
83///
84/// Usable in the
85/// [`Style::set`](crate::Style::set)/[`Styled::set`](crate::Styled::set) and
86/// [`Style::get`](crate::Style::get)/[`Styled::get`](crate::Styled::get) methods.
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
88pub struct UnderlineStyle;
89
90impl_styling_atribute_for! { UnderlineStyle {
91    type Value = Option<UnderlineEffect>;
92    args: [self, composed_styling, value];
93
94    set_in: {
95        composed_styling.set_underline_effect(value)
96    }
97
98    get_from: {
99        composed_styling.get_underline_effect()
100    }
101}}