acorn_lib/analyzer/
vale.rs1use crate::prelude::PathBuf;
6use crate::util::{Label, SemanticVersion};
7use bon::Builder;
8use color_eyre::owo_colors::OwoColorize;
9use derive_more::Display;
10use serde::{Deserialize, Serialize};
11use tracing::error;
12
13#[derive(Clone, Debug, Default, Display, Serialize, Deserialize)]
17#[serde(rename_all = "lowercase")]
18pub enum ValeOutputItemSeverity {
19    #[display("warning")]
23    Warning,
24    #[display("error")]
28    Error,
29    #[default]
33    #[display("suggestion")]
34    Suggestion,
35}
36#[derive(Builder, Clone, Debug, Default, Display)]
38#[display("{:?}", version)]
39#[builder(start_fn = init)]
40pub struct Vale {
41    pub version: Option<SemanticVersion>,
43    pub binary: Option<PathBuf>,
45    pub config: Option<ValeConfig>,
47}
48#[derive(Builder, Clone, Debug, Display)]
52#[display("{:?}", path)]
53#[builder(start_fn = init)]
54pub struct ValeConfig {
55    #[builder(default = PathBuf::from("./.vale/.vale.ini"))]
57    pub path: PathBuf,
58    #[builder(default = Vec::<String>::new())]
62    pub packages: Vec<String>,
63    #[builder(default = Vec::<String>::new())]
67    pub vocabularies: Vec<String>,
68    #[builder(default = Vec::<String>::new())]
72    pub disabled: Vec<String>,
73}
74#[derive(Clone, Debug, Serialize, Deserialize)]
76pub struct ValeOutput {
77    pub items: Vec<ValeOutputItem>,
79}
80#[derive(Clone, Debug, Serialize, Deserialize)]
84#[serde(rename_all = "PascalCase")]
85pub struct ValeOutputItem {
86    action: ValeOutputItemAction,
87    pub check: String,
91    description: String,
92    pub line: u32,
94    link: String,
95    pub message: String,
97    pub severity: ValeOutputItemSeverity,
99    pub span: Vec<u32>,
101    #[serde(rename = "Match")]
102    word_match: String,
103}
104#[derive(Clone, Debug, Serialize, Deserialize)]
106#[serde(rename_all = "PascalCase")]
107pub struct ValeOutputItemAction {
108    name: String,
110    params: Option<Vec<String>>,
112}
113impl ValeOutput {
114    pub fn parse(output: &str, path: PathBuf) -> Vec<ValeOutputItem> {
116        let processed = preprocess_vale_output(path, output);
117        if processed != "{}" {
118            let parsed: serde_json::Result<ValeOutput> = serde_json::from_str(&processed);
119            match parsed {
120                | Ok(ValeOutput { items }) => items,
121                | Err(why) => {
122                    error!("=> {} Parse Vale output - {why}", Label::fail());
123                    vec![]
124                }
125            }
126        } else {
127            vec![]
128        }
129    }
130}
131impl ValeOutputItemSeverity {
132    pub fn colored(&self) -> String {
134        match self {
135            | ValeOutputItemSeverity::Warning => self.to_string().yellow().to_string(),
136            | ValeOutputItemSeverity::Error => self.to_string().red().to_string(),
137            | ValeOutputItemSeverity::Suggestion => self.to_string().blue().to_string(),
138        }
139    }
140}
141#[cfg(any(unix, target_os = "wasi", target_os = "redox"))]
143pub fn preprocess_vale_output(path: PathBuf, output: &str) -> String {
144    output.replace(path.to_str().unwrap(), "items")
145}
146#[cfg(windows)]
148pub fn preprocess_vale_output(path: PathBuf, output: &str) -> String {
149    let input = path.as_path().display().to_string().replace("\\", "/");
150    output.replace("\\\\", "/").replace(&input, "items")
151}