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
/*
 *   Copyright (c) 2022 R3BL LLC
 *   All rights reserved.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

use serde::{Deserialize, Serialize};

use crate::*;

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Stylesheet {
    pub styles: Vec<Style>,
}

#[macro_export]
macro_rules! get_style {
    (
        @from_result: $arg_stylesheet_result : expr, // Eg: from: stylesheet,
        $arg_style_name : expr                      // Eg: "style1"
    ) => {
        if let Ok(ref it) = $arg_stylesheet_result {
            it.find_style_by_id($arg_style_name)
        } else {
            None
        }
    };

    (
        @from: $arg_stylesheet : expr, // Eg: from: stylesheet,
        $arg_style_name : expr        // Eg: "style1"
    ) => {
        $arg_stylesheet.find_style_by_id($arg_style_name)
    };
}

#[macro_export]
macro_rules! get_styles {
    (
        @from_result: $arg_stylesheet_result : expr, // Eg: from: stylesheet,
        [$($args:tt)*]                              // Eg: ["style1", "style2"]
    ) => {
        if let Ok(ref it) = $arg_stylesheet_result {
            it.find_styles_by_ids(vec![$($args)*])
        } else {
            None
        }
    };

    (
        @from: $arg_stylesheet : expr, // Eg: from: stylesheet,
        [$($args:tt)*]                // Eg: ["style1", "style2"]
    ) => {
        $arg_stylesheet.find_styles_by_ids(vec![$($args)*])
    };
}

impl Stylesheet {
    pub fn new() -> Self { Self::default() }

    pub fn add_style(&mut self, style: Style) -> CommonResult<()> {
        throws!({
            if style.id == u8::MAX {
                return CommonError::new_err_with_only_msg("Style id must be defined");
            }
            self.styles.push(style);
        });
    }

    pub fn add_styles(&mut self, styles: Vec<Style>) -> CommonResult<()> {
        throws!({
            for style in styles {
                self.add_style(style)?;
            }
        });
    }

    pub fn find_style_by_id(&self, id: u8) -> Option<Style> {
        self.styles.iter().find(|style| style.id == id).cloned()
    }

    /// Returns [None] if no style in `ids` [Vec] is found.
    pub fn find_styles_by_ids(&self, ids: Vec<u8>) -> Option<Vec<Style>> {
        let mut styles = Vec::new();

        for id in ids {
            if let Some(style) = self.find_style_by_id(id) {
                styles.push(style);
            }
        }

        if styles.is_empty() {
            None
        } else {
            styles.into()
        }
    }

    pub fn compute(styles: &Option<Vec<Style>>) -> Option<Style> {
        if let Some(styles) = styles {
            let mut computed = Style::default();
            styles.iter().for_each(|style| computed += style);
            computed.into()
        } else {
            None
        }
    }
}

/// Macro to make building [Stylesheet] easy. This returns a [CommonResult] because it checks to see
/// that all [Style]s that are added have an `id`. If they don't, then an a [CommonError] is thrown.
/// This is to ensure that valid styles are added to a stylesheet. Without an `id`, they can't be
/// retrieved after they're added here, rendering them useless.
///
/// Here's an example.
/// ```ignore
/// fn create_stylesheet() -> CommonResult<Stylesheet> {
///   throws_with_return!({
///     stylesheet! {
///         style! {
///           id: style1
///           padding: 1
///           color_bg: Color::Rgb { r: 55, g: 55, b: 248 }
///         },
///         vec![
///             style! {
///                 id: style1
///                 padding: 1
///                 color_bg: Color::Rgb { r: 55, g: 55, b: 248 }
///             },
///             style! {
///                 id: style2
///                 padding: 1
///                 color_bg: Color::Rgb { r: 85, g: 85, b: 255 }
///             },
///         ]
///     }
///   })
/// }
/// ```
#[macro_export]
macro_rules! stylesheet {
    (
        $($style:expr),*
        $(,)* /* Optional trailing comma https://stackoverflow.com/a/43143459/2085356. */
    ) => {
    {
        let mut stylesheet = Stylesheet::new();
            $(
                stylesheet.try_add($style)?;
            )*
            stylesheet
        }
    };
}

/// This trait exists to allow "pseudo operator overloading". Rust does not support operator
/// overloading, and the method to add a single style has a different signature than the one to add
/// a vector of styles. To get around this, the [TryAdd] trait is implemented for both [Style] and
/// [`Vec<Style>`]. Then the [stylesheet!] macro can "pseudo overload" them.
pub trait TryAdd<OtherType = Self> {
    fn try_add(&mut self, other: OtherType) -> CommonResult<()>;
}

impl TryAdd<Style> for Stylesheet {
    fn try_add(&mut self, other: Style) -> CommonResult<()> { self.add_style(other) }
}

impl TryAdd<Vec<Style>> for Stylesheet {
    fn try_add(&mut self, other: Vec<Style>) -> CommonResult<()> {
        self.add_styles(other)
    }
}