plut-core 2.8.0

Project model, run context and diagnostics for Plut
Documentation
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

/// How loud a finding is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
    #[default]
    Info,
    Warning,
    Error,
}

impl Severity {
    /// The lowercase name used in config and JSON.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Info => "info",
            Self::Warning => "warning",
            Self::Error => "error",
        }
    }
}

/// How strongly the rule believes a finding is a real issue.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum Confidence {
    Low,
    #[default]
    Medium,
    High,
}

impl Confidence {
    /// The lowercase name used in JSON and the terminal.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
        }
    }
}

/// A byte range in a source file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Span {
    pub start: usize,
    pub end: usize,
}

impl Span {
    /// Build a span from two byte offsets.
    #[must_use]
    pub const fn new(start: usize, end: usize) -> Self {
        Self { start, end }
    }
}

/// One report from one rule.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Finding {
    /// Rule id, such as `GB201`.
    pub rule: String,
    pub severity: Severity,
    /// Path relative to the project root.
    pub path: PathBuf,
    /// 1-based line of `span.start`. Zero when the finding is about the file as a whole.
    pub line: usize,
    /// 1-based column of `span.start`. Zero when the finding is about the file as a whole.
    pub column: usize,
    pub span: Span,
    pub message: String,
    /// How strongly the rule believes the finding.
    #[serde(default)]
    pub confidence: Confidence,
    /// The CWE this finding belongs to, when one applies.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cwe: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub help: Option<String>,
    /// Exploit scenario, for reviewers and agents.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exploit: Option<String>,
    /// Step-by-step remediation guidance.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remediation: Option<String>,
    /// Machine-applicable edits. Empty means "suggestion only".
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub edits: Vec<TextEdit>,
    /// How safe the edits are. Absent means review-required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fix_safety: Option<FixSafety>,
    /// Hash of the source the finding was produced from. Internal to `--fix`: it is
    /// not part of the report schema.
    #[serde(skip)]
    pub source_hash: Option<String>,
}

/// How safe it is to apply the edits without a human reading the diff.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FixSafety {
    /// No behaviour change by construction.
    Safe,
    /// A mechanical API migration, such as `wait` to `task.wait`.
    Migration,
    /// A patch that needs a human or an agent to review it first.
    ReviewRequired,
}

/// A byte-range replacement suggested by a rule.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TextEdit {
    pub start: usize,
    pub end: usize,
    pub replacement: String,
}

/// A stable hash of a source text, used to detect a file that changed between the
/// analysis and a fix. This is not a cryptographic hash; it only needs to change when
/// the bytes change, and to stay the same across processes.
#[must_use]
pub fn source_hash(source: &str) -> String {
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;

    for byte in source.as_bytes() {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }

    format!("{hash:016x}")
}