zenops 0.16.0

Declarative system configuration management for shell config and dotfiles.
Documentation
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! The `detect` configuration language: leaf checks (`File`, `Which`),
//! combinators (`Any`, `All`), and a host gate (`When`) that compose them.
//! Used by [`super::PkgConfig`] to decide whether a configured pkg is
//! installed on the current host. Pkg-level host gating still lives on
//! `pkg.*.when` (see [`super::PkgConfig::when`]); `When` here is the
//! detect-local form that lets a single pkg express OS-divergent detect
//! paths without splitting into two `[pkg.*]` entries.
//!
//! Each variant is identified on the wire by which key is present in the
//! table: `exists` → [`File`], `which` → [`Which`], `any` → [`Any`],
//! `all` → [`All`], `when` (paired with `then`) → [`When`]. The custom
//! [`Deserialize`] impl rejects empty tables, unknown kinds, and tables
//! that mix kinds, with explicit error messages naming the offender.
//!
//! [`File`]: DetectStrategy::File
//! [`Which`]: DetectStrategy::Which
//! [`Any`]: DetectStrategy::Any
//! [`All`]: DetectStrategy::All
//! [`When`]: DetectStrategy::When

use serde::de;
use zenops_expand::{ExpandLookup, ExpandStr};

use super::error::Error;
use crate::config::condition::{ConditionOrRef, Conditions, HostContext};

/// Sorted by canonical reading order so error messages are stable.
const KINDS: &[&str] = &["exists", "which", "any", "all", "when"];

/// A detect strategy. `File` and `Which` are leaves; `Any` and `All` are
/// combinators that compose other strategies; `When` gates a subtree on a
/// host-level condition. See the module docs for the wire-format mapping.
#[derive(Debug, Clone, PartialEq)]
pub enum DetectStrategy {
    File {
        path: ExpandStr,
    },
    Which {
        binary: ExpandStr,
    },
    /// Matches when **any** child strategy matches (short-circuits).
    Any {
        of: Vec<DetectStrategy>,
    },
    /// Matches when **every** child strategy matches. An empty `of` is
    /// vacuously true — callers should prefer omitting the pkg's `detect`
    /// field entirely to express "no check required".
    All {
        of: Vec<DetectStrategy>,
    },
    /// Gates a subtree on a host-level condition. Evaluates as `false`
    /// (never "skip / filter") when `when` fails, so that an `All` whose
    /// children are all gated to a different host correctly evaluates as
    /// `false` rather than empty-set-vacuously `true`.
    When {
        when: ConditionOrRef,
        then: Box<DetectStrategy>,
    },
}

impl DetectStrategy {
    /// Run the check. Unresolved `${var}` placeholders inside leaf checks
    /// yield `false` rather than an error. A `When` whose condition fails
    /// also yields `false` — see the variant docs.
    pub fn check(
        &self,
        conditions: &Conditions,
        ctx: &HostContext<'_>,
        lookup: &impl ExpandLookup,
    ) -> Result<bool, Error> {
        match self {
            Self::File { path } => {
                let Ok(expanded) = path.expand_to_string(lookup) else {
                    return Ok(false);
                };
                let resolved = expanded.replacen('~', &ctx.home.to_string_lossy(), 1);
                std::path::Path::new(&resolved)
                    .try_exists()
                    .map_err(|e| Error::ExistsFailed(resolved, e))
            }
            Self::Which { binary } => Ok(crate::utils::which::expand_and_exists(binary, lookup)?),
            Self::Any { of } => {
                for s in of {
                    if s.check(conditions, ctx, lookup)? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }
            Self::All { of } => {
                for s in of {
                    if !s.check(conditions, ctx, lookup)? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            Self::When { when, then } => {
                if !conditions.evaluate(when, ctx)? {
                    return Ok(false);
                }
                then.check(conditions, ctx, lookup)
            }
        }
    }
}

impl std::fmt::Display for DetectStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::File { path } => write!(f, "{}", path.as_template()),
            Self::Which { binary } => write!(f, "which {}", binary.as_template()),
            Self::Any { of } => write_combinator(f, "any", of),
            Self::All { of } => write_combinator(f, "all", of),
            Self::When { when, then } => write!(f, "when({}, {then})", format_when(when)),
        }
    }
}

