use std::fmt;
use crate::span::Span;
#[derive(Clone)]
pub struct ParseWarning {
pub kind: ParseWarningKind,
pub span: Span,
}
#[derive(Debug, Clone)]
pub enum ParseWarningKind {
Deprecation(DeprecationWarning),
}
impl ParseWarningKind {
pub(crate) fn at(self, span: Span) -> ParseWarning {
ParseWarning { kind: self, span }
}
}
impl fmt::Display for ParseWarning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ParseWarningKind::Deprecation(warning) = &self.kind;
if let Some(std::ops::Range { start, end }) = self.span.range() {
write!(f, "{warning}\n at {start}..{end}")
} else {
write!(f, "{warning}")
}
}
}
impl fmt::Display for ParseWarningKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let ParseWarningKind::Deprecation(c) = self;
c.fmt(f)
}
}
#[derive(Debug, Clone)]
pub enum DeprecationWarning {
Unicode(String),
ShorthandInRange(char),
}
impl fmt::Display for DeprecationWarning {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DeprecationWarning::Unicode(u) => {
let rest = u.trim_start_matches(['U', '+']);
write!(f, "This syntax is deprecated. Use `U+{rest}` instead.")
}
&DeprecationWarning::ShorthandInRange(c) => {
write!(
f,
"Shorthands in character ranges are deprecated. Use U+{:02X} instead",
c as u8
)
}
}
}
}