cynic_parser_deser/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::fmt;

use cynic_parser::Span;

use crate::{value::ValueType, DeserValue};

// TODO: Should these errors have paths in them as well?

#[derive(Debug)]
pub enum Error {
    UnexpectedType {
        expected: ValueType,
        found: ValueType,
        span: Option<Span>,
    },
    MissingField {
        name: String,
        object_span: Option<Span>,
    },
    UnknownField {
        name: String,
        field_type: ValueType,
        field_span: Option<Span>,
    },
    DuplicateField {
        name: String,
        original_field_span: Option<Span>,
        duplicate_field_span: Option<Span>,
    },
    Custom {
        text: String,
        span: Option<Span>,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::UnexpectedType {
                expected, found, ..
            } => write!(f, "found a {found} where we expected a {expected}"),
            Error::MissingField { name, .. } => write!(f, "missing field: {name}"),
            Error::UnknownField { name, .. } => write!(f, "unknown field: {name}"),
            Error::DuplicateField { name, .. } => write!(f, "duplicate field: {name}"),
            Error::Custom { text, .. } => write!(f, "{text}"),
        }
    }
}

impl std::error::Error for Error {}

impl Error {
    pub fn unexpected_type(expected: ValueType, found: DeserValue<'_>) -> Self {
        Error::UnexpectedType {
            expected,
            found: ValueType::from(found),
            span: found.span(),
        }
    }

    pub fn custom(text: impl Into<String>, span: Option<Span>) -> Self {
        Error::Custom {
            text: text.into(),
            span,
        }
    }
}