syn_select/
error.rs

1use std::fmt;
2
3/// An error encountered while parsing or executing a selector.
4#[derive(Debug)]
5pub struct Error {
6    kind: ErrorKind,
7}
8
9impl Error {
10    fn new(kind: ErrorKind) -> Self {
11        Error { kind }
12    }
13
14    /// Create an error indicating the caller provided an empty path to search.
15    pub(crate) fn empty_path() -> Self {
16        Error::new(ErrorKind::EmptyPath)
17    }
18
19    /// Create an error indicating the caller provided a non-empty string that
20    /// couldn't be parsed to a searchable path.
21    pub(crate) fn invalid_segment(segment: String) -> Self {
22        Error::new(ErrorKind::InvalidSegment(segment))
23    }
24}
25
26impl std::error::Error for Error {}
27
28impl fmt::Display for Error {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        match &self.kind {
31            ErrorKind::EmptyPath => f.write_str("Empty path"),
32            ErrorKind::InvalidSegment(segment) => write!(
33                f,
34                "Invalid path segment: `{}` is not an identifier",
35                segment
36            ),
37        }
38    }
39}
40
41#[derive(Debug)]
42enum ErrorKind {
43    /// The selector parser was passed an empty string.
44    EmptyPath,
45    /// The selector parser was passed a non-empty string that had
46    /// an invalid part after being split by the path separator.
47    InvalidSegment(String),
48}