use crate::xml::Location;
pub use crate::lowering::Reason as LoweringReason;
pub use crate::validator::{DefaultTransitionOwner, Reason as ValidationReason, WarningReason};
pub use crate::xml::Reason as ReadReason;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Span {
pub start_line: usize,
pub start_column: usize,
pub start_offset: usize,
pub end_line: usize,
pub end_column: usize,
pub end_offset: usize,
}
impl From<Location> for Span {
fn from(location: Location) -> Self {
Span {
start_line: location.start_line,
start_column: location.start_column,
start_offset: location.start_offset,
end_line: location.end_line,
end_column: location.end_column,
end_offset: location.end_offset,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Reason {
Read(ReadReason),
Lowering(LoweringReason),
Validation(ValidationReason),
Compile(CompileReason),
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompileReason {
#[non_exhaustive]
ExpressionCompileError {
source: String,
cause: String,
position: Option<(usize, usize)>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Refusal {
reason: Box<Reason>,
message: String,
span: Span,
}
impl Refusal {
pub(crate) fn new(reason: Reason, message: String, location: Location) -> Self {
Refusal {
reason: Box::new(reason),
message,
span: location.into(),
}
}
pub fn reason(&self) -> &Reason {
&self.reason
}
pub fn message(&self) -> &str {
&self.message
}
pub fn span(&self) -> Span {
self.span
}
}
impl std::fmt::Display for Refusal {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(&self.message)
}
}
impl std::error::Error for Refusal {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Warning {
reason: WarningReason,
message: String,
span: Span,
}
impl Warning {
pub fn reason(&self) -> &WarningReason {
&self.reason
}
pub fn message(&self) -> &str {
&self.message
}
pub fn span(&self) -> Span {
self.span
}
}
impl From<crate::xml::ParseError> for Refusal {
fn from(error: crate::xml::ParseError) -> Self {
Refusal::new(Reason::Read(error.reason), error.message, error.location)
}
}
impl From<crate::lowering::LoweringError> for Refusal {
fn from(error: crate::lowering::LoweringError) -> Self {
Refusal::new(
Reason::Lowering(error.reason),
error.message,
error.location,
)
}
}
impl From<crate::validator::ValidationError> for Refusal {
fn from(error: crate::validator::ValidationError) -> Self {
Refusal::new(
Reason::Validation(error.reason),
error.message,
error.location,
)
}
}
impl From<crate::validator::Warning> for Warning {
fn from(warning: crate::validator::Warning) -> Self {
Warning {
reason: warning.reason,
message: warning.message,
span: warning.location.into(),
}
}
}