fluent_ansi/
styling_attribute.rs

1/// A trait to represent an attribute that can be set in or retrieved from any [composed styling value](crate#composed-styling-types).
2pub trait StylingAttribute<S> {
3    /// The type of value associated with this attribute.
4    type Value: Default;
5
6    /// Sets this attribute in the given parameter, returning it updated.
7    #[must_use]
8    fn set_in(self, composed_styling: S, value: Self::Value) -> S;
9
10    /// Gets the value of this attribute from the given parameter.
11    #[must_use]
12    fn get_from(self, composed_styling: &S) -> Self::Value;
13}
14
15macro_rules! impl_styling_atribute_for {
16    {$type:ty {
17        type Value = $value_type:ty;
18        args: [$self:ident, $composed_styling:ident, $value:ident];
19        set_in: $set_in:block
20        get_from: $get_from:block
21    }} => {
22        impl $crate::StylingAttribute<$crate::Style> for $type {
23            type Value = $value_type;
24
25            fn set_in($self, $composed_styling: $crate::Style, $value: Self::Value) -> $crate::Style {
26                $set_in
27            }
28
29            fn get_from($self, $composed_styling: &$crate::Style) -> Self::Value {
30                $get_from
31            }
32        }
33
34        impl<C: Display> $crate::StylingAttribute<$crate::Styled<C>> for $type {
35            type Value = $value_type;
36
37            fn set_in($self, $composed_styling: $crate::Styled<C>, $value: Self::Value) -> $crate::Styled<C> {
38                $set_in
39            }
40
41            fn get_from($self, $composed_styling: &$crate::Styled<C>) -> Self::Value {
42                $get_from
43            }
44        }
45    };
46}
47pub(crate) use impl_styling_atribute_for;