Skip to main content

xbp_analysis/domain/
request.rs

1//! Analysis request / configuration domain objects.
2
3use super::rule::RuleSelection;
4use super::types::{ApprovedPattern, LanguageId, Severity, Suppression};
5use serde::{Deserialize, Serialize};
6use std::path::PathBuf;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "lowercase")]
10pub enum OutputFormat {
11    Terminal,
12    Json,
13    Sarif,
14    GithubActions,
15}
16
17impl OutputFormat {
18    pub fn parse(s: &str) -> Option<Self> {
19        match s.to_ascii_lowercase().as_str() {
20            "terminal" | "text" | "human" => Some(Self::Terminal),
21            "json" => Some(Self::Json),
22            "sarif" => Some(Self::Sarif),
23            "github" | "github-actions" | "gha" => Some(Self::GithubActions),
24            _ => None,
25        }
26    }
27}
28
29#[derive(Debug, Clone, Default, Serialize, Deserialize)]
30pub struct AnalysisConfig {
31    pub languages: Vec<LanguageId>,
32    pub rules: RuleSelection,
33    pub path_exclusions: Vec<String>,
34    pub generated_exclusions: Vec<String>,
35    pub suppressions: Vec<Suppression>,
36    pub approved_patterns: Vec<ApprovedPattern>,
37    pub min_severity: Option<Severity>,
38    pub adapter_config: serde_json::Value,
39}
40
41impl AnalysisConfig {
42    pub fn default_for_rust() -> Self {
43        Self {
44            languages: vec![LanguageId::rust()],
45            rules: RuleSelection::default(),
46            path_exclusions: vec![
47                "**/target/**".into(),
48                "**/node_modules/**".into(),
49                "**/.git/**".into(),
50                "**/vendor/**".into(),
51            ],
52            generated_exclusions: vec![
53                "**/*_generated.rs".into(),
54                "**/generated/**".into(),
55                "**/*.pb.rs".into(),
56            ],
57            suppressions: Vec::new(),
58            approved_patterns: Vec::new(),
59            min_severity: None,
60            adapter_config: serde_json::json!({}),
61        }
62    }
63}
64
65#[derive(Debug, Clone)]
66pub struct AnalysisRequest {
67    pub root: PathBuf,
68    pub paths: Vec<PathBuf>,
69    pub config: AnalysisConfig,
70    pub format: OutputFormat,
71    pub apply_fixes: bool,
72    pub workspace: bool,
73    pub language_filter: Option<LanguageId>,
74}
75
76impl AnalysisRequest {
77    pub fn new(root: impl Into<PathBuf>) -> Self {
78        Self {
79            root: root.into(),
80            paths: Vec::new(),
81            config: AnalysisConfig::default_for_rust(),
82            format: OutputFormat::Terminal,
83            apply_fixes: false,
84            workspace: false,
85            language_filter: None,
86        }
87    }
88}