use flavored::RpName;
use std::fmt;
use std::slice;
use {Source, Span};
#[derive(Debug, Clone, Copy, Serialize)]
pub enum SymbolKind {
#[serde(rename = "type")]
Type,
#[serde(rename = "interface")]
Interface,
#[serde(rename = "tuple")]
Tuple,
#[serde(rename = "enum")]
Enum,
#[serde(rename = "service")]
Service,
}
#[derive(Debug, Clone)]
pub enum Diagnostic {
Error(Span, String),
Info(Span, String),
Symbol {
kind: SymbolKind,
span: Span,
name: RpName,
},
}
#[derive(Debug, Clone)]
pub struct Diagnostics {
pub source: Source,
pub items: Vec<Diagnostic>,
}
impl Diagnostics {
pub fn new(source: Source) -> Self {
Self {
source,
items: Vec::new(),
}
}
pub fn has_errors(&self) -> bool {
self.items.iter().any(|item| match *item {
Diagnostic::Error(_, _) => true,
_ => false,
})
}
pub fn err<S: Into<Span>, E: fmt::Display>(&mut self, span: S, error: E) {
self.items
.push(Diagnostic::Error(span.into(), error.to_string()));
}
pub fn info<S: Into<Span>, I: fmt::Display>(&mut self, span: S, info: I) {
self.items
.push(Diagnostic::Info(span.into(), info.to_string()));
}
pub fn symbol<P: Into<Span>>(&mut self, kind: SymbolKind, span: P, name: &RpName) {
self.items.push(Diagnostic::Symbol {
kind,
span: span.into(),
name: name.clone(),
});
}
pub fn items(&self) -> Items {
Items {
iter: self.items.iter(),
}
}
pub fn clear(&mut self) {
self.items.clear();
}
}
pub struct Items<'a> {
iter: slice::Iter<'a, Diagnostic>,
}
impl<'a> Iterator for Items<'a> {
type Item = <slice::Iter<'a, Diagnostic> as Iterator>::Item;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
}