1use schemars::JsonSchema;
2use serde::{Deserialize, Deserializer, Serialize};
3
4#[derive(Debug, Clone, PartialEq, Serialize, JsonSchema)]
13#[serde(untagged)]
14pub enum StringOrBool {
15 Bool(bool),
16 String(String),
17}
18
19impl StringOrBool {
20 pub fn as_bool(&self) -> bool {
23 match self {
24 StringOrBool::Bool(b) => *b,
25 StringOrBool::String(s) => matches!(s.trim(), "true" | "1"),
26 }
27 }
28
29 pub fn as_str(&self) -> &str {
31 match self {
32 StringOrBool::Bool(true) => "true",
33 StringOrBool::Bool(false) => "false",
34 StringOrBool::String(s) => s,
35 }
36 }
37
38 pub fn is_template(&self) -> bool {
40 matches!(self, StringOrBool::String(s) if s.contains('{'))
41 }
42
43 pub fn try_evaluates_to_true(
58 &self,
59 render: impl Fn(&str) -> anyhow::Result<String>,
60 ) -> anyhow::Result<bool> {
61 match self {
62 StringOrBool::Bool(b) => Ok(*b),
63 StringOrBool::String(s) => {
64 reject_stale_typed_compare(s, "skip/enable expression")?;
65 let rendered = render(s)?;
66 Ok(matches!(rendered.trim(), "true" | "1"))
67 }
68 }
69 }
70}
71
72impl Default for StringOrBool {
73 fn default() -> Self {
74 StringOrBool::Bool(false)
75 }
76}
77
78fn reject_stale_typed_compare(template: &str, label: &str) -> anyhow::Result<()> {
88 if let Some(snippet) = crate::template::find_stale_typed_compare(template) {
89 anyhow::bail!(
90 "{label}: `{snippet}` compares a typed template variable to a quoted string; \
91 these variables are real booleans/numbers, so the comparison never matches and \
92 the condition would silently evaluate false in every mode. Use the variable \
93 directly instead — e.g. `not IsSnapshot` for `IsSnapshot == \"false\"`, \
94 `IsHarness` for `IsHarness == \"true\"`, or an unquoted numeric compare for \
95 `NightlyBuild`."
96 );
97 }
98 Ok(())
99}
100
101pub fn evaluate_if_condition(
121 condition: Option<&str>,
122 label: &str,
123 render: impl Fn(&str) -> anyhow::Result<String>,
124) -> anyhow::Result<bool> {
125 use anyhow::Context as _;
126 let Some(template) = condition else {
127 return Ok(true);
128 };
129 if template.is_empty() {
130 return Ok(true);
131 }
132 reject_stale_typed_compare(template, label)?;
133 let rendered = render(template).with_context(|| {
134 format!("{label}: `if` template render failed (expression: {template})")
135 })?;
136 let trimmed = rendered.trim();
137 let falsy = matches!(trimmed, "" | "false" | "0" | "no");
138 Ok(!falsy)
139}
140
141pub(crate) fn deserialize_string_or_bool_opt<'de, D>(
143 deserializer: D,
144) -> Result<Option<StringOrBool>, D::Error>
145where
146 D: Deserializer<'de>,
147{
148 use serde::de::{self, Visitor};
149
150 struct StringOrBoolVisitor;
151
152 impl<'de> Visitor<'de> for StringOrBoolVisitor {
153 type Value = Option<StringOrBool>;
154
155 fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
156 f.write_str("a bool, a string, or null")
157 }
158
159 fn visit_bool<E: de::Error>(self, v: bool) -> Result<Self::Value, E> {
160 Ok(Some(StringOrBool::Bool(v)))
161 }
162
163 fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
164 Ok(Some(StringOrBool::String(v.to_owned())))
165 }
166
167 fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
168 Ok(Some(StringOrBool::String(v)))
169 }
170
171 fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
172 Ok(None)
173 }
174
175 fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
176 Ok(None)
177 }
178 }
179
180 deserializer.deserialize_any(StringOrBoolVisitor)
181}
182
183#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, JsonSchema)]
189pub struct HumanDuration(
190 #[serde(serialize_with = "serialize_human_duration")] pub std::time::Duration,
191);
192
193impl HumanDuration {
194 pub fn duration(&self) -> std::time::Duration {
196 self.0
197 }
198
199 pub fn as_humantime_string(&self) -> String {
203 let total_secs = self.0.as_secs();
204 if total_secs == 0 {
205 return format!("{}ms", self.0.as_millis());
207 }
208 let hours = total_secs / 3600;
209 let mins = (total_secs % 3600) / 60;
210 let secs = total_secs % 60;
211 let mut out = String::new();
212 if hours > 0 {
213 out.push_str(&format!("{hours}h"));
214 }
215 if mins > 0 {
216 out.push_str(&format!("{mins}m"));
217 }
218 if secs > 0 || out.is_empty() {
219 out.push_str(&format!("{secs}s"));
220 }
221 out
222 }
223}
224
225impl<'de> Deserialize<'de> for HumanDuration {
226 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
227 where
228 D: Deserializer<'de>,
229 {
230 use serde::de::{self, Visitor};
231
232 struct DurVisitor;
233
234 impl<'de> Visitor<'de> for DurVisitor {
235 type Value = HumanDuration;
236
237 fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
238 f.write_str(
239 "a duration string with unit suffix (e.g. \"10m\", \"15s\", \"1h30m\", \"500ms\")",
240 )
241 }
242
243 fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
244 parse_humantime_duration(v)
245 .map(HumanDuration)
246 .map_err(E::custom)
247 }
248
249 fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
250 self.visit_str(&v)
251 }
252 }
253
254 deserializer.deserialize_str(DurVisitor)
255 }
256}
257
258fn serialize_human_duration<S: serde::Serializer>(
259 d: &std::time::Duration,
260 serializer: S,
261) -> Result<S::Ok, S::Error> {
262 serializer.serialize_str(&HumanDuration(*d).as_humantime_string())
263}
264
265pub(super) fn parse_humantime_duration(input: &str) -> Result<std::time::Duration, String> {
269 let s = input.trim();
270 if s.is_empty() {
271 return Err("empty duration string".to_string());
272 }
273 let mut total = std::time::Duration::ZERO;
274 let mut number_buf = String::new();
275 let mut had_any = false;
276 let mut iter = s.chars().peekable();
277 while let Some(&c) = iter.peek() {
278 if c.is_whitespace() {
279 iter.next();
280 continue;
281 }
282 if c.is_ascii_digit() {
283 number_buf.push(c);
284 iter.next();
285 continue;
286 }
287 if number_buf.is_empty() {
288 return Err(format!("expected digit before unit in '{input}'"));
289 }
290 let mut unit = String::new();
292 unit.push(c);
293 iter.next();
294 if let Some(&next) = iter.peek()
295 && unit == "m"
296 && next == 's'
297 {
298 unit.push('s');
299 iter.next();
300 }
301 let n: u64 = number_buf
302 .parse()
303 .map_err(|e| format!("invalid number '{number_buf}' in '{input}': {e}"))?;
304 let segment = match unit.as_str() {
305 "ms" => std::time::Duration::from_millis(n),
306 "s" => std::time::Duration::from_secs(n),
307 "m" => std::time::Duration::from_secs(n * 60),
308 "h" => std::time::Duration::from_secs(n * 3600),
309 "d" => std::time::Duration::from_secs(n * 86_400),
310 other => return Err(format!("unknown duration unit '{other}' in '{input}'")),
311 };
312 total += segment;
313 number_buf.clear();
314 had_any = true;
315 }
316 if !number_buf.is_empty() {
317 return Err(format!(
318 "trailing number '{number_buf}' without a unit in '{input}'"
319 ));
320 }
321 if !had_any {
322 return Err(format!("no duration components found in '{input}'"));
323 }
324 Ok(total)
325}
326
327#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, JsonSchema)]
335#[serde(transparent)]
336pub struct StringOrU32(#[serde(deserialize_with = "deserialize_u32_from_string_or_int")] pub u32);
337
338impl StringOrU32 {
339 pub fn value(&self) -> u32 {
341 self.0
342 }
343}
344
345fn deserialize_u32_from_string_or_int<'de, D>(deserializer: D) -> Result<u32, D::Error>
347where
348 D: Deserializer<'de>,
349{
350 use serde::de::{self, Visitor};
351
352 struct U32Visitor;
353
354 impl<'de> Visitor<'de> for U32Visitor {
355 type Value = u32;
356
357 fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
358 f.write_str("a u32 integer or a string parseable as octal/decimal (e.g. 18, \"0o022\", \"022\")")
359 }
360
361 fn visit_u64<E: de::Error>(self, v: u64) -> Result<Self::Value, E> {
362 u32::try_from(v).map_err(|_| E::custom(format!("value {v} does not fit in u32")))
363 }
364
365 fn visit_i64<E: de::Error>(self, v: i64) -> Result<Self::Value, E> {
366 u32::try_from(v).map_err(|_| E::custom(format!("value {v} does not fit in u32")))
367 }
368
369 fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
370 let trimmed = v.trim();
371 if let Some(rest) = trimmed
372 .strip_prefix("0o")
373 .or_else(|| trimmed.strip_prefix("0O"))
374 {
375 return u32::from_str_radix(rest, 8)
376 .map_err(|e| E::custom(format!("invalid octal '{v}': {e}")));
377 }
378 if trimmed.starts_with('0') && trimmed.len() > 1 {
381 return u32::from_str_radix(trimmed, 8)
382 .map_err(|e| E::custom(format!("invalid octal '{v}': {e}")));
383 }
384 trimmed
385 .parse::<u32>()
386 .map_err(|e| E::custom(format!("invalid u32 '{v}': {e}")))
387 }
388
389 fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
390 self.visit_str(&v)
391 }
392 }
393
394 deserializer.deserialize_any(U32Visitor)
395}
396
397pub(super) fn deserialize_string_or_vec_opt<'de, D>(
400 deserializer: D,
401) -> Result<Option<Vec<String>>, D::Error>
402where
403 D: Deserializer<'de>,
404{
405 use serde::de::{self, Visitor};
406
407 struct StringOrVecVisitor;
408
409 impl<'de> Visitor<'de> for StringOrVecVisitor {
410 type Value = Option<Vec<String>>;
411
412 fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
413 f.write_str("a string, a list of strings, or null")
414 }
415
416 fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
417 Ok(Some(vec![v.to_owned()]))
418 }
419
420 fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
421 Ok(Some(vec![v]))
422 }
423
424 fn visit_seq<A: de::SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
425 let mut items = Vec::new();
426 while let Some(item) = seq.next_element::<String>()? {
427 items.push(item);
428 }
429 Ok(Some(items))
430 }
431
432 fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
433 Ok(None)
434 }
435
436 fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
437 Ok(None)
438 }
439 }
440
441 deserializer.deserialize_any(StringOrVecVisitor)
442}