freedesktop_entry_parser/
errors.rs1use nom::error::{Error as NomError, ErrorKind};
6use std::error::Error;
7use std::fmt::{Debug, Display};
8use std::str::Utf8Error;
9
10#[derive(Debug)]
13pub enum ParseError {
14 Other {
17 at: ErrorBytes,
19 kind: ErrorKind,
21 },
22 Incomplete,
24 Utf8Error { bytes: Vec<u8>, source: Utf8Error },
26}
27
28impl Display for ParseError {
29 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 match self {
31 ParseError::Other { at, kind } => {
32 write!(f, "Error parings input: {} at {at}", kind.description())
33 }
34 ParseError::Incomplete => write!(f, "Incomplete input"),
35 ParseError::Utf8Error { source, .. } => Display::fmt(&source, f),
36 }
37 }
38}
39impl Error for ParseError {}
40
41#[derive(Debug)]
46pub enum ErrorBytes {
47 Valid(String),
49 Invalid(Vec<u8>),
51}
52
53impl Display for ErrorBytes {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 match self {
56 ErrorBytes::Valid(str) => Display::fmt(&str, f),
57 ErrorBytes::Invalid(bytes) => Debug::fmt(&bytes, f),
58 }
59 }
60}
61
62impl Error for ErrorBytes {}
63
64impl From<nom::Err<NomError<&[u8]>>> for ParseError {
65 fn from(e: nom::Err<NomError<&[u8]>>) -> Self {
66 match e {
67 nom::Err::Error(NomError { input, code })
68 | nom::Err::Failure(NomError { input, code }) => {
69 match std::str::from_utf8(input) {
70 Ok(s) => ParseError::Other {
71 at: ErrorBytes::Valid(s.to_owned()),
72 kind: code,
73 },
74 Err(_) => ParseError::Other {
75 at: ErrorBytes::Invalid(input.to_vec()),
76 kind: code,
77 },
78 }
79 }
80 nom::Err::Incomplete(_) => ParseError::Incomplete,
81 }
82 }
83}