unsafe-budget 0.3.0

keeps the unsafety demons out. an unsafe code budget gate for CI pipelines.
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
use crate::error::{Error, Result};
use crate::model::{Scope, Totals, Unit, UnitKind};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

/// Budget enforcement mode.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
    #[default]
    Ratchet,
    Caps,
}

/// Caps configuration for explicit limits.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Caps {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<u64>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub workspace: HashMap<String, u64>,
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub deps: HashMap<String, u64>,
}

/// A single occurrence to ignore when counting unsafe code.
///
/// Matches a specific file and line number in the scan output. The `reason`
/// field is optional documentation for reviewers.
///
/// # Example (unsafe-budget.toml)
///
/// ```toml
/// [[ignore]]
/// file = "src/ffi.rs"
/// line = 42
/// reason = "ffi boundary, reviewed 2026-04-12"
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct IgnoreEntry {
    /// File path relative to the project root.
    pub file: PathBuf,
    /// Line number (1-indexed).
    pub line: u32,
    /// Optional human-readable reason for the ignore.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}

/// Warning configuration for near-budget usage.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Warnings {
    /// Warn when usage reaches this fraction of budget, e.g. 0.8 for 80%.
    pub threshold: f64,
}

/// Main configuration from unsafe-budget.toml.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    #[serde(default)]
    pub mode: Mode,
    #[serde(default = "default_true")]
    pub include_deps: bool,
    #[serde(default)]
    pub workspace_only: bool,
    #[serde(default)]
    pub ignore_units: Vec<String>,
    /// Specific occurrences to exclude from counts.
    ///
    /// Each entry matches a file path and line number. Matched occurrences are
    /// removed before budgets are checked, so they do not count toward any unit's
    /// unsafe total.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub ignore: Vec<IgnoreEntry>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub caps: Option<Caps>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub warnings: Option<Warnings>,
    /// Timeout in seconds for external plugin execution.
    ///
    /// When set, plugin subprocesses that exceed this duration are killed and
    /// reported as errors. Prevents hanging plugins from blocking CI pipelines
    /// indefinitely.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub plugin_timeout_secs: Option<u64>,
}

fn default_true() -> bool {
    true
}

impl Default for Config {
    fn default() -> Self {
        Config {
            mode: Mode::default(),
            include_deps: true,
            workspace_only: false,
            ignore_units: Vec::new(),
            ignore: Vec::new(),
            caps: None,
            warnings: None,
            plugin_timeout_secs: None,
        }
    }
}

impl Config {
    /// Load config from file, or return defaults if not found.
    pub fn load(path: &Path) -> Result<Self> {
        if !path.exists() {
            return Ok(Config::default());
        }
        let content = fs::read_to_string(path)?;
        let config: Config = toml::from_str(&content)?;
        Ok(config)
    }

    /// Load config from the standard location in a directory.
    pub fn load_from_dir(dir: &Path) -> Result<Self> {
        Self::load(&dir.join("unsafe-budget.toml"))
    }
}

/// Baseline data from unsafe-budget.lock.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Baseline {
    pub tool_version: String,
    pub analyzer_id: String,
    pub scope: Scope,
    pub totals: Totals,
    pub units: Vec<BaselineUnit>,
}

/// Unit entry in baseline file (uses string kind for TOML compatibility).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaselineUnit {
    pub name: String,
    pub kind: UnitKind,
    pub unsafe_count: u64,
}

impl From<&Unit> for BaselineUnit {
    fn from(u: &Unit) -> Self {
        BaselineUnit {
            name: u.name.clone(),
            kind: u.kind,
            unsafe_count: u.unsafe_count,
        }
    }
}

impl Baseline {
    /// Load baseline from file.
    pub fn load(path: &Path) -> Result<Self> {
        if !path.exists() {
            return Err(Error::BaselineNotFound {
                path: path.to_path_buf(),
            });
        }
        let content = fs::read_to_string(path)?;
        let baseline: Baseline = toml::from_str(&content)
            .map_err(|e| Error::Baseline(format!("failed to parse {}: {}", path.display(), e)))?;
        Ok(baseline)
    }

    /// Load baseline from the standard location in a directory.
    pub fn load_from_dir(dir: &Path) -> Result<Self> {
        Self::load(&dir.join("unsafe-budget.lock"))
    }

    /// Write baseline to file.
    pub fn save(&self, path: &Path) -> Result<()> {
        let content = toml::to_string_pretty(self)?;
        let header = "# Auto-generated by unsafe-budget. Do not edit manually.\n\n";
        fs::write(path, format!("{}{}", header, content))?;
        Ok(())
    }

    /// Save baseline to the standard location in a directory.
    pub fn save_to_dir(&self, dir: &Path) -> Result<()> {
        self.save(&dir.join("unsafe-budget.lock"))
    }

