use serde::{Deserialize, Serialize};
use crate::path::Path;
use crate::tree::{Node, Span};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum Severity {
Error,
Warning,
Hint,
}
impl Severity {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Error => "error",
Self::Warning => "warning",
Self::Hint => "hint",
}
}
}
#[derive(Debug, PartialEq, Eq, Serialize)]
pub struct Code {
pub name: &'static str,
pub severity: Severity,
pub level: u8,
pub summary: &'static str,
}
impl Code {
#[must_use]
pub fn layer(&self) -> &'static str {
self.name.split('.').next().unwrap_or(self.name)
}
}
pub const REVIEW_PREFIX: &str = "review.";
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum Applicability {
Certain,
Likely,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Edit {
Text {
offset: usize,
length: usize,
replacement: String,
},
Replace {
path: Path,
value: Node,
},
RenameKey {
path: Path,
to: String,
},
Remove {
path: Path,
},
Insert {
path: Path,
value: Node,
},
Move {
from: Path,
to: Path,
},
}
impl Edit {
#[must_use]
pub const fn path(&self) -> Option<&Path> {
match self {
Self::Text { .. } => None,
Self::Replace { path, .. }
| Self::RenameKey { path, .. }
| Self::Remove { path }
| Self::Insert { path, .. } => Some(path),
Self::Move { from, .. } => Some(from),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Fix {
pub title: String,
pub applicability: Applicability,
pub edits: Vec<Edit>,
}
impl Fix {
#[must_use]
pub fn certain(title: impl Into<String>, edits: Vec<Edit>) -> Self {
Self {
title: title.into(),
applicability: Applicability::Certain,
edits,
}
}
#[must_use]
pub fn likely(title: impl Into<String>, edits: Vec<Edit>) -> Self {
Self {
title: title.into(),
applicability: Applicability::Likely,
edits,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Reference {
pub topic: String,
pub definition: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Diagnostic {
pub code: String,
pub severity: Severity,
pub level: u8,
pub path: Path,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub span: Option<Span>,
pub message: String,
#[serde(default)]
pub help: Vec<String>,
#[serde(default)]
pub fixes: Vec<Fix>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reference: Option<Reference>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub subject: Option<Node>,
#[serde(default = "one")]
pub count: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub also: Vec<Path>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub data: Option<Node>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub origin: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub excerpt: Option<String>,
}
const fn one() -> usize {
1
}
impl Diagnostic {
#[must_use]
pub fn new(code: &'static Code, path: Path, message: impl Into<String>) -> Self {
Self::external(code.name, code.severity, code.level, path, message)
}
#[must_use]
pub fn external(
code: impl Into<String>,
severity: Severity,
level: u8,
path: Path,
message: impl Into<String>,
) -> Self {
Self {
code: code.into(),
severity,
level,
path,
span: None,
message: message.into(),
help: Vec::new(),
fixes: Vec::new(),
reference: None,
subject: None,
count: 1,
also: Vec::new(),
data: None,
origin: None,
excerpt: None,
}
}
#[must_use]
pub const fn at(mut self, span: Option<Span>) -> Self {
self.span = span;
self
}
#[must_use]
pub fn help(mut self, line: impl Into<String>) -> Self {
self.help.push(line.into());
self
}
#[must_use]
pub fn fix(mut self, fix: Fix) -> Self {
self.fixes.push(fix);
self
}
#[must_use]
pub fn reference(mut self, topic: impl Into<String>, definition: impl Into<String>) -> Self {
self.reference = Some(Reference {
topic: topic.into(),
definition: definition.into(),
});
self
}
#[must_use]
pub fn subject(mut self, subject: Node) -> Self {
self.subject = Some(subject);
self
}
#[must_use]
pub fn data(mut self, data: Node) -> Self {
self.data = Some(data);
self
}
#[must_use]
pub fn origin(mut self, origin: impl Into<String>) -> Self {
self.origin = Some(origin.into());
self
}
#[must_use]
pub fn is(&self, code: &Code) -> bool {
self.code == code.name
}
#[must_use]
pub fn is_error(&self) -> bool {
self.severity == Severity::Error
}
#[must_use]
pub fn datum(&self, key: &str) -> Option<&Node> {
self.data.as_ref()?.get(key)
}
#[must_use]
pub fn has_advice(&self) -> bool {
!self.help.is_empty() || !self.fixes.is_empty()
}
}
#[derive(Clone, Debug)]
pub struct Collector {
items: Vec<Diagnostic>,
dropped: usize,
capacity: usize,
}
impl Default for Collector {
fn default() -> Self {
Self::new(Self::DEFAULT_CAPACITY)
}
}
impl Collector {
pub const DEFAULT_CAPACITY: usize = 1000;
#[must_use]
pub const fn new(capacity: usize) -> Self {
Self {
items: Vec::new(),
dropped: 0,
capacity,
}
}
pub fn push(&mut self, diagnostic: Diagnostic) {
if self.items.len() < self.capacity {
self.items.push(diagnostic);
} else {
self.dropped += 1;
}
}
#[must_use]
pub const fn is_full(&self) -> bool {
self.items.len() >= self.capacity
}
#[must_use]
pub fn items(&self) -> &[Diagnostic] {
&self.items
}
pub fn items_mut(&mut self) -> &mut [Diagnostic] {
&mut self.items
}
#[must_use]
pub const fn dropped(&self) -> usize {
self.dropped
}
#[must_use]
pub fn has_errors(&self) -> bool {
self.items.iter().any(Diagnostic::is_error)
}
#[must_use]
pub fn into_parts(self) -> (Vec<Diagnostic>, usize) {
(self.items, self.dropped)
}
}
impl Extend<Diagnostic> for Collector {
fn extend<I: IntoIterator<Item = Diagnostic>>(&mut self, items: I) {
for item in items {
self.push(item);
}
}
}