irox_gpx/
error.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2023 IROX Contributors
3
4use std::fmt::{Display, Formatter};
5
6#[derive(Debug, Copy, Clone, Eq, PartialEq)]
7pub enum ErrorType {
8    IOError,
9    XMLError,
10}
11
12#[derive(Debug, Clone, Eq, PartialEq)]
13pub struct Error {
14    error_type: ErrorType,
15    msg: String,
16}
17
18impl Error {
19    pub fn new(error_type: ErrorType, msg: String) -> Error {
20        Error { error_type, msg }
21    }
22    pub fn ioe<T>(msg: &'static str) -> Result<T, Error> {
23        Err(Error::new(ErrorType::IOError, msg.to_string()))
24    }
25}
26
27impl Display for Error {
28    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29        f.write_fmt(format_args!(
30            "GPXError({:?}): {}",
31            self.error_type, self.msg
32        ))
33    }
34}
35
36impl std::error::Error for Error {}
37
38impl From<std::io::Error> for Error {
39    fn from(value: std::io::Error) -> Self {
40        Error {
41            error_type: ErrorType::IOError,
42            msg: value.to_string(),
43        }
44    }
45}
46
47impl From<xml::writer::Error> for Error {
48    fn from(value: xml::writer::Error) -> Self {
49        Error {
50            error_type: ErrorType::XMLError,
51            msg: value.to_string(),
52        }
53    }
54}
55
56impl From<xml::reader::Error> for Error {
57    fn from(value: xml::reader::Error) -> Self {
58        Error {
59            error_type: ErrorType::XMLError,
60            msg: value.to_string(),
61        }
62    }
63}