    /// Get the unsafe count for a unit by name.
    pub fn get_unit(&self, name: &str) -> Option<&BaselineUnit> {
        self.units.iter().find(|u| u.name == name)
    }
}

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

    #[test]
    fn test_config_defaults() {
        let config = Config::default();
        assert_eq!(config.mode, Mode::Ratchet);
        assert!(config.include_deps);
        assert!(!config.workspace_only);
        assert!(config.ignore_units.is_empty());
    }

    #[test]
    fn test_config_parse() {
        let toml = r#"
mode = "caps"
include_deps = false
workspace_only = true
ignore_units = ["foo", "bar"]

[caps]
default = 10

[caps.workspace]
my_crate = 5

[caps.deps]
libc = 100

[warnings]
threshold = 0.8
"#;
        let config: Config = toml::from_str(toml).unwrap();
        assert_eq!(config.mode, Mode::Caps);
        assert!(!config.include_deps);
        assert!(config.workspace_only);
        assert_eq!(config.ignore_units, vec!["foo", "bar"]);

        let caps = config.caps.as_ref().unwrap();
        assert_eq!(caps.default, Some(10));
        assert_eq!(caps.workspace.get("my_crate"), Some(&5));
        assert_eq!(caps.deps.get("libc"), Some(&100));
        assert_eq!(config.warnings.as_ref().unwrap().threshold, 0.8);
    }

    #[test]
    fn test_config_ignore_parse() {
        let toml = r#"
[[ignore]]
file = "src/ffi.rs"
line = 42
reason = "ffi boundary"

[[ignore]]
file = "src/platform.rs"
line = 7
"#;
        let config: Config = toml::from_str(toml).unwrap();
        assert_eq!(config.ignore.len(), 2);

        assert_eq!(
            config.ignore[0].file,
            std::path::PathBuf::from("src/ffi.rs")
        );
        assert_eq!(config.ignore[0].line, 42);
        assert_eq!(config.ignore[0].reason.as_deref(), Some("ffi boundary"));

        assert_eq!(
            config.ignore[1].file,
            std::path::PathBuf::from("src/platform.rs")
        );
        assert_eq!(config.ignore[1].line, 7);
        assert!(config.ignore[1].reason.is_none());
    }

    #[test]
    fn test_config_ignore_empty_by_default() {
        let config = Config::default();
        assert!(config.ignore.is_empty());
    }

    #[test]
    fn test_baseline_roundtrip() {
        let baseline = Baseline {
            tool_version: "0.1.0".into(),
            analyzer_id: "rustc_unsafe_lint".into(),
            scope: Scope {
                workspace_only: false,
                include_deps: true,
                features: vec![],
                all_targets: false,
                targets: vec![],
                manifest_path: None,
            },
            totals: Totals {
                workspace_unsafe: 10,
                deps_unsafe: 20,
                overall_unsafe: 30,
            },
            units: vec![
                BaselineUnit {
                    name: "my_crate".into(),
                    kind: UnitKind::Workspace,
                    unsafe_count: 10,
                },
                BaselineUnit {
                    name: "libc".into(),
                    kind: UnitKind::Dep,
                    unsafe_count: 20,
                },
            ],
        };

        let serialized = toml::to_string_pretty(&baseline).unwrap();
        let parsed: Baseline = toml::from_str(&serialized).unwrap();

        assert_eq!(parsed.tool_version, baseline.tool_version);
        assert_eq!(parsed.units.len(), 2);
        assert_eq!(parsed.units[0].name, "my_crate");
    }

    #[test]
    fn test_baseline_get_unit_found() {
        let baseline = Baseline {
            tool_version: "0.1.0".into(),
            analyzer_id: "test".into(),
            scope: Scope {
                workspace_only: false,
                include_deps: true,
                features: vec![],
                all_targets: false,
                targets: vec![],
                manifest_path: None,
            },
            totals: Totals::default(),
            units: vec![BaselineUnit {
                name: "my_crate".into(),
                kind: UnitKind::Workspace,
                unsafe_count: 10,
            }],
        };

        let unit = baseline.get_unit("my_crate");
        assert!(unit.is_some());
        assert_eq!(unit.unwrap().unsafe_count, 10);
    }

    #[test]
    fn test_baseline_get_unit_not_found() {
        let baseline = Baseline {
            tool_version: "0.1.0".into(),
            analyzer_id: "test".into(),
            scope: Scope {
                workspace_only: false,
                include_deps: true,
                features: vec![],
                all_targets: false,
                targets: vec![],
                manifest_path: None,
            },
            totals: Totals::default(),
            units: vec![],
        };

        let unit = baseline.get_unit("nonexistent");
        assert!(unit.is_none());
    }

    #[test]
    fn test_baseline_unit_from_unit() {
        use crate::model::Unit;

        let unit = Unit {
            name: "test_crate".into(),
            kind: UnitKind::Workspace,
            unsafe_count: 42,
        };

        let baseline_unit = BaselineUnit::from(&unit);

        assert_eq!(baseline_unit.name, "test_crate");
        assert_eq!(baseline_unit.kind, UnitKind::Workspace);
        assert_eq!(baseline_unit.unsafe_count, 42);
    }

    #[test]
    fn test_mode_default() {
        let mode = Mode::default();
        assert_eq!(mode, Mode::Ratchet);
    }

    #[test]
    fn test_caps_default() {
        let caps = Caps::default();
        assert!(caps.default.is_none());
        assert!(caps.workspace.is_empty());
        assert!(caps.deps.is_empty());
    }

    #[test]
    fn test_config_mode_parse() {
        let ratchet_toml = r#"mode = "ratchet""#;
        let caps_toml = r#"mode = "caps""#;

        let ratchet: Config = toml::from_str(ratchet_toml).unwrap();
        let caps: Config = toml::from_str(caps_toml).unwrap();

        assert_eq!(ratchet.mode, Mode::Ratchet);
        assert_eq!(caps.mode, Mode::Caps);
    }

    #[test]
    fn test_config_load_missing_file_returns_defaults() {
        use std::path::Path;
        let result = Config::load(Path::new("/nonexistent/path/config.toml"));
        assert!(result.is_ok());
        let config = result.unwrap();
        assert_eq!(config.mode, Mode::Ratchet);
    }
}