From ea3c425f0417288f098b30e12703c457f39a99ee Mon Sep 17 00:00:00 2001 From: Atul Bhosale Date: Tue, 23 Jan 2018 01:08:48 +0530 Subject: [PATCH] Replace try! with ? --- juniper/src/ast.rs | 14 +- juniper/src/executor/mod.rs | 3 +- .../introspection/input_object.rs | 12 +- juniper/src/executor_tests/variables.rs | 3 +- juniper/src/http/mod.rs | 19 +-- juniper/src/integrations/serde.rs | 52 +++---- juniper/src/lib.rs | 2 +- juniper/src/parser/document.rs | 134 +++++++++--------- juniper/src/parser/lexer.rs | 66 ++++----- juniper/src/parser/parser.rs | 18 +-- juniper/src/parser/value.rs | 34 +++-- juniper/src/schema/meta.rs | 99 ++++++++----- juniper/src/schema/model.rs | 15 +- juniper/src/types/name.rs | 2 +- 14 files changed, 260 insertions(+), 213 deletions(-) diff --git a/juniper/src/ast.rs b/juniper/src/ast.rs index 8d5c0ae9..45547ff4 100644 --- a/juniper/src/ast.rs +++ b/juniper/src/ast.rs @@ -432,25 +432,25 @@ impl fmt::Display for InputValue { InputValue::Enum(ref v) => write!(f, "{}", v), InputValue::Variable(ref v) => write!(f, "${}", v), InputValue::List(ref v) => { - try!(write!(f, "[")); + write!(f, "[")?; for (i, spanning) in v.iter().enumerate() { - try!(spanning.item.fmt(f)); + spanning.item.fmt(f)?; if i < v.len() - 1 { - try!(write!(f, ", ")); + write!(f, ", ")?; } } write!(f, "]") } InputValue::Object(ref o) => { - try!(write!(f, "{{")); + write!(f, "{{")?; for (i, &(ref k, ref v)) in o.iter().enumerate() { - try!(write!(f, "{}: ", k.item)); - try!(v.item.fmt(f)); + write!(f, "{}: ", k.item)?; + v.item.fmt(f)?; if i < o.len() - 1 { - try!(write!(f, ", ")); + write!(f, ", ")?; } } diff --git a/juniper/src/executor/mod.rs b/juniper/src/executor/mod.rs index d2001572..85d45913 100644 --- a/juniper/src/executor/mod.rs +++ b/juniper/src/executor/mod.rs @@ -220,7 +220,8 @@ impl<'a, T: GraphQLType, C> IntoResolvable<'a, T, C> for FieldResult<(&'a T::Con } impl<'a, T: GraphQLType, C> IntoResolvable<'a, Option, C> - for FieldResult> { + for FieldResult> +{ fn into(self, _: &'a C) -> FieldResult)>> { self.map(|o| o.map(|(ctx, v)| (ctx, Some(v)))) } diff --git a/juniper/src/executor_tests/introspection/input_object.rs b/juniper/src/executor_tests/introspection/input_object.rs index 211fc761..f72d0b5c 100644 --- a/juniper/src/executor_tests/introspection/input_object.rs +++ b/juniper/src/executor_tests/introspection/input_object.rs @@ -68,15 +68,19 @@ pub struct NamedPublic { #[derive(GraphQLInputObject, Debug)] #[graphql(_internal)] struct FieldDescription { - #[graphql(description = "The first field")] field_one: String, - #[graphql(description = "The second field")] field_two: String, + #[graphql(description = "The first field")] + field_one: String, + #[graphql(description = "The second field")] + field_two: String, } #[derive(GraphQLInputObject, Debug)] #[graphql(_internal)] struct FieldWithDefaults { - #[graphql(default = "123")] field_one: i32, - #[graphql(default = "456", description = "The second field")] field_two: i32, + #[graphql(default = "123")] + field_one: i32, + #[graphql(default = "456", description = "The second field")] + field_two: i32, } graphql_object!(Root: () |&self| { diff --git a/juniper/src/executor_tests/variables.rs b/juniper/src/executor_tests/variables.rs index e385cfd8..b67a1ed2 100644 --- a/juniper/src/executor_tests/variables.rs +++ b/juniper/src/executor_tests/variables.rs @@ -56,7 +56,8 @@ struct ExampleInputObject { #[derive(GraphQLInputObject, Debug)] #[graphql(_internal)] struct InputWithDefaults { - #[graphql(default = "123")] a: i32, + #[graphql(default = "123")] + a: i32, } graphql_object!(TestType: () |&self| { diff --git a/juniper/src/http/mod.rs b/juniper/src/http/mod.rs index 03ad2d22..ac975c34 100644 --- a/juniper/src/http/mod.rs +++ b/juniper/src/http/mod.rs @@ -19,7 +19,8 @@ use executor::ExecutionError; #[derive(Deserialize, Clone, Serialize, PartialEq, Debug)] pub struct GraphQLRequest { query: String, - #[serde(rename = "operationName")] operation_name: Option, + #[serde(rename = "operationName")] + operation_name: Option, variables: Option, } @@ -101,22 +102,22 @@ impl<'a> ser::Serialize for GraphQLResponse<'a> { { match self.0 { Ok((ref res, ref err)) => { - let mut map = try!(serializer.serialize_map(None)); + let mut map = serializer.serialize_map(None)?; - try!(map.serialize_key("data")); - try!(map.serialize_value(res)); + map.serialize_key("data")?; + map.serialize_value(res)?; if !err.is_empty() { - try!(map.serialize_key("errors")); - try!(map.serialize_value(err)); + map.serialize_key("errors")?; + map.serialize_value(err)?; } map.end() } Err(ref err) => { - let mut map = try!(serializer.serialize_map(Some(1))); - try!(map.serialize_key("errors")); - try!(map.serialize_value(err)); + let mut map = serializer.serialize_map(Some(1))?; + map.serialize_key("errors")?; + map.serialize_value(err)?; map.end() } } diff --git a/juniper/src/integrations/serde.rs b/juniper/src/integrations/serde.rs index 896c7fa3..55d85d8c 100644 --- a/juniper/src/integrations/serde.rs +++ b/juniper/src/integrations/serde.rs @@ -15,21 +15,21 @@ impl ser::Serialize for ExecutionError { where S: ser::Serializer, { - let mut map = try!(serializer.serialize_map(Some(4))); + let mut map = serializer.serialize_map(Some(4))?; - try!(map.serialize_key("message")); - try!(map.serialize_value(self.error().message())); + map.serialize_key("message")?; + map.serialize_value(self.error().message())?; let locations = vec![self.location()]; - try!(map.serialize_key("locations")); - try!(map.serialize_value(&locations)); + map.serialize_key("locations")?; + map.serialize_value(&locations)?; - try!(map.serialize_key("path")); - try!(map.serialize_value(self.path())); + map.serialize_key("path")?; + map.serialize_value(self.path())?; if !self.error().data().is_null() { - try!(map.serialize_key("data")); - try!(map.serialize_value(self.error().data())); + map.serialize_key("data")?; + map.serialize_value(self.error().data())?; } map.end() @@ -123,7 +123,7 @@ impl<'de> de::Deserialize<'de> for InputValue { { let mut values = Vec::new(); - while let Some(el) = try!(visitor.next_element()) { + while let Some(el) = visitor.next_element()? { values.push(el); } @@ -136,7 +136,7 @@ impl<'de> de::Deserialize<'de> for InputValue { { let mut values: IndexMap = IndexMap::new(); - while let Some((key, value)) = try!(visitor.next_entry()) { + while let Some((key, value)) = visitor.next_entry()? { values.insert(key, value); } @@ -176,13 +176,13 @@ impl ser::Serialize for RuleError { where S: ser::Serializer, { - let mut map = try!(serializer.serialize_map(Some(2))); + let mut map = serializer.serialize_map(Some(2))?; - try!(map.serialize_key("message")); - try!(map.serialize_value(self.message())); + map.serialize_key("message")?; + map.serialize_value(self.message())?; - try!(map.serialize_key("locations")); - try!(map.serialize_value(self.locations())); + map.serialize_key("locations")?; + map.serialize_value(self.locations())?; map.end() } @@ -193,15 +193,15 @@ impl ser::Serialize for SourcePosition { where S: ser::Serializer, { - let mut map = try!(serializer.serialize_map(Some(2))); + let mut map = serializer.serialize_map(Some(2))?; let line = self.line() + 1; - try!(map.serialize_key("line")); - try!(map.serialize_value(&line)); + map.serialize_key("line")?; + map.serialize_value(&line)?; let column = self.column() + 1; - try!(map.serialize_key("column")); - try!(map.serialize_value(&column)); + map.serialize_key("column")?; + map.serialize_value(&column)?; map.end() } @@ -212,11 +212,11 @@ impl<'a> ser::Serialize for Spanning> { where S: ser::Serializer, { - let mut map = try!(serializer.serialize_map(Some(2))); + let mut map = serializer.serialize_map(Some(2))?; let message = format!("{}", self.item); - try!(map.serialize_key("message")); - try!(map.serialize_value(&message)); + map.serialize_key("message")?; + map.serialize_value(&message)?; let mut location = IndexMap::new(); location.insert("line".to_owned(), self.start.line() + 1); @@ -224,8 +224,8 @@ impl<'a> ser::Serialize for Spanning> { let locations = vec![location]; - try!(map.serialize_key("locations")); - try!(map.serialize_value(&locations)); + map.serialize_key("locations")?; + map.serialize_value(&locations)?; map.end() } diff --git a/juniper/src/lib.rs b/juniper/src/lib.rs index 8398dd72..10ed9bdd 100644 --- a/juniper/src/lib.rs +++ b/juniper/src/lib.rs @@ -184,7 +184,7 @@ where QueryT: GraphQLType, MutationT: GraphQLType, { - let document = try!(parse_document_source(document_source)); + let document = parse_document_source(document_source)?; { let errors = validate_input_values(variables, &document, &root_node.schema); diff --git a/juniper/src/parser/document.rs b/juniper/src/parser/document.rs index d26e2e48..9e47cc72 100644 --- a/juniper/src/parser/document.rs +++ b/juniper/src/parser/document.rs @@ -11,7 +11,7 @@ use parser::value::parse_value_literal; #[doc(hidden)] pub fn parse_document_source(s: &str) -> UnlocatedParseResult { let mut lexer = Lexer::new(s); - let mut parser = try!(Parser::new(&mut lexer).map_err(|s| s.map(ParseError::LexerError))); + let mut parser = Parser::new(&mut lexer).map_err(|s| s.map(ParseError::LexerError))?; parse_document(&mut parser) } @@ -19,7 +19,7 @@ fn parse_document<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Docum let mut defs = Vec::new(); loop { - defs.push(try!(parse_definition(parser))); + defs.push(parse_definition(parser)?); if parser.peek().item == Token::EndOfFile { return Ok(defs); @@ -29,19 +29,17 @@ fn parse_document<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Docum fn parse_definition<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Definition<'a>> { match parser.peek().item { - Token::CurlyOpen | Token::Name("query") | Token::Name("mutation") => Ok( - Definition::Operation(try!(parse_operation_definition(parser))), - ), - Token::Name("fragment") => Ok(Definition::Fragment(try!(parse_fragment_definition( - parser - )))), + Token::CurlyOpen | Token::Name("query") | Token::Name("mutation") => { + Ok(Definition::Operation(parse_operation_definition(parser)?)) + } + Token::Name("fragment") => Ok(Definition::Fragment(parse_fragment_definition(parser)?)), _ => Err(parser.next()?.map(ParseError::UnexpectedToken)), } } fn parse_operation_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Operation<'a>> { if parser.peek().item == Token::CurlyOpen { - let selection_set = try!(parse_selection_set(parser)); + let selection_set = parse_selection_set(parser)?; Ok(Spanning::start_end( &selection_set.start, @@ -56,14 +54,14 @@ fn parse_operation_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Op )) } else { let start_pos = parser.peek().start.clone(); - let operation_type = try!(parse_operation_type(parser)); + let operation_type = parse_operation_type(parser)?; let name = match parser.peek().item { - Token::Name(_) => Some(try!(parser.expect_name())), + Token::Name(_) => Some(parser.expect_name()?), _ => None, }; - let variable_definitions = try!(parse_variable_definitions(parser)); - let directives = try!(parse_directives(parser)); - let selection_set = try!(parse_selection_set(parser)); + let variable_definitions = parse_variable_definitions(parser)?; + let directives = parse_directives(parser)?; + let selection_set = parse_selection_set(parser)?; Ok(Spanning::start_end( &start_pos, @@ -82,7 +80,7 @@ fn parse_operation_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Op fn parse_fragment_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Fragment<'a>> { let Spanning { start: start_pos, .. - } = try!(parser.expect(&Token::Name("fragment"))); + } = parser.expect(&Token::Name("fragment"))?; let name = match parser.expect_name() { Ok(n) => if n.item == "on" { return Err(n.map(|_| ParseError::UnexpectedToken(Token::Name("on")))); @@ -92,10 +90,10 @@ fn parse_fragment_definition<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Fra Err(e) => return Err(e), }; - try!(parser.expect(&Token::Name("on"))); - let type_cond = try!(parser.expect_name()); - let directives = try!(parse_directives(parser)); - let selection_set = try!(parse_selection_set(parser)); + parser.expect(&Token::Name("on"))?; + let type_cond = parser.expect_name()?; + let directives = parse_directives(parser)?; + let selection_set = parse_selection_set(parser)?; Ok(Spanning::start_end( &start_pos, @@ -113,7 +111,7 @@ fn parse_optional_selection_set<'a>( parser: &mut Parser<'a>, ) -> OptionParseResult<'a, Vec>> { if parser.peek().item == Token::CurlyOpen { - Ok(Some(try!(parse_selection_set(parser)))) + Ok(Some(parse_selection_set(parser)?)) } else { Ok(None) } @@ -134,14 +132,14 @@ fn parse_fragment<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Selec let Spanning { start: ref start_pos, .. - } = try!(parser.expect(&Token::Ellipsis)); + } = parser.expect(&Token::Ellipsis)?; match parser.peek().item { Token::Name("on") => { parser.next()?; - let name = try!(parser.expect_name()); - let directives = try!(parse_directives(parser)); - let selection_set = try!(parse_selection_set(parser)); + let name = parser.expect_name()?; + let directives = parse_directives(parser)?; + let selection_set = parse_selection_set(parser)?; Ok(Selection::InlineFragment(Spanning::start_end( &start_pos.clone(), @@ -154,7 +152,7 @@ fn parse_fragment<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Selec ))) } Token::CurlyOpen => { - let selection_set = try!(parse_selection_set(parser)); + let selection_set = parse_selection_set(parser)?; Ok(Selection::InlineFragment(Spanning::start_end( &start_pos.clone(), @@ -167,8 +165,8 @@ fn parse_fragment<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Selec ))) } Token::Name(_) => { - let frag_name = try!(parser.expect_name()); - let directives = try!(parse_directives(parser)); + let frag_name = parser.expect_name()?; + let directives = parse_directives(parser)?; Ok(Selection::FragmentSpread(Spanning::start_end( &start_pos.clone(), @@ -183,8 +181,8 @@ fn parse_fragment<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Selec ))) } Token::At => { - let directives = try!(parse_directives(parser)); - let selection_set = try!(parse_selection_set(parser)); + let directives = parse_directives(parser)?; + let selection_set = parse_selection_set(parser)?; Ok(Selection::InlineFragment(Spanning::start_end( &start_pos.clone(), @@ -201,17 +199,17 @@ fn parse_fragment<'a>(parser: &mut Parser<'a>) -> UnlocatedParseResult<'a, Selec } fn parse_field<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Field<'a>> { - let mut alias = Some(try!(parser.expect_name())); + let mut alias = Some(parser.expect_name()?); - let name = if try!(parser.skip(&Token::Colon)).is_some() { - try!(parser.expect_name()) + let name = if parser.skip(&Token::Colon)?.is_some() { + parser.expect_name()? } else { alias.take().unwrap() }; - let arguments = try!(parse_arguments(parser)); - let directives = try!(parse_directives(parser)); - let selection_set = try!(parse_optional_selection_set(parser)); + let arguments = parse_arguments(parser)?; + let directives = parse_directives(parser)?; + let selection_set = parse_optional_selection_set(parser)?; Ok(Spanning::start_end( &alias.as_ref().unwrap_or(&name).start.clone(), @@ -237,13 +235,11 @@ fn parse_arguments<'a>(parser: &mut Parser<'a>) -> OptionParseResult<'a, Argumen Ok(None) } else { Ok(Some( - try!(parser.delimited_nonempty_list( - &Token::ParenOpen, - parse_argument, - &Token::ParenClose - )).map(|args| Arguments { - items: args.into_iter().map(|s| s.item).collect(), - }), + parser + .delimited_nonempty_list(&Token::ParenOpen, parse_argument, &Token::ParenClose)? + .map(|args| Arguments { + items: args.into_iter().map(|s| s.item).collect(), + }), )) } } @@ -251,9 +247,9 @@ fn parse_arguments<'a>(parser: &mut Parser<'a>) -> OptionParseResult<'a, Argumen fn parse_argument<'a>( parser: &mut Parser<'a>, ) -> ParseResult<'a, (Spanning<&'a str>, Spanning)> { - let name = try!(parser.expect_name()); - try!(parser.expect(&Token::Colon)); - let value = try!(parse_value_literal(parser, false)); + let name = parser.expect_name()?; + parser.expect(&Token::Colon)?; + let value = parse_value_literal(parser, false)?; Ok(Spanning::start_end( &name.start.clone(), @@ -277,13 +273,15 @@ fn parse_variable_definitions<'a>( Ok(None) } else { Ok(Some( - try!(parser.delimited_nonempty_list( - &Token::ParenOpen, - parse_variable_definition, - &Token::ParenClose - )).map(|defs| VariableDefinitions { - items: defs.into_iter().map(|s| s.item).collect(), - }), + parser + .delimited_nonempty_list( + &Token::ParenOpen, + parse_variable_definition, + &Token::ParenClose, + )? + .map(|defs| VariableDefinitions { + items: defs.into_iter().map(|s| s.item).collect(), + }), )) } } @@ -293,13 +291,13 @@ fn parse_variable_definition<'a>( ) -> ParseResult<'a, (Spanning<&'a str>, VariableDefinition<'a>)> { let Spanning { start: start_pos, .. - } = try!(parser.expect(&Token::Dollar)); - let var_name = try!(parser.expect_name()); - try!(parser.expect(&Token::Colon)); - let var_type = try!(parse_type(parser)); + } = parser.expect(&Token::Dollar)?; + let var_name = parser.expect_name()?; + parser.expect(&Token::Colon)?; + let var_type = parse_type(parser)?; - let default_value = if try!(parser.skip(&Token::Equals)).is_some() { - Some(try!(parse_value_literal(parser, true))) + let default_value = if parser.skip(&Token::Equals)?.is_some() { + Some(parse_value_literal(parser, true)?) } else { None }; @@ -328,7 +326,7 @@ fn parse_directives<'a>( } else { let mut items = Vec::new(); while parser.peek().item == Token::At { - items.push(try!(parse_directive(parser))); + items.push(parse_directive(parser)?); } Ok(Spanning::spanning(items)) @@ -338,9 +336,9 @@ fn parse_directives<'a>( fn parse_directive<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Directive<'a>> { let Spanning { start: start_pos, .. - } = try!(parser.expect(&Token::At)); - let name = try!(parser.expect_name()); - let arguments = try!(parse_arguments(parser)); + } = parser.expect(&Token::At)?; + let name = parser.expect_name()?; + let arguments = parse_arguments(parser)?; Ok(Spanning::start_end( &start_pos, @@ -355,20 +353,20 @@ fn parse_directive<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Directive<'a> pub fn parse_type<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, Type<'a>> { let parsed_type = if let Some(Spanning { start: start_pos, .. - }) = try!(parser.skip(&Token::BracketOpen)) + }) = parser.skip(&Token::BracketOpen)? { - let inner_type = try!(parse_type(parser)); - let Spanning { end: end_pos, .. } = try!(parser.expect(&Token::BracketClose)); + let inner_type = parse_type(parser)?; + let Spanning { end: end_pos, .. } = parser.expect(&Token::BracketClose)?; Spanning::start_end(&start_pos, &end_pos, Type::List(Box::new(inner_type.item))) } else { - try!(parser.expect_name()).map(|s| Type::Named(Cow::Borrowed(s))) + parser.expect_name()?.map(|s| Type::Named(Cow::Borrowed(s))) }; Ok(match *parser.peek() { Spanning { item: Token::ExclamationMark, .. - } => try!(wrap_non_null(parser, parsed_type)), + } => wrap_non_null(parser, parsed_type)?, _ => parsed_type, }) } @@ -377,7 +375,7 @@ fn wrap_non_null<'a>( parser: &mut Parser<'a>, inner: Spanning>, ) -> ParseResult<'a, Type<'a>> { - let Spanning { end: end_pos, .. } = try!(parser.expect(&Token::ExclamationMark)); + let Spanning { end: end_pos, .. } = parser.expect(&Token::ExclamationMark)?; let wrapped = match inner.item { Type::Named(name) => Type::NonNullNamed(name), diff --git a/juniper/src/parser/lexer.rs b/juniper/src/parser/lexer.rs index 88d06532..2c3913a2 100644 --- a/juniper/src/parser/lexer.rs +++ b/juniper/src/parser/lexer.rs @@ -160,10 +160,10 @@ impl<'a> Lexer<'a> { let start_pos = self.position.clone(); for _ in 0..3 { - let (_, ch) = try!(self.next_char().ok_or(Spanning::zero_width( + let (_, ch) = self.next_char().ok_or(Spanning::zero_width( &self.position, - LexerError::UnexpectedEndOfFile - ))); + LexerError::UnexpectedEndOfFile, + ))?; if ch != '.' { return Err(Spanning::zero_width( &start_pos, @@ -181,10 +181,10 @@ impl<'a> Lexer<'a> { fn scan_name(&mut self) -> LexerResult<'a> { let start_pos = self.position.clone(); - let (start_idx, start_ch) = try!(self.next_char().ok_or(Spanning::zero_width( + let (start_idx, start_ch) = self.next_char().ok_or(Spanning::zero_width( &self.position, - LexerError::UnexpectedEndOfFile - ))); + LexerError::UnexpectedEndOfFile, + ))?; assert!(is_name_start(start_ch)); let mut end_idx = start_idx; @@ -207,10 +207,10 @@ impl<'a> Lexer<'a> { fn scan_string(&mut self) -> LexerResult<'a> { let start_pos = self.position.clone(); - let (_, start_ch) = try!(self.next_char().ok_or(Spanning::zero_width( + let (_, start_ch) = self.next_char().ok_or(Spanning::zero_width( &self.position, - LexerError::UnexpectedEndOfFile - ))); + LexerError::UnexpectedEndOfFile, + ))?; assert!(start_ch == '"'); let mut acc = String::new(); @@ -262,7 +262,7 @@ impl<'a> Lexer<'a> { Some((_, 'u')) => { let start_pos = self.position.clone(); self.next_char(); - acc.push(try!(self.scan_escaped_unicode(&start_pos))); + acc.push(self.scan_escaped_unicode(&start_pos)?); } Some((_, ch)) => { let mut s = String::from("\\"); @@ -314,18 +314,18 @@ impl<'a> Lexer<'a> { &mut self, start_pos: &SourcePosition, ) -> Result> { - let (start_idx, _) = try!(self.peek_char().ok_or(Spanning::zero_width( + let (start_idx, _) = self.peek_char().ok_or(Spanning::zero_width( &self.position, - LexerError::UnterminatedString - ))); + LexerError::UnterminatedString, + ))?; let mut end_idx = start_idx; let mut len = 0; for _ in 0..4 { - let (idx, ch) = try!(self.next_char().ok_or(Spanning::zero_width( + let (idx, ch) = self.next_char().ok_or(Spanning::zero_width( &self.position, - LexerError::UnterminatedString - ))); + LexerError::UnterminatedString, + ))?; if !ch.is_alphanumeric() { break; @@ -344,12 +344,12 @@ impl<'a> Lexer<'a> { )); } - let code_point = try!( - u32::from_str_radix(escape, 16).map_err(|_| Spanning::zero_width( + let code_point = u32::from_str_radix(escape, 16).map_err(|_| { + Spanning::zero_width( start_pos, LexerError::UnknownEscapeSequence("\\u".to_owned() + escape), - )) - ); + ) + })?; char::from_u32(code_point).ok_or_else(|| { Spanning::zero_width( @@ -361,14 +361,14 @@ impl<'a> Lexer<'a> { fn scan_number(&mut self) -> LexerResult<'a> { let start_pos = self.position.clone(); - let int_part = try!(self.scan_integer_part()); + let int_part = self.scan_integer_part()?; let mut frac_part = None; let mut exp_part = None; if let Some((_, '.')) = self.peek_char() { self.next_char(); - frac_part = Some(try!(self.scan_digits())); + frac_part = Some(self.scan_digits()?); } if let Some((_, ch)) = self.peek_char() { @@ -385,7 +385,7 @@ impl<'a> Lexer<'a> { self.next_char(); } } - exp_part = Some(if is_negative { -1 } else { 1 } * try!(self.scan_digits())); + exp_part = Some(if is_negative { -1 } else { 1 } * self.scan_digits()?); } } @@ -416,10 +416,10 @@ impl<'a> Lexer<'a> { fn scan_integer_part(&mut self) -> Result> { let is_negative = { - let (_, init_ch) = try!(self.peek_char().ok_or(Spanning::zero_width( + let (_, init_ch) = self.peek_char().ok_or(Spanning::zero_width( &self.position, - LexerError::UnexpectedEndOfFile - ))); + LexerError::UnexpectedEndOfFile, + ))?; if init_ch == '-' { self.next_char(); @@ -429,10 +429,10 @@ impl<'a> Lexer<'a> { } }; - let (_, ch) = try!(self.peek_char().ok_or(Spanning::zero_width( + let (_, ch) = self.peek_char().ok_or(Spanning::zero_width( &self.position, - LexerError::UnexpectedEndOfFile - ))); + LexerError::UnexpectedEndOfFile, + ))?; if ch == '0' { self.next_char(); @@ -445,16 +445,16 @@ impl<'a> Lexer<'a> { _ => Ok(0), } } else { - Ok(try!(self.scan_digits()) * if is_negative { -1 } else { 1 }) + Ok(self.scan_digits()? * if is_negative { -1 } else { 1 }) } } fn scan_digits(&mut self) -> Result> { let start_pos = self.position.clone(); - let (start_idx, ch) = try!(self.peek_char().ok_or(Spanning::zero_width( + let (start_idx, ch) = self.peek_char().ok_or(Spanning::zero_width( &self.position, - LexerError::UnexpectedEndOfFile - ))); + LexerError::UnexpectedEndOfFile, + ))?; let mut end_idx = start_idx; if !ch.is_digit(10) { diff --git a/juniper/src/parser/parser.rs b/juniper/src/parser/parser.rs index c700e2ca..e85b5e3a 100644 --- a/juniper/src/parser/parser.rs +++ b/juniper/src/parser/parser.rs @@ -103,15 +103,15 @@ impl<'a> Parser<'a> { { let Spanning { start: start_pos, .. - } = try!(self.expect(opening)); + } = self.expect(opening)?; let mut items = Vec::new(); loop { - if let Some(Spanning { end: end_pos, .. }) = try!(self.skip(closing)) { + if let Some(Spanning { end: end_pos, .. }) = self.skip(closing)? { return Ok(Spanning::start_end(&start_pos, &end_pos, items)); } - items.push(try!(parser(self))); + items.push(parser(self)?); } } @@ -128,13 +128,13 @@ impl<'a> Parser<'a> { { let Spanning { start: start_pos, .. - } = try!(self.expect(opening)); + } = self.expect(opening)?; let mut items = Vec::new(); loop { - items.push(try!(parser(self))); + items.push(parser(self)?); - if let Some(Spanning { end: end_pos, .. }) = try!(self.skip(closing)) { + if let Some(Spanning { end: end_pos, .. }) = self.skip(closing)? { return Ok(Spanning::start_end(&start_pos, &end_pos, items)); } } @@ -153,13 +153,13 @@ impl<'a> Parser<'a> { { let Spanning { start: start_pos, .. - } = try!(self.expect(opening)); + } = self.expect(opening)?; let mut items = Vec::new(); loop { - items.push(try!(parser(self))); + items.push(parser(self)?); - if let Some(Spanning { end: end_pos, .. }) = try!(self.skip(closing)) { + if let Some(Spanning { end: end_pos, .. }) = self.skip(closing)? { return Ok(Spanning::start_end(&start_pos, &end_pos, items)); } } diff --git a/juniper/src/parser/value.rs b/juniper/src/parser/value.rs index 3bd5678b..016df991 100644 --- a/juniper/src/parser/value.rs +++ b/juniper/src/parser/value.rs @@ -63,33 +63,37 @@ pub fn parse_value_literal<'a>( } fn parse_list_literal<'a>(parser: &mut Parser<'a>, is_const: bool) -> ParseResult<'a, InputValue> { - Ok(try!(parser.delimited_list( - &Token::BracketOpen, - |p| parse_value_literal(p, is_const), - &Token::BracketClose - )).map(InputValue::parsed_list)) + Ok(parser + .delimited_list( + &Token::BracketOpen, + |p| parse_value_literal(p, is_const), + &Token::BracketClose, + )? + .map(InputValue::parsed_list)) } fn parse_object_literal<'a>( parser: &mut Parser<'a>, is_const: bool, ) -> ParseResult<'a, InputValue> { - Ok(try!(parser.delimited_list( - &Token::CurlyOpen, - |p| parse_object_field(p, is_const), - &Token::CurlyClose - )).map(|items| InputValue::parsed_object(items.into_iter().map(|s| s.item).collect()))) + Ok(parser + .delimited_list( + &Token::CurlyOpen, + |p| parse_object_field(p, is_const), + &Token::CurlyClose, + )? + .map(|items| InputValue::parsed_object(items.into_iter().map(|s| s.item).collect()))) } fn parse_object_field<'a>( parser: &mut Parser<'a>, is_const: bool, ) -> ParseResult<'a, (Spanning, Spanning)> { - let key = try!(parser.expect_name()); + let key = parser.expect_name()?; - try!(parser.expect(&Token::Colon)); + parser.expect(&Token::Colon)?; - let value = try!(parse_value_literal(parser, is_const)); + let value = parse_value_literal(parser, is_const)?; Ok(Spanning::start_end( &key.start.clone(), @@ -101,12 +105,12 @@ fn parse_object_field<'a>( fn parse_variable_literal<'a>(parser: &mut Parser<'a>) -> ParseResult<'a, InputValue> { let Spanning { start: start_pos, .. - } = try!(parser.expect(&Token::Dollar)); + } = parser.expect(&Token::Dollar)?; let Spanning { item: name, end: end_pos, .. - } = try!(parser.expect_name()); + } = parser.expect_name()?; Ok(Spanning::start_end( &start_pos, diff --git a/juniper/src/schema/meta.rs b/juniper/src/schema/meta.rs index 9e8a6790..c0407ddc 100644 --- a/juniper/src/schema/meta.rs +++ b/juniper/src/schema/meta.rs @@ -8,62 +8,85 @@ use types::base::TypeKind; /// Scalar type metadata pub struct ScalarMeta<'a> { - #[doc(hidden)] pub name: Cow<'a, str>, - #[doc(hidden)] pub description: Option, - #[doc(hidden)] pub try_parse_fn: Box bool + Send + Sync>, + #[doc(hidden)] + pub name: Cow<'a, str>, + #[doc(hidden)] + pub description: Option, + #[doc(hidden)] + pub try_parse_fn: Box bool + Send + Sync>, } /// List type metadata #[derive(Debug)] pub struct ListMeta<'a> { - #[doc(hidden)] pub of_type: Type<'a>, + #[doc(hidden)] + pub of_type: Type<'a>, } /// Nullable type metadata #[derive(Debug)] pub struct NullableMeta<'a> { - #[doc(hidden)] pub of_type: Type<'a>, + #[doc(hidden)] + pub of_type: Type<'a>, } /// Object type metadata #[derive(Debug)] pub struct ObjectMeta<'a> { - #[doc(hidden)] pub name: Cow<'a, str>, - #[doc(hidden)] pub description: Option, - #[doc(hidden)] pub fields: Vec>, - #[doc(hidden)] pub interface_names: Vec, + #[doc(hidden)] + pub name: Cow<'a, str>, + #[doc(hidden)] + pub description: Option, + #[doc(hidden)] + pub fields: Vec>, + #[doc(hidden)] + pub interface_names: Vec, } /// Enum type metadata pub struct EnumMeta<'a> { - #[doc(hidden)] pub name: Cow<'a, str>, - #[doc(hidden)] pub description: Option, - #[doc(hidden)] pub values: Vec, - #[doc(hidden)] pub try_parse_fn: Box bool + Send + Sync>, + #[doc(hidden)] + pub name: Cow<'a, str>, + #[doc(hidden)] + pub description: Option, + #[doc(hidden)] + pub values: Vec, + #[doc(hidden)] + pub try_parse_fn: Box bool + Send + Sync>, } /// Interface type metadata #[derive(Debug)] pub struct InterfaceMeta<'a> { - #[doc(hidden)] pub name: Cow<'a, str>, - #[doc(hidden)] pub description: Option, - #[doc(hidden)] pub fields: Vec>, + #[doc(hidden)] + pub name: Cow<'a, str>, + #[doc(hidden)] + pub description: Option, + #[doc(hidden)] + pub fields: Vec>, } /// Union type metadata #[derive(Debug)] pub struct UnionMeta<'a> { - #[doc(hidden)] pub name: Cow<'a, str>, - #[doc(hidden)] pub description: Option, - #[doc(hidden)] pub of_type_names: Vec, + #[doc(hidden)] + pub name: Cow<'a, str>, + #[doc(hidden)] + pub description: Option, + #[doc(hidden)] + pub of_type_names: Vec, } /// Input object metadata pub struct InputObjectMeta<'a> { - #[doc(hidden)] pub name: Cow<'a, str>, - #[doc(hidden)] pub description: Option, - #[doc(hidden)] pub input_fields: Vec>, - #[doc(hidden)] pub try_parse_fn: Box bool + Send + Sync>, + #[doc(hidden)] + pub name: Cow<'a, str>, + #[doc(hidden)] + pub description: Option, + #[doc(hidden)] + pub input_fields: Vec>, + #[doc(hidden)] + pub try_parse_fn: Box bool + Send + Sync>, } /// A placeholder for not-yet-registered types @@ -72,7 +95,8 @@ pub struct InputObjectMeta<'a> { /// is inserted into a registry to indicate existence. #[derive(Debug)] pub struct PlaceholderMeta<'a> { - #[doc(hidden)] pub of_type: Type<'a>, + #[doc(hidden)] + pub of_type: Type<'a>, } /// Generic type metadata @@ -92,20 +116,29 @@ pub enum MetaType<'a> { /// Metadata for a field #[derive(Debug, Clone)] pub struct Field<'a> { - #[doc(hidden)] pub name: String, - #[doc(hidden)] pub description: Option, - #[doc(hidden)] pub arguments: Option>>, - #[doc(hidden)] pub field_type: Type<'a>, - #[doc(hidden)] pub deprecation_reason: Option, + #[doc(hidden)] + pub name: String, + #[doc(hidden)] + pub description: Option, + #[doc(hidden)] + pub arguments: Option>>, + #[doc(hidden)] + pub field_type: Type<'a>, + #[doc(hidden)] + pub deprecation_reason: Option, } /// Metadata for an argument to a field #[derive(Debug, Clone)] pub struct Argument<'a> { - #[doc(hidden)] pub name: String, - #[doc(hidden)] pub description: Option, - #[doc(hidden)] pub arg_type: Type<'a>, - #[doc(hidden)] pub default_value: Option, + #[doc(hidden)] + pub name: String, + #[doc(hidden)] + pub description: Option, + #[doc(hidden)] + pub arg_type: Type<'a>, + #[doc(hidden)] + pub default_value: Option, } /// Metadata for a single value in an enum diff --git a/juniper/src/schema/model.rs b/juniper/src/schema/model.rs index 20aa1d95..7fb57734 100644 --- a/juniper/src/schema/model.rs +++ b/juniper/src/schema/model.rs @@ -13,11 +13,16 @@ use schema::meta::{Argument, InterfaceMeta, MetaType, ObjectMeta, PlaceholderMet /// This brings the mutation and query types together, and provides the /// predefined metadata fields. pub struct RootNode<'a, QueryT: GraphQLType, MutationT: GraphQLType> { - #[doc(hidden)] pub query_type: QueryT, - #[doc(hidden)] pub query_info: QueryT::TypeInfo, - #[doc(hidden)] pub mutation_type: MutationT, - #[doc(hidden)] pub mutation_info: MutationT::TypeInfo, - #[doc(hidden)] pub schema: SchemaType<'a>, + #[doc(hidden)] + pub query_type: QueryT, + #[doc(hidden)] + pub query_info: QueryT::TypeInfo, + #[doc(hidden)] + pub mutation_type: MutationT, + #[doc(hidden)] + pub mutation_info: MutationT::TypeInfo, + #[doc(hidden)] + pub schema: SchemaType<'a>, } /// Metadata for a schema diff --git a/juniper/src/types/name.rs b/juniper/src/types/name.rs index 9208c6d2..f146814f 100644 --- a/juniper/src/types/name.rs +++ b/juniper/src/types/name.rs @@ -30,7 +30,7 @@ impl Name { } } } - return input.len() > 0; + return !input.is_empty(); } }