async-graphql/async-graphql-parser/src/lib.rs

54 lines
1.3 KiB
Rust
Raw Normal View History

2020-09-06 05:38:31 +00:00
//! 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)]
2020-09-06 05:38:31 +00:00
use pest::error::LineColLocation;
2020-09-06 06:16:36 +00:00
use pest::RuleType;
use std::fmt;
2020-09-06 05:38:31 +00:00
pub use parser::parse_query;
2020-09-06 06:16:36 +00:00
pub use pos::{Pos, Positioned};
2020-09-06 05:38:31 +00:00
pub mod types;
2020-05-15 03:42:01 +00:00
2020-09-06 05:38:31 +00:00
mod parser;
2020-09-06 06:16:36 +00:00
mod pos;
2020-05-15 03:42:01 +00:00
mod utils;
2020-09-06 05:38:31 +00:00
/// 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 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 {
2020-09-06 06:16:36 +00:00
LineColLocation::Pos((line, column))
| LineColLocation::Span((line, column), _) => Pos { line, column },
2020-09-06 05:38:31 +00:00
}
},
message: err.to_string(),
}
}
}
/// An alias for `Result<T, Error>`.
pub type Result<T> = std::result::Result<T, Error>;