Skip to content

Commit 65b0250

Browse files
authored
Merge pull request #30 from graphql-rust/errors
Use `thiserror` crate instead of `failure`
2 parents 54a6aaa + df7cf33 commit 65b0250

File tree

6 files changed

+32
-33
lines changed

6 files changed

+32
-33
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ edition = "2018"
1616

1717
[dependencies]
1818
combine = "3.2.0"
19-
failure = "0.1.1"
19+
thiserror = "1.0.11"
2020

2121
[dev-dependencies]
2222
pretty_assertions = "0.5.0"

src/lib.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
//! -------------------------------
2121
//!
2222
//! ```rust
23-
//! # extern crate failure;
2423
//! # extern crate graphql_parser;
25-
//! use graphql_parser::parse_query;
24+
//! use graphql_parser::query::{parse_query, ParseError};
2625
//!
27-
//! # fn parse() -> Result<(), failure::Error> {
26+
//! # fn parse() -> Result<(), ParseError> {
2827
//! let ast = parse_query::<&str>("query MyQuery { field1, field2 }")?;
2928
//! // Format canonical representation
3029
//! assert_eq!(format!("{}", ast), "\
@@ -44,11 +43,10 @@
4443
//! --------------------------------
4544
//!
4645
//! ```rust
47-
//! # extern crate failure;
4846
//! # extern crate graphql_parser;
49-
//! use graphql_parser::parse_schema;
47+
//! use graphql_parser::schema::{parse_schema, ParseError};
5048
//!
51-
//! # fn parse() -> Result<(), failure::Error> {
49+
//! # fn parse() -> Result<(), ParseError> {
5250
//! let ast = parse_schema::<String>(r#"
5351
//! schema {
5452
//! query: Query
@@ -93,7 +91,6 @@
9391
//!
9492
#![warn(missing_debug_implementations)]
9593

96-
#[macro_use] extern crate failure;
9794
#[cfg(test)] #[macro_use] extern crate pretty_assertions;
9895

9996

src/query/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use combine::easy::Errors;
2+
use thiserror::Error;
23

34
use crate::tokenizer::Token;
45
use crate::position::Pos;
@@ -10,8 +11,8 @@ pub type InternalError<'a> = Errors<Token<'a>, Token<'a>, Pos>;
1011
///
1112
/// This structure is opaque for forward compatibility. We are exploring a
1213
/// way to improve both error message and API.
13-
#[derive(Fail, Debug)]
14-
#[fail(display="query parse error: {}", _0)]
14+
#[derive(Error, Debug)]
15+
#[error("query parse error: {}", _0)]
1516
pub struct ParseError(String);
1617

