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
use std::path::{Path, PathBuf};

// Helper trait for getting CSS from a string or path.
pub trait IntoCssStr: 'static {
    fn get_style(&self) -> Result<String, std::io::Error>;
}

impl IntoCssStr for CSS {
    fn get_style(&self) -> Result<String, std::io::Error> {
        match self {
            CSS::Path(path) => std::fs::read_to_string(path),

            CSS::String(style_string) => Ok(style_string.to_owned()),
        }
    }
}

impl IntoCssStr for &'static str {
    fn get_style(&self) -> Result<String, std::io::Error> {
        Ok(self.to_string())
    }
}

impl IntoCssStr for PathBuf {
    fn get_style(&self) -> Result<String, std::io::Error> {
        std::fs::read_to_string(self)
    }
}

impl IntoCssStr for Path {
    fn get_style(&self) -> Result<String, std::io::Error> {
        std::fs::read_to_string(self)
    }
}

#[doc(hidden)]
pub enum CSS {
    Path(PathBuf),
    String(String),
}

impl CSS {
    pub fn from_string(style: &str) -> Self {
        Self::String(style.to_owned())
    }

    pub fn from_file(path: impl AsRef<Path>) -> Self {
        Self::Path(path.as_ref().to_owned())
    }
}

impl From<&str> for CSS {
    fn from(value: &str) -> Self {
        CSS::from_string(value)
    }
}

impl From<PathBuf> for CSS {
    fn from(value: PathBuf) -> Self {
        CSS::from_file(value)
    }
}

#[cfg(debug_assertions)]
#[macro_export]
macro_rules! include_style {
    ($filename:tt) => {
        $crate::util::CSS::from_file(concat!(env!("CARGO_MANIFEST_DIR"), "/", $filename))
    };
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! include_style {
    ($filename:tt) => {
        $crate::prelude::CSS::from_string(include_str!(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/",
            $filename
        )))
    };
}