Skip to main content

haste_artifact_patcher/rules/
mod.rs

1//! Rules are transforms written in Rust that apply across many resources, for
2//! changes that are impractical as targeted patches. A manifest enables a rule
3//! by its [`Rule::name`].
4
5mod clippy_doc_markdown;
6
7use serde_json::Value;
8
9/// A single string rewritten by a rule.
10#[derive(Debug, Clone)]
11pub struct Edit {
12    /// JSON pointer into the resource.
13    pub pointer: String,
14    pub before: Value,
15    pub after: Value,
16}
17
18pub trait Rule: Sync {
19    /// Name used to enable the rule in a manifest.
20    fn name(&self) -> &'static str;
21    /// Rewrites `resource` in place and returns what changed.
22    fn apply(&self, resource: &mut Value) -> Vec<Edit>;
23}
24
25static RULES: &[&dyn Rule] = &[&clippy_doc_markdown::ClippyDocMarkdown];
26
27/// Looks up a rule by name.
28#[must_use]
29pub fn find(name: &str) -> Option<&'static dyn Rule> {
30    RULES.iter().copied().find(|rule| rule.name() == name)
31}
32
33/// Names of every available rule.
34#[must_use]
35pub fn names() -> Vec<&'static str> {
36    RULES.iter().map(|rule| rule.name()).collect()
37}