blue_build_utils/
macros.rs

1/// Easily create a `String`.
2#[macro_export]
3macro_rules! string {
4    ($str:expr) => {
5        String::from($str)
6    };
7}
8
9/// Easily create a `Cow<'_, str>`.
10#[macro_export]
11macro_rules! cowstr {
12    ($str:expr) => {
13        ::std::borrow::Cow::<'_, str>::from($str)
14    };
15}
16
17/// Easily create a `Vec<String>`.
18/// Uses the same syntax as `vec![]`.
19#[macro_export]
20macro_rules! string_vec {
21    ($($string:expr),* $(,)?) => {
22        {
23            vec![
24                $($crate::string!($string),)*
25            ]
26        }
27    };
28}
29
30/// Easily create a `Vec<Cow<'_, str>>`.
31/// Uses the same syntax as `vec![]`.
32#[macro_export]
33macro_rules! cowstr_vec {
34    ($($string:expr),* $(,)?) => {
35        {
36            vec![
37                $($crate::cowstr!($string),)*
38            ]
39        }
40    };
41}
42
43#[macro_export]
44macro_rules! impl_de_fromstr {
45    ($($typ:ty),* $(,)?) => {
46        $(
47            impl TryFrom<&str> for $typ {
48                type Error = miette::Error;
49
50                fn try_from(value: &str) -> Result<Self, Self::Error> {
51                    value.parse()
52                }
53            }
54
55            impl TryFrom<&String> for $typ {
56                type Error = miette::Error;
57
58                fn try_from(value: &String) -> Result<Self, Self::Error> {
59                    Self::try_from(value.as_str())
60                }
61            }
62
63            impl TryFrom<String> for $typ {
64                type Error = miette::Error;
65
66                fn try_from(value: String) -> Result<Self, Self::Error> {
67                    Self::try_from(value.as_str())
68                }
69            }
70
71            impl<'de> serde::de::Deserialize<'de> for $typ {
72                fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
73                where
74                    D: serde::Deserializer<'de>,
75                {
76                    Self::try_from(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
77                }
78            }
79        )*
80    };
81}
82
83#[macro_export]
84macro_rules! sudo_cmd {
85    (
86        prompt = $prompt:expr,
87        sudo_check = $sudo_check:expr,
88        $command:expr,
89        $($rest:tt)*
90    ) => {
91        {
92            let _use_sudo = ($sudo_check) && !$crate::running_as_root();
93
94            ::comlexr::cmd!(
95                if _use_sudo {
96                    "sudo"
97                } else {
98                    $command
99                },
100                if _use_sudo && $crate::has_env_var($crate::constants::SUDO_ASKPASS) => [
101                    "-A",
102                    "-p",
103                    $prompt,
104                ],
105                if _use_sudo => [
106                    "--preserve-env",
107                    $command,
108                ],
109                $($rest)*
110            )
111        }
112    };
113    (
114        sudo_check = $sudo_check:expr,
115        $command:expr,
116        $($rest:tt)*
117    ) => {
118        {
119            let _use_sudo = ($sudo_check) && !$crate::running_as_root();
120
121            ::comlexr::cmd!(
122                if _use_sudo {
123                    "sudo"
124                } else {
125                    $command
126                },
127                if _use_sudo && $crate::has_env_var($crate::constants::SUDO_ASKPASS) => [
128                    "-A",
129                    "-p",
130                    $crate::constants::SUDO_PROMPT,
131                ],
132                if _use_sudo => [
133                    "--preserve-env",
134                    $command,
135                ],
136                $($rest)*
137            )
138        }
139    };
140    (
141        prompt = $prompt:expr,
142        $command:expr,
143        $($rest:tt)*
144    ) => {
145        {
146            let _use_sudo = !$crate::running_as_root();
147
148            ::comlexr::cmd!(
149                if _use_sudo {
150                    "sudo"
151                } else {
152                    $command
153                },
154                if _use_sudo && $crate::has_env_var($crate::constants::SUDO_ASKPASS) => [
155                    "-A",
156                    "-p",
157                    $prompt,
158                ],
159                if _use_sudo => [
160                    "--preserve-env",
161                    $command,
162                ],
163                $($rest)*
164            )
165        }
166    };
167    (
168        $command:expr,
169        $($rest:tt)*
170    ) => {
171        {
172            let _use_sudo = !$crate::running_as_root();
173
174            ::comlexr::cmd!(
175                if _use_sudo {
176                    "sudo"
177                } else {
178                    $command
179                },
180                if _use_sudo && $crate::has_env_var($crate::constants::SUDO_ASKPASS) => [
181                    "-A",
182                    "-p",
183                    $crate::constants::SUDO_PROMPT,
184                ],
185                if _use_sudo => [
186                    "--preserve-env",
187                    $command,
188                ],
189                $($rest)*
190            )
191        }
192    };
193}