Skip to main content

graphrecords_query/error/
argument.rs

1use crate::Diagnostic;
2use std::{
3    error::Error,
4    fmt::{self, Display, Formatter},
5};
6
7#[derive(Debug, Clone, Copy)]
8pub enum Absent {
9    Uncovered,
10    Empty,
11}
12
13impl Display for Absent {
14    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
15        match self {
16            Self::Uncovered => formatter.write_str("argument did not cover this index"),
17            Self::Empty => formatter.write_str("argument provided no value for this lookup"),
18        }
19    }
20}
21
22impl Error for Absent {}
23
24#[derive(Debug)]
25pub struct ArgumentAbsent {
26    cause: Absent,
27}
28
29impl ArgumentAbsent {
30    #[must_use]
31    pub const fn new(cause: Absent) -> Self {
32        Self { cause }
33    }
34
35    #[must_use]
36    pub const fn cause(&self) -> Absent {
37        self.cause
38    }
39}
40
41impl Display for ArgumentAbsent {
42    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
43        write!(formatter, "{}", self.cause)
44    }
45}
46
47impl Error for ArgumentAbsent {
48    fn source(&self) -> Option<&(dyn Error + 'static)> {
49        Some(&self.cause)
50    }
51}
52
53impl Diagnostic for ArgumentAbsent {
54    fn name() -> &'static str {
55        "ArgumentAbsent"
56    }
57
58    fn help(&self) -> Option<String> {
59        Some(
60            "make the argument cover the subject's elements or state a policy with `on_missing(...)`"
61                .to_string(),
62        )
63    }
64}