1718
impl<'a> From<InternalError<'a>> for ParseError {

src/schema/ast.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::str::FromStr;
22

3+
use thiserror::Error;
4+
35
pub use crate::common::{Directive, Type, Value, Text};
46
use crate::position::Pos;
57

68
#[derive(Debug, Clone, Default, PartialEq)]
7-
pub struct Document<'a, T: Text<'a>>
9+
pub struct Document<'a, T: Text<'a>>
810
where T: Text<'a>
911
{
1012
pub definitions: Vec<Definition<'a, T>>,
@@ -55,7 +57,7 @@ pub struct ScalarType<'a, T: Text<'a>> {
5557
pub directives: Vec<Directive<'a, T>>,
5658
}
5759

58-
impl<'a, T> ScalarType<'a, T>
60+
impl<'a, T> ScalarType<'a, T>
5961
where T: Text<'a>
6062
{
6163
pub fn new(name: T::Value) -> Self {
@@ -75,7 +77,7 @@ pub struct ScalarTypeExtension<'a, T: Text<'a>> {
7577
pub directives: Vec<Directive<'a, T>>,
7678
}
7779

78-
impl<'a, T> ScalarTypeExtension<'a, T>
80+
impl<'a, T> ScalarTypeExtension<'a, T>
7981
where T: Text<'a>
8082
{
8183
pub fn new(name: T::Value) -> Self {
@@ -97,7 +99,7 @@ pub struct ObjectType<'a, T: Text<'a>> {
9799
pub fields: Vec<Field<'a, T>>,
98100
}
99101

100-
impl<'a, T> ObjectType<'a, T>
102+
impl<'a, T> ObjectType<'a, T>
101103
where T: Text<'a>
102104
{
103105
pub fn new(name: T::Value) -> Self {
@@ -121,7 +123,7 @@ pub struct ObjectTypeExtension<'a, T: Text<'a>> {
121123
pub fields: Vec<Field<'a, T>>,
122124
}
123125

124-
impl<'a, T> ObjectTypeExtension<'a, T>
126+
impl<'a, T> ObjectTypeExtension<'a, T>
125127
where T: Text<'a>
126128
{
127129
pub fn new(name: T::Value) -> Self {
@@ -164,7 +166,7 @@ pub struct InterfaceType<'a, T: Text<'a>> {
164166
pub fields: Vec<Field<'a, T>>,
165167
}
166168

167-
impl<'a, T> InterfaceType<'a, T>
169+
impl<'a, T> InterfaceType<'a, T>
168170
where T: Text<'a>
169171
{
170172
pub fn new(name: T::Value) -> Self {
@@ -186,7 +188,7 @@ pub struct InterfaceTypeExtension<'a, T: Text<'a>> {
186188
pub fields: Vec<Field<'a, T>>,
187189
}
188190

189-
impl<'a, T> InterfaceTypeExtension<'a, T>
191+
impl<'a, T> InterfaceTypeExtension<'a, T>
190192
where T: Text<'a>
191193
{
192194
pub fn new(name: T::Value) -> Self {
@@ -208,7 +210,7 @@ pub struct UnionType<'a, T: Text<'a>> {
208210
pub types: Vec<T::Value>,
209211
}
210212

211-
impl<'a, T> UnionType<'a, T>
213+
impl<'a, T> UnionType<'a, T>
212214
where T: Text<'a>
213215
{
214216
pub fn new(name: T::Value) -> Self {
@@ -230,7 +232,7 @@ pub struct UnionTypeExtension<'a, T: Text<'a>> {
230232
pub types: Vec<T::Value>,
231233
}
232234

233-
impl<'a, T> UnionTypeExtension<'a, T>
235+
impl<'a, T> UnionTypeExtension<'a, T>
234236
where T: Text<'a>
235237
{
236238
pub fn new(name: T::Value) -> Self {
@@ -252,7 +254,7 @@ pub struct EnumType<'a, T: Text<'a>> {
252254
pub values: Vec<EnumValue<'a, T>>,
253255
}
254256

255-
impl<'a, T> EnumType<'a, T>
257+
impl<'a, T> EnumType<'a, T>
256258
where T: Text<'a>
257259
{
258260
pub fn new(name: T::Value) -> Self {
@@ -274,7 +276,7 @@ pub struct EnumValue<'a, T: Text<'a>> {
274276
pub directives: Vec<Directive<'a, T>>,
275277
}
276278

277-
impl<'a, T> EnumValue<'a, T>
279+
impl<'a, T> EnumValue<'a, T>
278280
where T: Text<'a>
279281
{
280282
pub fn new(name: T::Value) -> Self {
@@ -295,7 +297,7 @@ pub struct EnumTypeExtension<'a, T: Text<'a>> {
295297
pub values: Vec<EnumValue<'a, T>>,
296298
}
297299

298-
impl<'a, T> EnumTypeExtension<'a, T>
300+
impl<'a, T> EnumTypeExtension<'a, T>
299301
where T: Text<'a>
300302
{
301303
pub fn new(name: T::Value) -> Self {
@@ -317,7 +319,7 @@ pub struct InputObjectType<'a, T: Text<'a>> {
317319
pub fields: Vec<InputValue<'a, T>>,
318320
}
319321

320-
impl<'a, T> InputObjectType<'a, T>
322+
impl<'a, T> InputObjectType<'a, T>
321323
where T: Text<'a>
322324
{
323325
pub fn new(name: T::Value) -> Self {
@@ -339,7 +341,7 @@ pub struct InputObjectTypeExtension<'a, T: Text<'a>> {
339341
pub fields: Vec<InputValue<'a, T>>,
340342
}
341343

342-
impl<'a, T> InputObjectTypeExtension<'a, T>
344+
impl<'a, T> InputObjectTypeExtension<'a, T>
343345
where T: Text<'a>
344346
{
345347
pub fn new(name: T::Value) -> Self {
@@ -386,7 +388,7 @@ pub struct DirectiveDefinition<'a, T: Text<'a>> {
386388
pub locations: Vec<DirectiveLocation>,
387389
}
388390

389-
impl<'a, T> DirectiveDefinition<'a, T>
391+
impl<'a, T> DirectiveDefinition<'a, T>
390392
where T: Text<'a>
391393
{
392394
pub fn new(name: T::Value) -> Self {
@@ -460,8 +462,8 @@ impl DirectiveLocation {
460462
}
461463
}
462464

463-
#[derive(Debug, Fail)]
464-
#[fail(display = "invalid directive location")]
465+
#[derive(Debug, Error)]
466+
#[error("invalid directive location")]
465467
pub struct InvalidDirectiveLocation;
466468

467469

src/schema/error.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use combine::easy::Errors;
2+
use thiserror::Error;
23

34
use crate::tokenizer::Token;
45
use crate::position::Pos;
@@ -10,8 +11,8 @@ pub type InternalError<'a> = Errors<Token<'a>, Token<'a>, Pos>;
1011
///
1112
/// This structure is opaque for forward compatibility. We are exploring a
1213
/// way to improve both error message and API.
13-
#[derive(Fail, Debug)]
14-
#[fail(display="schema parse error: {}", _0)]
14+
#[derive(Error, Debug)]
15+
#[error("schema parse error: {}", _0)]
1516
pub struct ParseError(String);
1617

1718
impl<'a> From<InternalError<'a>> for ParseError {

src/schema/grammar.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use combine::easy::{Error, Errors};
33
use combine::error::StreamError;
44
use combine::combinator::{many, many1, eof, optional, position, choice};
55
use combine::combinator::{sep_by1};
6-
use failure::Fail;
76

87
use crate::tokenizer::{Kind as T, Token, TokenStream};
98
use crate::helpers::{punct, ident, kind, name};
@@ -460,8 +459,7 @@ pub fn directive_locations<'a>(input: &mut TokenStream<'a>)
460459
optional(punct("|"))
461460
.with(sep_by1(
462461
kind(T::Name)
463-
.and_then(|tok| tok.value.parse::<DirectiveLocation>()
464-
.map_err(|e| e.compat())),
462+
.and_then(|tok| tok.value.parse::<DirectiveLocation>()),
465463
punct("|")))
466464
)
467465
.map(|opt| opt.unwrap_or_else(Vec::new))
@@ -558,7 +556,7 @@ pub fn definition<'a, T>(input: &mut TokenStream<'a>)
558556
}
559557

560558
/// Parses a piece of schema language and returns an AST
561-
pub fn parse_schema<'a, T>(s: &'a str) -> Result<Document<'a, T>, ParseError>
559+
pub fn parse_schema<'a, T>(s: &'a str) -> Result<Document<'a, T>, ParseError>
562560
where T: Text<'a>,
563561
{
564562
let mut tokens = TokenStream::new(s);

0 commit comments

Comments
 (0)