iri_string/template/
error.rsuse core::fmt;
#[cfg(feature = "std")]
use std::error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ErrorKind {
WriteFailed,
ExpressionNotClosed,
InvalidCharacter,
InvalidExpression,
InvalidPercentEncoding,
InvalidUtf8,
UnexpectedValueType,
UnsupportedOperator,
}
impl ErrorKind {
#[must_use]
fn as_str(self) -> &'static str {
match self {
Self::WriteFailed => "failed to write to the backend writer",
Self::ExpressionNotClosed => "expression not closed",
Self::InvalidCharacter => "invalid character",
Self::InvalidExpression => "invalid expression",
Self::InvalidPercentEncoding => "invalid percent-encoded triplets",
Self::InvalidUtf8 => "invalid utf-8 byte sequence",
Self::UnexpectedValueType => "unexpected value type for the variable",
Self::UnsupportedOperator => "unsupported operator",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Error {
kind: ErrorKind,
location: usize,
}
impl Error {
#[inline]
#[must_use]
pub(super) fn new(kind: ErrorKind, location: usize) -> Self {
Self { kind, location }
}
#[cfg(test)]
pub(super) fn location(&self) -> usize {
self.location
}
}
impl fmt::Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"invalid URI template: {} (at {}-th byte)",
self.kind.as_str(),
self.location
)
}
}
#[cfg(feature = "std")]
impl error::Error for Error {}
#[cfg(feature = "alloc")]
pub struct CreationError<T> {
source: T,
error: Error,
}
#[cfg(feature = "alloc")]
impl<T> CreationError<T> {
#[must_use]
pub fn into_source(self) -> T {
self.source
}
#[must_use]
pub fn validation_error(&self) -> Error {
self.error
}
#[must_use]
pub(crate) fn new(error: Error, source: T) -> Self {
Self { source, error }
}
}
#[cfg(feature = "alloc")]
impl<T: fmt::Debug> fmt::Debug for CreationError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CreationError")
.field("source", &self.source)
.field("error", &self.error)
.finish()
}
}
#[cfg(feature = "alloc")]
impl<T: Clone> Clone for CreationError<T> {
fn clone(&self) -> Self {
Self {
source: self.source.clone(),
error: self.error,
}
}
}
#[cfg(feature = "alloc")]
impl<T> fmt::Display for CreationError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error.fmt(f)
}
}
#[cfg(feature = "std")]
impl<T: fmt::Debug> error::Error for CreationError<T> {}