pdf_rs/
error.rs

1use std::num::{ParseFloatError, ParseIntError};
2use std::string::FromUtf8Error;
3use crate::error::error_kind::{FLOAT_PARSE_ERROR, INT_PARSE_ERROR, INVALID_UTF8_STR, STD_IO_ERROR};
4
5macro_rules! error_kind {
6    ($(($id:ident,$code:literal,$message:literal)),+$(,)?) => {
7        pub(crate) mod error_kind{
8        $(
9            pub(crate) const $id: super::Kind = ($code, $message);
10        )+
11    }
12    };
13}
14
15pub(crate) type Kind = (u16, &'static str);
16
17pub type Result<T> = std::result::Result<T, Error>;
18
19/// Enumurate all error kind
20error_kind!(
21    (INVALID_PDF_VERSION, 1000, "Invalid PDF version"),
22    (STD_IO_ERROR, 1001, "Std IO Error"),
23    (INVALID_PDF_FILE, 1002, "Invalid PDF file"),
24    (TRAILER_NOT_FOUND, 1003, "Trailer not found"),
25    (EOF, 1004, "End of file"),
26    (INVALID_UTF8_STR,1005, "Invalid UTF8 string"),
27    (INT_PARSE_ERROR,1006, "Int parse error"),
28    (INVALID_CROSS_TABLE_ENTRY,1007, "Invalid cross table entry"),
29    (TRAILER_EXCEPT_A_DICT,1008, "Trailer except a dict"),
30    (INVALID_NUMBER,1009, "Invalid number"),
31    (FLOAT_PARSE_ERROR,1010, "Float parse error"),
32    (EXCEPT_TOKEN,1011, "Except a token"),
33    (STR_NOT_ENCODED,1012, "String not encoded"),
34    (ILLEGAL_TOKEN,1013, "Illegal token"),
35    (INVALID_REAL_NUMBER,1014, "Invalid real number"),
36    (PARSE_UNSIGNED_VALUE_ERR,1015, "Parse unsigned value error"),
37    (SEEK_EXEED_MAX_SIZE,1016, "Seek exceed max size"),
38);
39
40#[derive(Debug)]
41struct Inner {
42    pub code: u16,
43    pub message: String,
44}
45
46#[derive(Debug)]
47pub struct Error {
48    inner: Inner,
49}
50
51impl Error {
52    pub(crate) fn new(kind: Kind, message: String) -> Self {
53        Self {
54            inner: Inner {
55                code: kind.0,
56                message,
57            },
58        }
59    }
60}
61
62impl From<Kind> for Error {
63    fn from(kind: Kind) -> Self {
64        Self {
65            inner: Inner {
66                code: kind.0,
67                message: kind.1.to_string(),
68            },
69        }
70    }
71}
72
73impl From<std::io::Error> for Error {
74    fn from(e: std::io::Error) -> Self {
75        Self {
76            inner: Inner {
77                code: STD_IO_ERROR.0,
78                message: e.to_string(),
79            },
80        }
81    }
82}
83
84impl From<FromUtf8Error> for Error {
85    fn from(e: FromUtf8Error) -> Self {
86        Self {
87            inner: Inner {
88                code: INVALID_UTF8_STR.0,
89                message: e.to_string(),
90            },
91        }
92    }
93}
94
95
96impl From<ParseIntError> for Error{
97    fn from(e: ParseIntError) -> Self {
98        Self {
99            inner: Inner {
100                code: INT_PARSE_ERROR.0,
101                message: e.to_string(),
102            },
103        }
104    }
105}
106
107impl From<ParseFloatError> for Error {
108    fn from(e: ParseFloatError) -> Self {
109
110        Self {
111            inner: Inner {
112                code: FLOAT_PARSE_ERROR.0,
113                message: e.to_string(),
114            },
115        }
116    }
117}