use std::ffi::OsStr;
use jsonvet::{Location, Severity};
use crate::sarif::{level, string};
pub(crate) const VERSION: u32 = 1;
pub(crate) fn start() -> String {
format!("{{\"version\":{VERSION},\"diagnostics\":[\n")
}
pub(crate) fn diagnostic(
file: &OsStr,
rule: &str,
severity: Severity,
message: &str,
at: Option<(Location, usize)>,
related: Option<(Location, usize, &str)>,
) -> String {
let related = related.map_or_else(String::new, |(first, offset, text)| {
format!(
r#"{{"message":{},{}}}"#,
string(text),
position(Some((first, offset)))
)
});
format!(
r#"{{"file":{},"rule":{},"url":{},"severity":"{}","message":{},{},"related":[{related}]}}"#,
string(&file.to_string_lossy()),
string(rule),
string(&crate::rules::help_uri(rule)),
level(severity),
string(message),
position(at),
)
}
fn position(at: Option<(Location, usize)>) -> String {
match at {
Some((at, offset)) => format!(
r#""line":{},"column":{},"offset":{offset}"#,
at.line, at.column
),
None => r#""line":null,"column":null,"offset":null"#.to_owned(),
}
}