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