fn format_when(cor: &ConditionOrRef) -> String {
    match cor {
        ConditionOrRef::Ref(name) => name.to_string(),
        ConditionOrRef::Inline(c) => format!("{c:?}"),
    }
}

fn write_combinator(
    f: &mut std::fmt::Formatter<'_>,
    name: &str,
    of: &[DetectStrategy],
) -> std::fmt::Result {
    write!(f, "{name}(")?;
    for (i, s) in of.iter().enumerate() {
        if i > 0 {
            write!(f, ", ")?;
        }
        write!(f, "{s}")?;
    }
    write!(f, ")")
}

// ---------- Deserialize ----------

impl<'de> de::Deserialize<'de> for DetectStrategy {
    fn deserialize<D: de::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        struct Visitor;

        impl<'de> de::Visitor<'de> for Visitor {
            type Value = DetectStrategy;

            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(
                    f,
                    "a detect table with exactly one of: {} (or `when` paired with `then`)",
                    KINDS.join(", ")
                )
            }

            fn visit_map<A: de::MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
                let first_key: String = match map.next_key()? {
                    Some(k) => k,
                    None => {
                        return Err(de::Error::custom(format!(
                            "empty detect table; expected exactly one of: {}",
                            KINDS.join(", ")
                        )));
                    }
                };

                match first_key.as_str() {
                    "exists" => {
                        let path: ExpandStr = map.next_value()?;
                        forbid_extra_keys(&mut map, &first_key)?;
                        Ok(DetectStrategy::File { path })
                    }
                    "which" => {
                        let binary: ExpandStr = map.next_value()?;
                        forbid_extra_keys(&mut map, &first_key)?;
                        Ok(DetectStrategy::Which { binary })
                    }
                    "any" => {
                        let of: Vec<DetectStrategy> = map.next_value()?;
                        forbid_extra_keys(&mut map, &first_key)?;
                        Ok(DetectStrategy::Any { of })
                    }
                    "all" => {
                        let of: Vec<DetectStrategy> = map.next_value()?;
                        forbid_extra_keys(&mut map, &first_key)?;
                        Ok(DetectStrategy::All { of })
                    }
                    "when" | "then" => {
                        let mut when_val: Option<ConditionOrRef> = None;
                        let mut then_val: Option<Box<DetectStrategy>> = None;

                        match first_key.as_str() {
                            "when" => when_val = Some(map.next_value()?),
                            "then" => then_val = Some(Box::new(map.next_value()?)),
                            _ => unreachable!(),
                        }

                        while let Some(k) = map.next_key::<String>()? {
                            match k.as_str() {
                                "when" if when_val.is_some() => {
                                    return Err(de::Error::custom(
                                        "`when` strategy has duplicate `when` key",
                                    ));
                                }
                                "when" => when_val = Some(map.next_value()?),
                                "then" if then_val.is_some() => {
                                    return Err(de::Error::custom(
                                        "`when` strategy has duplicate `then` key",
                                    ));
                                }
                                "then" => then_val = Some(Box::new(map.next_value()?)),
                                other => {
                                    return Err(de::Error::custom(format!(
                                        "`when` strategy has unexpected key '{other}'; expected exactly `when` and `then`"
                                    )));
                                }
                            }
                        }

                        let when = when_val.ok_or_else(|| {
                            de::Error::custom(
                                "found `then` without a matching `when`; a detect strategy with `then` must also have `when`",
                            )
                        })?;
                        let then = then_val.ok_or_else(|| {
                            de::Error::custom("`when` strategy is missing the `then` key")
                        })?;
                        Ok(DetectStrategy::When { when, then })
                    }
                    other => Err(de::Error::custom(format!(
                        "unknown detect kind '{other}', expected one of: {}",
                        KINDS.join(", ")
                    ))),
                }
            }
        }

        d.deserialize_map(Visitor)
    }
}

fn forbid_extra_keys<'de, A: de::MapAccess<'de>>(map: &mut A, key: &str) -> Result<(), A::Error> {
    if let Some(extra) = map.next_key::<String>()? {
        return Err(de::Error::custom(format!(
            "detect must have exactly one kind, found '{key}' and '{extra}'"
        )));
    }
    Ok(())
}

// ---------- JsonSchema ----------

