use std::fmt;
use std::path::{Path, PathBuf};
use crate::io::Format;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("{}: {source}", display_path(path.as_deref()))]
Io {
path: Option<PathBuf>,
#[source]
source: std::io::Error,
},
#[error("{}:{line}:{column}: invalid {format} input: {message}", display_path(path.as_deref()))]
Parse {
format: Format,
path: Option<PathBuf>,
line: usize,
column: usize,
message: String,
},
#[error("unsupported input: {0}")]
Unsupported(String),
#[error("invalid input: {0}")]
InvalidInput(String),
#[error("no atoms selected: {0}")]
EmptySelection(String),
#[error("cancelled")]
Cancelled,
#[error("internal error: {0}")]
Internal(String),
#[error("{what} exceeds the configured limit of {limit} bytes")]
ResourceLimit {
what: String,
limit: u64,
},
}
fn display_path(path: Option<&Path>) -> String {
path.map_or_else(|| "<input>".to_owned(), |p| p.display().to_string())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ParseError {
pub(crate) line: usize,
pub(crate) column: usize,
pub(crate) message: String,
}
impl ParseError {
pub(crate) fn new(line: usize, column: usize, message: impl Into<String>) -> Self {
Self {
line,
column,
message: message.into(),
}
}
pub(crate) fn into_error(self, format: Format, path: Option<PathBuf>) -> Error {
Error::Parse {
format,
path,
line: self.line,
column: self.column,
message: self.message,
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}: {}", self.line, self.column, self.message)
}
}