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
//! A parser for GraphQL. Used in the [`async-graphql`](https://crates.io/crates/async-graphql)
//! crate.
//!
//! It uses the [pest](https://crates.io/crates/pest) crate to parse the input and then transforms
//! it into Rust types.
#![forbid(unsafe_code)]

use pest::error::LineColLocation;
use pest::RuleType;
use std::fmt;

pub use parse::{parse_query, parse_schema};
pub use pos::{Pos, Positioned};

pub mod types;

mod parse;
mod pos;

/// Parser error.
#[derive(Debug, PartialEq)]
pub struct Error {
    /// The position at which the error occurred.
    pub pos: Pos,
    /// The error message.
    pub message: String,
}

impl Error {
    /// Create a new error with the given position and message.
    pub fn new(message: impl Into<String>, pos: Pos) -> Self {
        Self {
            pos,
            message: message.into(),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}

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

impl<R: RuleType> From<pest::error::Error<R>> for Error {
    fn from(err: pest::error::Error<R>) -> Self {
        Error {
            pos: {
                match err.line_col {
                    LineColLocation::Pos((line, column))
                    | LineColLocation::Span((line, column), _) => Pos { line, column },
                }
            },
            message: err.to_string(),
        }
    }
}

/// An alias for `Result<T, Error>`.
pub type Result<T> = std::result::Result<T, Error>;