impl schemars::JsonSchema for DetectStrategy {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "DetectStrategy".into()
    }

    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        let exists = generator.subschema_for::<ExpandStr>();
        let which = generator.subschema_for::<ExpandStr>();
        let any_children = generator.subschema_for::<Vec<DetectStrategy>>();
        let all_children = generator.subschema_for::<Vec<DetectStrategy>>();
        let when_cond = generator.subschema_for::<ConditionOrRef>();
        let then_child = generator.subschema_for::<DetectStrategy>();
        schemars::json_schema!({
            "description": "A check for whether a pkg is installed. Exactly one kind per table; `when` additionally requires `then`.",
            "oneOf": [
                {
                    "type": "object",
                    "properties": { "exists": exists },
                    "required": ["exists"],
                    "additionalProperties": false,
                },
                {
                    "type": "object",
                    "properties": { "which": which },
                    "required": ["which"],
                    "additionalProperties": false,
                },
                {
                    "type": "object",
                    "properties": { "any": any_children },
                    "required": ["any"],
                    "additionalProperties": false,
                },
                {
                    "type": "object",
                    "properties": { "all": all_children },
                    "required": ["all"],
                    "additionalProperties": false,
                },
                {
                    "type": "object",
                    "properties": { "when": when_cond, "then": then_child },
                    "required": ["when", "then"],
                    "additionalProperties": false,
                },
            ],
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse_err(toml_src: &str) -> String {
        toml::from_str::<DetectStrategy>(toml_src)
            .unwrap_err()
            .to_string()
    }

    #[test]
    fn unknown_kind_lists_valid_kinds() {
        let err = parse_err(r#"nonsense = "x""#);
        assert!(
            err.contains("unknown detect kind 'nonsense'"),
            "expected unknown-kind message, got: {err}"
        );
        for kind in KINDS {
            assert!(err.contains(kind), "expected '{kind}' in error, got: {err}");
        }
    }

    #[test]
    fn empty_table_errors() {
        let err = parse_err("");
        assert!(
            err.contains("empty detect table"),
            "expected empty-table message, got: {err}"
        );
    }

    #[test]
    fn multiple_kinds_error_names_both() {
        let err = parse_err(
            r#"
                exists = "/x"
                which = "y"
            "#,
        );
        assert!(
            err.contains("exactly one kind"),
            "expected 'exactly one kind' message, got: {err}"
        );
        assert!(
            err.contains("'exists'") && err.contains("'which'"),
            "expected both keys to appear in error, got: {err}"
        );
    }

    #[test]
    fn when_missing_then_errors() {
        let err = parse_err(r#"when = "macos""#);
        assert!(
            err.contains("missing the `then` key"),
            "expected missing-then message, got: {err}"
        );
    }

    #[test]
    fn when_with_unexpected_extra_key_errors() {
        let err = parse_err(
            r#"
                when = "macos"
                then = { exists = "/x" }
                bogus = 1
            "#,
        );
        assert!(
            err.contains("'bogus'"),
            "expected error to name 'bogus', got: {err}"
        );
    }

    #[test]
    fn when_then_reversed_works() {
        let reversed: DetectStrategy = toml::from_str(
            r#"
                then = { exists = "/x" }
                when = "macos"
            "#,
        )
        .unwrap();
        let canonical: DetectStrategy = toml::from_str(
            r#"
                when = "macos"
                then = { exists = "/x" }
            "#,
        )
        .unwrap();
        assert_eq!(reversed, canonical);
    }

    #[test]
    fn then_without_when_errors() {
        let err = parse_err(r#"then = { exists = "/x" }"#);
        assert!(
            err.contains("`then` without a matching `when`"),
            "expected then-without-when message, got: {err}"
        );
    }

    #[test]
    fn rejects_legacy_tagged_form() {
        // Migration guard: the old `type = "..."` shape must fail with a
        // diagnostic that names the new keys, so users see how to update.
        let err = parse_err(
            r#"
                type = "file"
                path = "/x"
            "#,
        );
        assert!(
            err.contains("unknown detect kind 'type'"),
            "expected error to name 'type' as unknown, got: {err}"
        );
    }

    #[test]
    fn duplicate_when_errors() {
        let err = parse_err(
            r#"
                when = "macos"
                when = "linux"
            "#,
        );
        // TOML's own parser rejects the duplicate key before we see it;
        // either path is acceptable — the user gets a clear diagnostic.
        assert!(
            err.contains("duplicate") || err.contains("redefin"),
            "expected duplicate-key message, got: {err}"
        );
    }
}