1use std::{error, fmt, result};
2
3pub type Result<T> = result::Result<T, Error>;
4
5#[derive(Debug, PartialEq, Eq)]
6pub enum Error {
7 RequiredFieldNotFound {
8 name: &'static str,
9 },
10 InvalidFieldType {
11 optional: bool,
12 name: &'static str,
13 found: &'static str,
14 required: &'static str,
15 },
16 InvalidTokenFound {
17 found: char,
18 expected: &'static str,
19 index: usize,
20 },
21 NoEndMarker {
22 found: char,
23 index: usize,
24 },
25 WrongStringLength {
26 length: usize,
27 index: usize,
28 },
29 NoValueForKey {
30 key: String,
31 index: usize,
32 },
33}
34
35impl fmt::Display for Error {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 match self {
38 Self::RequiredFieldNotFound { name } => {
39 writeln!(f, "required field {name} was not found in given file")
40 }
41 Self::InvalidFieldType {
42 optional,
43 name,
44 found,
45 required,
46 } => writeln!(
47 f,
48 "{req} filed {name} has invalid type, required: {required}, found: {found}",
49 req = if *optional { "Optional" } else { "Required" }
50 ),
51 Self::InvalidTokenFound {
52 found,
53 expected,
54 index,
55 } => writeln!(
56 f,
57 "invalid token found at index {index}, expected {expected}, found: {found}"
58 ),
59 Self::NoEndMarker { found, index } => {
60 writeln!(f, "The marker {found} at index {index} has no end marker")
61 }
62 Self::WrongStringLength { length, index } => writeln!(
63 f,
64 "The string at index {index} has an invalid length {length}"
65 ),
66 Self::NoValueForKey { key, index } => {
67 writeln!(f, "The key {key} at index {index} has no value")
68 }
69 }
70 }
71}
72
73impl error::Error for Error {}