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    (NO_XREF_TABLE_FOUND,1017, "No xref table found")
39);
40
41#[derive(Debug)]
42struct Inner {
43    pub code: u16,
44    pub message: String,
45}
46
47#[derive(Debug)]
48pub struct Error {
49    inner: Inner,
50}
51
52impl Error {
53    pub(crate) fn new(kind: Kind, message: String) -> Self {
54        Self {
55            inner: Inner {
56                code: kind.0,
57                message,
58            },
59        }
60    }
61}
62
63impl From<Kind> for Error {
64    fn from(kind: Kind) -> Self {
65        Self {
66            inner: Inner {
67                code: kind.0,
68                message: kind.1.to_string(),
69            },
70        }
71    }
72}
73
74impl From<std::io::Error> for Error {
75    fn from(e: std::io::Error) -> Self {
76        Self {
77            inner: Inner {
78                code: STD_IO_ERROR.0,
79                message: e.to_string(),
80            },
81        }
82    }
83}
84
85impl From<FromUtf8Error> for Error {
86    fn from(e: FromUtf8Error) -> Self {
87        Self {
88            inner: Inner {
89                code: INVALID_UTF8_STR.0,
90                message: e.to_string(),
91            },
92        }
93    }
94}
95
96
97impl From<ParseIntError> for Error{
98    fn from(e: ParseIntError) -> Self {
99        Self {
100            inner: Inner {
101                code: INT_PARSE_ERROR.0,
102                message: e.to_string(),
103            },
104        }
105    }
106}
107
108impl From<ParseFloatError> for Error {
109    fn from(e: ParseFloatError) -> Self {
110
111        Self {
112            inner: Inner {
113                code: FLOAT_PARSE_ERROR.0,
114                message: e.to_string(),
115            },
116        }
117    }
118}