use std::error::Error;
use std::fmt::{self, Display, Formatter};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseHgvsError {
kind: ParseHgvsErrorKind,
code: &'static str,
message: String,
input: String,
fragment: Option<String>,
parser_version: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseHgvsErrorKind {
InvalidSyntax,
UnsupportedSyntax,
SemanticConstraint,
}
impl ParseHgvsError {
pub(crate) fn new(
kind: ParseHgvsErrorKind,
code: &'static str,
message: impl Into<String>,
input: impl Into<String>,
fragment: Option<String>,
) -> Self {
Self {
kind,
code,
message: message.into(),
input: input.into(),
fragment,
parser_version: env!("CARGO_PKG_VERSION"),
}
}
pub(crate) fn invalid(input: &str) -> Self {
Self::new(
ParseHgvsErrorKind::InvalidSyntax,
"invalid.syntax",
"failed to parse HGVS variant",
input,
None,
)
}
pub(crate) fn unsupported(
code: &'static str,
message: &'static str,
input: &str,
fragment: Option<String>,
) -> Self {
Self::new(
ParseHgvsErrorKind::UnsupportedSyntax,
code,
message,
input,
fragment,
)
}
pub fn kind(&self) -> ParseHgvsErrorKind {
self.kind
}
pub fn code(&self) -> &'static str {
self.code
}
pub fn message(&self) -> &str {
&self.message
}
pub fn input(&self) -> &str {
&self.input
}
pub fn fragment(&self) -> Option<&str> {
self.fragment.as_deref()
}
pub fn parser_version(&self) -> &'static str {
self.parser_version
}
}
impl Display for ParseHgvsError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"[{}] {}: `{}` (tinyhgvs {})",
self.code, self.message, self.input, self.parser_version
)
}
}
impl Error for ParseHgvsError {}