#![allow(clippy::module_name_repetitions)]
use std::fmt::Debug;
#[cfg(feature = "miette")]
use miette::{Diagnostic, SourceSpan};
use thiserror::Error;
use crate::Span;
#[derive(Clone, Error)]
#[cfg_attr(feature = "miette", derive(Diagnostic))]
#[error("Invalid beancount syntax at line: {line_number}")]
pub struct Error {
#[cfg(feature = "miette")]
#[source_code]
src: String,
#[cfg(feature = "miette")]
#[label]
span: SourceSpan,
line_number: u32,
}
impl Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Error")
.field("line_number", &self.line_number())
.finish()
}
}
impl Error {
#[cfg(not(feature = "miette"))]
pub(crate) fn new(_: impl Into<String>, span: Span<'_>) -> Self {
Self {
line_number: span.location_line(),
}
}
#[cfg(feature = "miette")]
pub(crate) fn new(src: impl Into<String>, span: Span<'_>) -> Self {
Self {
src: src.into(),
span: span.location_offset().into(),
line_number: span.location_line(),
}
}
#[must_use]
pub fn line_number(&self) -> u32 {
self.line_number
}
}
#[allow(missing_docs)]
#[derive(Debug, Error)]
#[cfg_attr(feature = "miette", derive(Diagnostic))]
pub enum ReadFileError {
#[error("IO error")]
Io(#[from] std::io::Error),
#[error("Syntax error")]
Syntax(#[from] Error),
}
#[derive(Debug, Clone, Error)]
#[non_exhaustive]
#[error("Cannot convert to the desired type")]
pub struct ConversionError;