use std::fmt::{Debug, Display, Formatter};
#[derive(Debug)]
pub enum KissXmlError {
ParsingError(ParsingError),
TypeCastError(TypeCastError),
DoesNotExistError(DoesNotExistError),
IndexOutOfBounds(IndexOutOfBounds),
InvalidAttributeName(InvalidAttributeName),
InvalidElementName(InvalidElementName),
InvalidContent(InvalidContent),
NotSupportedError(NotSupportedError),
IOError(std::io::Error),
}
impl From<std::io::Error> for KissXmlError {
fn from(e: std::io::Error) -> Self {KissXmlError::IOError(e)}
}
impl Display for KissXmlError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
KissXmlError::ParsingError(e) => write!(f, "{}", e),
KissXmlError::TypeCastError(e) => write!(f, "{}", e),
KissXmlError::DoesNotExistError(e) => write!(f, "{}", e),
KissXmlError::IndexOutOfBounds(e) => write!(f, "{}", e),
KissXmlError::InvalidAttributeName(e) => write!(f, "{}", e),
KissXmlError::InvalidElementName(e) => write!(f, "{}", e),
KissXmlError::InvalidContent(e) => write!(f, "{}", e),
KissXmlError::NotSupportedError(e) => write!(f, "{}", e),
KissXmlError::IOError(e) => write!(f, "{}", e),
}
}
}
impl std::error::Error for KissXmlError{}
#[derive(Clone, Debug)]
pub struct ParsingError {
pub msg: String
}
impl ParsingError{
pub fn new(msg: impl Into<String>) -> Self {
let mut m = msg.into();
m = m.trim_start_matches("ParsingError: ").to_string();
Self{msg: m}
}
}
impl From<ParsingError> for KissXmlError {
fn from(e: ParsingError) -> Self {KissXmlError::ParsingError(e)}
}
impl Display for ParsingError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "ParsingError: {}", self.msg)
}
}
impl std::error::Error for ParsingError{}
#[derive(Clone, Debug)]
pub struct TypeCastError {
pub msg: String
}
impl TypeCastError{
pub fn new(msg: impl Into<String>) -> Self {
Self{msg: msg.into()}
}
}
impl From<TypeCastError> for KissXmlError {
fn from(e: TypeCastError) -> Self {KissXmlError::TypeCastError(e)}
}
impl Display for TypeCastError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "TypeCastError: {}", self.msg)
}
}
impl std::error::Error for TypeCastError{}
#[derive(Clone, Debug)]
pub struct DoesNotExistError {
pub msg: String
}
impl DoesNotExistError{
pub fn new(msg: impl Into<String>) -> Self {
Self{msg: msg.into()}
}
}
impl From<DoesNotExistError> for KissXmlError {
fn from(e: DoesNotExistError) -> Self {KissXmlError::DoesNotExistError(e)}
}
impl Display for DoesNotExistError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "DoesNotExistError: {}", self.msg)
}
}
impl Default for DoesNotExistError {
fn default() -> Self {
Self{msg: String::from("requested item not found")}
}
}
impl std::error::Error for DoesNotExistError{}
#[derive(Clone, Debug)]
pub struct IndexOutOfBounds {
pub index: isize,
pub bounds: Option<(isize, isize)>
}
impl IndexOutOfBounds{
pub fn new(index: isize, bounds: Option<(isize, isize)>) -> Self {
Self{index, bounds}
}
}
impl From<IndexOutOfBounds> for KissXmlError {
fn from(e: IndexOutOfBounds) -> Self {KissXmlError::IndexOutOfBounds(e)}
}
impl Display for IndexOutOfBounds {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.bounds {
Some(b) => write!(f, "Index {} is out of bounds (valid range: {} - {})", &self.index, b.0, b.1),
None => write!(f, "Index {} is out of bounds", &self.index)
}
}
}
impl std::error::Error for IndexOutOfBounds{}
#[derive(Clone, Debug)]
pub struct InvalidAttributeName {
pub msg: String
}
impl InvalidAttributeName{
pub fn new(msg: impl Into<String>) -> Self {
Self{msg: msg.into()}
}
}
impl From<InvalidAttributeName> for KissXmlError {
fn from(e: InvalidAttributeName) -> Self {KissXmlError::InvalidAttributeName(e)}
}
impl Display for InvalidAttributeName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "InvalidAttributeName: {}", self.msg)
}
}
impl std::error::Error for InvalidAttributeName{}
#[derive(Clone, Debug)]
pub struct InvalidElementName {
pub msg: String
}
impl InvalidElementName{
pub fn new(msg: impl Into<String>) -> Self {
Self{msg: msg.into()}
}
}
impl From<InvalidElementName> for KissXmlError {
fn from(e: InvalidElementName) -> Self {KissXmlError::InvalidElementName(e)}
}
impl Display for InvalidElementName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "InvalidElementName: {}", self.msg)
}
}
impl std::error::Error for InvalidElementName{}
#[derive(Clone, Debug)]
pub struct InvalidContent {
pub msg: String
}
impl InvalidContent{
pub fn new(msg: impl Into<String>) -> Self {
Self{msg: msg.into()}
}
}
impl From<InvalidContent> for KissXmlError {
fn from(e: InvalidContent) -> Self {KissXmlError::InvalidContent(e)}
}
impl Display for InvalidContent {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "InvalidContent: {}", self.msg)
}
}
impl std::error::Error for InvalidContent{}
#[derive(Clone, Debug)]
pub struct NotSupportedError {
pub msg: String
}
impl NotSupportedError{
pub fn new(msg: impl Into<String>) -> Self {
Self{msg: msg.into()}
}
}
impl From<NotSupportedError> for KissXmlError {
fn from(e: NotSupportedError) -> Self {KissXmlError::NotSupportedError(e)}
}
impl Display for NotSupportedError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "NotSupportedError: {}", self.msg)
}
}
impl std::error::Error for NotSupportedError{}