Skip to main content

mars_schema/
package_tools.rs

1//! `[package.tools]` and the Agents the package ships.
2//!
3//! The package's rules act as if written in each shipped Agent's `tools:` map, except for a
4//! key the Agent's own map already names. `mars sync` writes the missing keys into the
5//! installed file; `prepare` adds them to the Agent's reading. Both ask [`missing`], so the
6//! precedence is one rule.
7
8use serde_yaml::Value;
9
10use crate::frontmatter::Frontmatter;
11use crate::tool_names::OverlayToolName;
12use crate::tool_policy::ToolAction;
13use crate::tool_rule_map::ToolRuleMap;
14
15/// The entries of `package_tools` that `profile`'s own `tools:` does not name, in the package's
16/// order. Keys compare by their parsed form, so `bash(git push:*)` and `bash(git push *)` are
17/// one key. Empty for a list-form `tools:`, which is reported where the profile is read.
18pub fn missing<'a>(
19    profile: &Frontmatter,
20    package_tools: &'a ToolRuleMap,
21) -> Vec<(&'a OverlayToolName, ToolAction)> {
22    let own: Vec<OverlayToolName> = match profile.get("tools") {
23        None | Some(Value::Null) => Vec::new(),
24        Some(Value::Mapping(map)) => map
25            .keys()
26            .filter_map(|key| key.as_str())
27            .filter_map(|key| OverlayToolName::try_from(key.to_owned()).ok())
28            .collect(),
29        Some(_) => return Vec::new(),
30    };
31    package_tools
32        .iter()
33        .filter(|(name, _)| !own.iter().any(|own| own.parsed() == name.parsed()))
34        .collect()
35}