use crate::raw::model::RawFormat;
use std::error::Error as StdError;
use std::fmt;
use super::InputSource;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ParameterErrorKind {
SizeOverflow,
#[non_exhaustive]
Allocation {
requested_bytes: usize,
},
Missing,
Duplicate,
Malformed,
Invalid,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParameterError(Box<ParameterErrorInner>);
#[derive(Clone, Debug, Eq, PartialEq)]
struct ParameterErrorInner {
format: RawFormat,
source: InputSource,
parameter: Option<String>,
kind: ParameterErrorKind,
detail: String,
}
impl ParameterError {
pub(crate) fn new(
format: RawFormat,
source: InputSource,
parameter: Option<String>,
kind: ParameterErrorKind,
detail: impl Into<String>,
) -> Self {
Self(Box::new(ParameterErrorInner {
format,
source,
parameter,
kind,
detail: detail.into(),
}))
}
pub fn format(&self) -> RawFormat {
self.0.format
}
pub fn input_source(&self) -> &InputSource {
&self.0.source
}
pub fn parameter(&self) -> Option<&str> {
self.0.parameter.as_deref()
}
pub fn kind(&self) -> ParameterErrorKind {
self.0.kind
}
pub fn detail(&self) -> &str {
&self.0.detail
}
}
impl fmt::Display for ParameterError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let parameter = self.0.parameter.as_deref().unwrap_or("parameter record");
write!(
formatter,
"{:?} {parameter} error in {}: {}",
self.0.kind, self.0.source, self.0.detail
)
}
}
impl StdError for ParameterError {}