Skip to main content

specfold_core/plugins/
protocol.rs

1//! The `specfold.plugin.v1` messages. One request per process: specfold writes a JSON object to
2//! the plugin's stdin and reads one JSON object from its stdout.
3
4use crate::check::finding::{Fix, Severity};
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8pub const PROTOCOL: &str = "specfold.plugin.v1";
9
10#[derive(Debug, Clone, Serialize)]
11pub struct Request<'a> {
12    pub protocol: &'static str,
13    pub method: &'a str,
14    #[serde(skip_serializing_if = "Value::is_null")]
15    pub params: Value,
16}
17
18#[derive(Debug, Clone, Deserialize, Serialize, Default)]
19pub struct PluginRule {
20    pub id: String,
21    pub title: String,
22    #[serde(default)]
23    pub explain: String,
24    #[serde(default)]
25    pub on_commit: bool,
26}
27
28#[derive(Debug, Clone, Deserialize, Serialize, Default)]
29pub struct Provides {
30    #[serde(default)]
31    pub rules: Vec<PluginRule>,
32    #[serde(default)]
33    pub importers: Vec<String>,
34    #[serde(default)]
35    pub exporters: Vec<String>,
36    #[serde(default)]
37    pub audits: Vec<String>,
38    #[serde(default)]
39    pub events: Vec<String>,
40}
41
42#[derive(Debug, Clone, Deserialize, Serialize)]
43pub struct Description {
44    #[serde(default)]
45    pub protocol: String,
46    pub name: String,
47    #[serde(default)]
48    pub version: String,
49    #[serde(default)]
50    pub provides: Provides,
51    /// Extras an exporter asks for in `export.run`: `body` and `links`.
52    #[serde(default, skip_serializing_if = "Vec::is_empty")]
53    pub wants: Vec<String>,
54}
55
56/// A finding as a plugin reports it; the rule id is namespaced by specfold.
57#[derive(Debug, Clone, Deserialize)]
58pub struct PluginFinding {
59    pub rule: String,
60    #[serde(default = "default_severity")]
61    pub severity: Severity,
62    pub path: String,
63    #[serde(default = "one")]
64    pub line: usize,
65    pub message: String,
66    pub fix: Option<Fix>,
67    #[serde(default)]
68    pub candidates: Vec<String>,
69}
70
71fn default_severity() -> Severity {
72    Severity::Error
73}
74
75fn one() -> usize {
76    1
77}
78
79#[derive(Debug, Clone, Deserialize)]
80pub struct FindingsResponse {
81    #[serde(default)]
82    pub findings: Vec<PluginFinding>,
83}