Skip to main content

buildfix_types/
ops.rs

1use serde::{Deserialize, Serialize};
2
3/// Safety class for an operation.
4///
5/// In buildfix terms:
6/// - safe: fully determined from repo-local truth, low impact
7/// - guarded: deterministic but higher impact (requires explicit allow)
8/// - unsafe: deterministic only when user-provided parameters are present and
9///   `--allow-unsafe` is enabled
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub enum SafetyClass {
13    Safe,
14    Guarded,
15    Unsafe,
16}
17
18impl SafetyClass {
19    pub fn is_safe(self) -> bool {
20        matches!(self, SafetyClass::Safe)
21    }
22    pub fn is_guarded(self) -> bool {
23        matches!(self, SafetyClass::Guarded)
24    }
25    pub fn is_unsafe(self) -> bool {
26        matches!(self, SafetyClass::Unsafe)
27    }
28}
29
30/// Operation kind for plan ops.
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(tag = "type", rename_all = "snake_case")]
33pub enum OpKind {
34    TomlSet {
35        toml_path: Vec<String>,
36        value: serde_json::Value,
37    },
38    TomlRemove {
39        toml_path: Vec<String>,
40    },
41    JsonSet {
42        json_path: Vec<String>,
43        value: serde_json::Value,
44    },
45    JsonRemove {
46        json_path: Vec<String>,
47    },
48    YamlSet {
49        yaml_path: Vec<String>,
50        value: serde_json::Value,
51    },
52    YamlRemove {
53        yaml_path: Vec<String>,
54    },
55    TomlTransform {
56        rule_id: String,
57        #[serde(default, skip_serializing_if = "Option::is_none")]
58        args: Option<serde_json::Value>,
59    },
60    TextReplaceAnchored {
61        find: String,
62        replace: String,
63        #[serde(default, skip_serializing_if = "Vec::is_empty")]
64        anchor_before: Vec<String>,
65        #[serde(default, skip_serializing_if = "Vec::is_empty")]
66        anchor_after: Vec<String>,
67        #[serde(default, skip_serializing_if = "Option::is_none")]
68        max_replacements: Option<u64>,
69    },
70}
71
72/// Target path for an operation.
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct OpTarget {
75    pub path: String,
76}
77
78/// Optional preview fragment for an operation.
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct OpPreview {
81    pub patch_fragment: String,
82}