use std::collections::BTreeMap;
use toml_edit::{Item, Table, Value};
use crate::path::DottedPath;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Report {
pub(crate) diagnostics: Vec<Diagnostic>,
}
impl Report {
pub fn diagnostics(&self) -> &[Diagnostic] {
&self.diagnostics
}
pub fn has_errors(&self) -> bool {
self.diagnostics
.iter()
.any(|d| d.severity == Severity::Error)
}
pub(crate) fn push(&mut self, diagnostic: Diagnostic) {
self.diagnostics.push(diagnostic);
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic {
pub kind: DiagnosticKind,
pub severity: Severity,
pub path: DottedPath,
pub line: usize,
pub column: usize,
}
impl Diagnostic {
pub(crate) fn severity_of(kind: &DiagnosticKind) -> Severity {
match kind {
DiagnosticKind::UnknownKey | DiagnosticKind::Migrated { .. } => Severity::Warning,
DiagnosticKind::TypeMismatch { .. } => Severity::Error,
}
}
pub(crate) fn new(kind: DiagnosticKind, path: DottedPath, at: Position) -> Self {
Self {
severity: Self::severity_of(&kind),
kind,
path,
line: at.line,
column: at.column,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiagnosticKind {
UnknownKey,
TypeMismatch {
expected: TomlType,
found: TomlType,
},
Migrated {
from: DottedPath,
},
}
impl std::fmt::Display for DiagnosticKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UnknownKey => f.write_str("no such option in the defaults"),
Self::TypeMismatch { expected, found } => {
write!(f, "expected {expected}, found {found}")
}
Self::Migrated { from } => write!(f, "moved from {from}"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Severity {
Warning,
Error,
}
impl std::fmt::Display for Severity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Warning => "warning",
Self::Error => "error",
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TomlType {
String,
Integer,
Float,
Boolean,
Datetime,
Array,
InlineTable,
Table,
ArrayOfTables,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SpanIndex {
pub(crate) spans: BTreeMap<DottedPath, Span>,
}
impl SpanIndex {
pub(crate) fn build(root: &Table) -> Self {
let mut index = Self::default();
index.walk(root, None);
index
}
fn walk(&mut self, table: &Table, path: Option<&DottedPath>) {
for (name, item) in table.iter() {
let child = match path {
Some(path) => path.child(name),
None => DottedPath::new(name),
};
if let Some(key) = table.key(name)
&& let Some(span) = key.span()
{
self.spans.insert(
child.clone(),
Span {
start: span.start,
end: span.end,
},
);
}
if let Item::Table(sub) = item {
self.walk(sub, Some(&child));
}
}
}
pub(crate) fn position(&self, src: &str, path: &DottedPath) -> Position {
match self.spans.get(path) {
Some(span) => Position::of(src, span.start),
None => Position { line: 1, column: 1 },
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Span {
pub start: usize,
pub end: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Position {
pub line: usize,
pub column: usize,
}
impl Position {
pub(crate) fn of(src: &str, offset: usize) -> Self {
let offset = offset.min(src.len());
let before = &src[..offset];
let line = before.matches('\n').count() + 1;
let column = match before.rfind('\n') {
Some(at) => before[at + 1..].chars().count() + 1,
None => before.chars().count() + 1,
};
Self { line, column }
}
}
impl TomlType {
pub(crate) fn of(item: &Item) -> Option<Self> {
match item {
Item::Value(value) => Some(Self::of_value(value)),
Item::Table(_) => Some(Self::Table),
Item::ArrayOfTables(_) => Some(Self::ArrayOfTables),
Item::None => None,
}
}
fn of_value(value: &Value) -> Self {
match value {
Value::String(_) => Self::String,
Value::Integer(_) => Self::Integer,
Value::Float(_) => Self::Float,
Value::Boolean(_) => Self::Boolean,
Value::Datetime(_) => Self::Datetime,
Value::Array(_) => Self::Array,
Value::InlineTable(_) => Self::InlineTable,
}
}
pub fn name(self) -> &'static str {
match self {
Self::String => "string",
Self::Integer => "integer",
Self::Float => "float",
Self::Boolean => "boolean",
Self::Datetime => "datetime",
Self::Array => "array",
Self::InlineTable => "inline table",
Self::Table => "table",
Self::ArrayOfTables => "array of tables",
}
}
}
impl std::fmt::Display for TomlType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name())
}
}