console_display/
macros.rs

1/// Implements getters for properties on a struct.
2/// May take attributes and visibility.
3///
4/// # Examples
5/// ```
6/// use console_display::impl_getters;
7/// struct Example {
8///     flag: bool,
9///     data: [String; 8],
10/// }
11///
12/// impl Example {
13///     impl_getters!(
14///         #[must_use] pub flag: bool,
15///         data: [String; 8]
16///     );
17/// }
18/// ```
19#[macro_export]
20macro_rules! impl_getters {
21    ($($(#[$attr:meta])* $visibility:vis $field:ident: $type:ty),*) => {
22        $(
23            $(#[$attr])*
24            $visibility fn $field(&self) -> &$type {
25                &self.$field
26            }
27        )*
28    };
29    ($($(#[$attr:meta])* $visibility:vis const $field:ident: $type:ty),*) => {
30        $(
31            $(#[$attr])*
32            $visibility const fn $field(&self) -> &$type {
33                &self.$field
34            }
35        )*
36    };
37}
38
39/// Implements mutable getters for properties on a struct.
40///
41/// # Examples
42/// ```
43/// use console_display::impl_getters_mut;
44/// struct Example {
45///     flag: bool,
46///     data: [String; 8],
47/// }
48///
49/// impl Example {
50///     impl_getters_mut!(
51///         data: [String; 8]
52///     );
53/// }
54/// ```
55#[macro_export]
56macro_rules! impl_getters_mut {
57    ($($visibility:vis $field:ident: $type:ty),*) => {
58        $(paste::paste!{
59            $visibility fn [<$field _mut>](&mut self) -> &mut $type {
60                &mut self.$field
61            }
62        })*
63    };
64    ($($visibility:vis const $field:ident: $type:ty),*) => {
65        $(paste::paste!{
66            $visibility const fn [<$field _mut>](&mut self) -> &mut $type {
67                &mut self.$field
68            }
69        })*
70    };
71}
72
73/// Implements setters for properties on a struct.
74/// May take a visibility for each setter.
75///
76/// # Examples
77/// ```
78/// use console_display::impl_setters;
79/// struct Example {
80///     flag: bool,
81/// }
82///
83/// impl Example {
84///     impl_setters!(
85///         pub flag: bool
86///     );
87/// }
88/// ```
89#[macro_export]
90macro_rules! impl_setters {
91    ($($visibility:vis $field:ident: $type:ty),*) => {
92        $(paste::paste!{
93            $visibility fn [<set_ $field>](&mut self, val: $type) {
94                self.$field = val;
95            }
96        })*
97    };
98    ($($visibility:vis const $field:ident: $type:ty),*) => {
99        $(paste::paste!{
100            $visibility const fn [<set_ $field>](&mut self, val: $type) {
101                self.$field = val;
102            }
103        })*
104    };
105}
106
107/// Implements new for a struct.
108/// May take a visibility and generics.
109///
110/// # Examples
111/// ```
112/// use console_display::impl_new;
113/// struct Example {
114///     flag: bool,
115///     data: [String; 8],
116/// }
117///
118/// impl Example {
119///     impl_new!(
120///         pub Example,
121///         flag: bool,
122///         data: [String; 8]
123///     );
124/// }
125///
126/// struct GenericExample<T> {
127///     flag: T,
128///     data: [String; 8],
129/// }
130///
131/// impl<T> GenericExample<T> {
132///     impl_new!(
133///         pub GenericExample<T>,
134///         flag: T,
135///         data: [String; 8]
136///     );
137/// }
138/// ```
139#[macro_export]
140macro_rules! impl_new {
141    ($(#[$attr:meta])* $visibility:vis $struct:ident$(< $($generic:ty),* >)?, $($arg:ident: $type:ty), *) => {
142        $(#[$attr])*
143        $visibility fn new($($arg: $type),*) -> $struct$(< $($generic),* >)?  {
144            $struct {
145                $($arg), *
146            }
147        }
148    };
149    ($(#[$attr:meta])* $visibility:vis const $struct:ident$(< $($generic:ty),* >)?, $($arg:ident: $type:ty), *) => {
150        $(#[$attr])*
151        $visibility const fn new($($arg: $type),*) -> $struct$(< $($generic),* >)?  {
152            $struct {
153                $($arg), *
154            }
155        }
156    };
157}
158
159/// Constrains a constant generic parameter with the given constraint.
160/// Should be used in conjunction with the `or` and `and` macros.
161///
162/// # Examples
163/// ```
164/// #![allow(incomplete_features)]
165/// #![feature(generic_const_exprs)]
166///
167/// use console_display::constraint;
168/// use console_display::and;
169///
170/// fn subpixel_static<const X: usize, const Y: usize>() -> u8
171/// where
172///     constraint!(and!(X < 10, Y < 20)):,
173/// {
174///     generate_vec()[X + Y * 10]
175/// }
176///
177/// fn generate_vec() -> Vec<u8> {
178///     let mut vec = Vec::with_capacity(200);
179///     for i in 0..200 {
180///         vec.push(i);
181///     }
182///     vec
183/// }
184/// ```
185#[macro_export]
186macro_rules! constraint {
187    {$x:expr} => {
188        [(); 0 - !$x as usize]
189    };
190}
191
192/// Inserts a eagerly evaluated `or` into a `constraint`.
193/// See [constraint] for examples and usage.
194#[macro_export]
195macro_rules! or {
196    ($x:expr, $y:expr) => {
197        ($x as u8 | $y as u8) == 1
198    };
199}
200
201/// Inserts a eagerly evaluated `and` into a `constraint`.
202/// See [constraint] for examples and usage.
203#[macro_export]
204macro_rules! and {
205    ($x:expr, $y:expr) => {
206        ($x as u8 & $y as u8) == 1
207    };
208}
209
210#[macro_export]
211macro_rules! impl_from_mono_chrome_pixel_for_datacell {
212    ($type:ty) => {
213        impl From<$type> for DataCell {
214            fn from(val: $type) -> Self {
215                Self {
216                    character: val.character(),
217                    foreground: TerminalColor::Default,
218                    background: TerminalColor::Default,
219                }
220            }
221        }
222    };
223}
224
225#[macro_export]
226macro_rules! impl_from_color_pixel_for_datacell {
227    ($type:ty, $base:ty) => {
228        impl From<$type> for DataCell {
229            fn from(val: $type) -> Self {
230                let colors = val.pixels();
231                let grouping = TerminalColor::group(&colors);
232                let symb = <$base>::new(grouping).character();
233
234                let mut col1 = vec![];
235                let mut col2 = vec![];
236                for i in 0..grouping.len() {
237                    if grouping[i] {
238                        col1.push(colors[i]);
239                    }
240                    else {
241                        col2.push(colors[i]);
242                    }
243                }
244                let col1 = TerminalColor::mix(&col1);
245                let col2 = TerminalColor::mix(&col2);
246
247                Self {
248                    character: symb,
249                    foreground: col1,
250                    background: col2,
251                }
252            }
253        }
254    };
255}