Skip to content

Commit ab8d7e6

Browse files
authored
Merge pull request #5 from Keats/clippy
Clippy
2 parents fe82e17 + 960d4b4 commit ab8d7e6

File tree

5 files changed

+140
-142
lines changed

5 files changed

+140
-142
lines changed

src/format.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Formatting graphql
22
use std::default::Default;
33

4+
#[derive(Debug, PartialEq)]
45
pub(crate) struct Formatter<'a> {
56
buf: String,
67
style: &'a Style,
@@ -44,37 +45,45 @@ impl<'a> Formatter<'a> {
4445
indent: 0,
4546
}
4647
}
48+
4749
pub fn indent(&mut self) {
4850
for _ in 0..self.indent {
4951
self.buf.push(' ');
5052
}
5153
}
54+
5255
pub fn endline(&mut self) {
5356
self.buf.push('\n');
5457
}
58+
5559
pub fn start_block(&mut self) {
5660
self.buf.push('{');
5761
self.endline();
5862
self.indent += self.style.indent;
5963
}
64+
6065
pub fn end_block(&mut self) {
6166
self.indent = self.indent.checked_sub(self.style.indent)
6267
.expect("negative indent");
6368
self.indent();
6469
self.buf.push('}');
6570
self.endline();
6671
}
72+
6773
pub fn margin(&mut self) {
68-
if self.buf.len() != 0 {
74+
if !self.buf.is_empty() {
6975
self.buf.push('\n');
7076
}
7177
}
78+
7279
pub fn write(&mut self, s: &str) {
7380
self.buf.push_str(s);
7481
}
82+
7583
pub fn into_string(self) -> String {
7684
self.buf
7785
}
86+
7887
pub fn write_quoted(&mut self, s: &str) {
7988
let mut has_newline = false;
8089
let mut has_nonprintable = false;
@@ -105,7 +114,7 @@ impl<'a> Formatter<'a> {
105114
self.endline();
106115
self.indent += self.style.indent;
107116
for line in s.lines() {
108-
if line.trim().len() != 0 {
117+
if !line.trim().is_empty() {
109118
self.indent();
110119
self.write(&line.replace(r#"""""#, r#"\""""#));
111120
}

src/helpers.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ use tokenizer::{TokenStream, Kind, Token};
88
use position::Pos;
99

1010

11-
#[derive(Clone)]
11+
#[derive(Debug, Clone)]
1212
pub struct TokenMatch<'a> {
1313
kind: Kind,
1414
phantom: PhantomData<&'a u8>,
1515
}
1616

17-
#[derive(Clone)]
17+
#[derive(Debug, Clone)]
1818
pub struct NameMatch<'a> {
1919
phantom: PhantomData<&'a u8>,
2020
}
2121

22-
#[derive(Clone)]
22+
#[derive(Debug, Clone)]
2323
pub struct Value<'a> {
2424
kind: Kind,
2525
value: &'static str,
@@ -89,6 +89,7 @@ impl<'a> Parser for Value<'a> {
8989
c.kind == self.kind && c.value == self.value
9090
}).parse_lazy(input)
9191
}
92+
9293
fn add_error(&mut self,
9394
error: &mut Tracked<<Self::Input as StreamOnce>::Error>)
9495
{

src/query_format.rs

Lines changed: 36 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Displayable for Selection {
8787
}
8888

8989
fn format_arguments(arguments: &[(String, Value)], f: &mut Formatter) {
90-
if arguments.len() > 0 {
90+
if !arguments.is_empty() {
9191
f.write("(");
9292
f.write(&arguments[0].0);
9393
f.write(": ");
@@ -112,7 +112,7 @@ impl Displayable for Field {
112112
f.write(&self.name);
113113
format_arguments(&self.arguments, f);
114114
format_directives(&self.directives, f);
115-
if self.selection_set.items.len() > 0 {
115+
if !self.selection_set.items.is_empty() {
116116
f.write(" ");
117117
f.start_block();
118118
for item in &self.selection_set.items {
@@ -133,7 +133,7 @@ impl Displayable for Query {
133133
if let Some(ref name) = self.name {
134134
f.write(" ");
135135
f.write(name);
136-
if self.variable_definitions.len() > 0 {
136+
if !self.variable_definitions.is_empty() {
137137
f.write("(");
138138
self.variable_definitions[0].display(f);
139139
for var in &self.variable_definitions[1..] {
@@ -161,7 +161,7 @@ impl Displayable for Mutation {
161161
if let Some(ref name) = self.name {
162162
f.write(" ");
163163
f.write(name);
164-
if self.variable_definitions.len() > 0 {
164+
if !self.variable_definitions.is_empty() {
165165
f.write("(");
166166
for var in &self.variable_definitions {
167167
var.display(f);
@@ -187,7 +187,7 @@ impl Displayable for Subscription {
187187
if let Some(ref name) = self.name {
188188
f.write(" ");
189189
f.write(name);
190-
if self.variable_definitions.len() > 0 {
190+
if !self.variable_definitions.is_empty() {
191191
f.write("(");
192192
for var in &self.variable_definitions {
193193
var.display(f);
@@ -248,7 +248,7 @@ impl Displayable for Value {
248248
Value::Enum(ref name) => f.write(name),
249249
Value::List(ref items) => {
250250
f.write("[");
251-
if items.len() > 0 {
251+
if !items.is_empty() {
252252
items[0].display(f);
253253
for item in &items[1..] {
254254
f.write(", ");
@@ -330,88 +330,33 @@ impl Displayable for Directive {
330330
}
331331
}
332332

333-
impl fmt::Display for Document {
334-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
335-
f.write_str(&to_string(self))
336-
}
337-
}
338-
impl fmt::Display for Definition {
339-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
340-
f.write_str(&to_string(self))
341-
}
342-
}
343-
impl fmt::Display for OperationDefinition {
344-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
345-
f.write_str(&to_string(self))
346-
}
347-
}
348-
impl fmt::Display for FragmentDefinition {
349-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
350-
f.write_str(&to_string(self))
351-
}
352-
}
353-
impl fmt::Display for SelectionSet {
354-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
355-
f.write_str(&to_string(self))
356-
}
357-
}
358-
impl fmt::Display for Selection {
359-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
360-
f.write_str(&to_string(self))
361-
}
362-
}
363-
impl fmt::Display for Field {
364-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
365-
f.write_str(&to_string(self))
366-
}
367-
}
368-
impl fmt::Display for Query {
369-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
370-
f.write_str(&to_string(self))
371-
}
372-
}
373-
impl fmt::Display for Mutation {
374-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
375-
f.write_str(&to_string(self))
376-
}
377-
}
378-
impl fmt::Display for Subscription {
379-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
380-
f.write_str(&to_string(self))
381-
}
382-
}
383-
impl fmt::Display for VariableDefinition {
384-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
385-
f.write_str(&to_string(self))
386-
}
387-
}
388-
impl fmt::Display for VariableType {
389-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
390-
f.write_str(&to_string(self))
391-
}
392-
}
393-
impl fmt::Display for Value {
394-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
395-
f.write_str(&to_string(self))
396-
}
397-
}
398-
impl fmt::Display for InlineFragment {
399-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
400-
f.write_str(&to_string(self))
401-
}
402-
}
403-
impl fmt::Display for TypeCondition {
404-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
405-
f.write_str(&to_string(self))
406-
}
407-
}
408-
impl fmt::Display for FragmentSpread {
409-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
410-
f.write_str(&to_string(self))
411-
}
412-
}
413-
impl fmt::Display for Directive {
414-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
415-
f.write_str(&to_string(self))
416-
}
417-
}
333+
macro_rules! impl_display {
334+
($( $typ: ident ),+) => {
335+
$(
336+
impl fmt::Display for $typ {
337+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
338+
f.write_str(&to_string(self))
339+
}
340+
}
341+
)+
342+
};
343+
}
344+
impl_display!(
345+
Document,
346+
Definition,
347+
OperationDefinition,
348+
FragmentDefinition,
349+
SelectionSet,
350+
Field,
351+
Query,
352+
Mutation,
353+
Subscription,
354+
VariableDefinition,
355+
VariableType,
356+
Value,
357+
InlineFragment,
358+
TypeCondition,
359+
FragmentSpread,
360+
Directive
361+
);
362+

src/query_grammar.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,13 @@ pub fn variable_type<'a>(input: &mut TokenStream<'a>)
112112
.map(Box::new)
113113
.map(VariableType::ListType))
114114
.and(optional(punct("!")).map(|v| v.is_some()))
115-
.map(|(typ, strict)| match strict {
116-
true => VariableType::NonNullType(Box::new(typ)),
117-
false => typ,
118-
})
115+
.map(|(typ, strict)|
116+
if strict {
117+
VariableType::NonNullType(Box::new(typ))
118+
} else {
119+
typ
120+
}
121+
)
119122
.parse_stream(input)
120123
}
121124

@@ -137,7 +140,7 @@ pub fn float_value<'a>(input: &mut TokenStream<'a>)
137140

138141
fn unquote_string(s: &str) -> Result<String, Error<Token, Token>> {
139142
let mut res = String::with_capacity(s.len());
140-
debug_assert!(s.starts_with("\"") && s.ends_with("\""));
143+
debug_assert!(s.starts_with('"') && s.ends_with('"'));
141144
let mut chars = s[1..s.len()-1].chars();
142145
while let Some(c) = chars.next() {
143146
match c {
@@ -161,7 +164,8 @@ fn unquote_string(s: &str) -> Result<String, Error<Token, Token>> {
161164
c => res.push(c),
162165
}
163166
}
164-
return Ok(res);
167+
168+
Ok(res)
165169
}
166170

167171
fn unquote_block_string(src: &str) -> Result<String, Error<Token, Token>> {
@@ -180,7 +184,7 @@ fn unquote_block_string(src: &str) -> Result<String, Error<Token, Token>> {
180184
let mut lines = src[3..src.len()-3].lines();
181185
if let Some(first) = lines.next() {
182186
let stripped = first.trim();
183-
if stripped.len() > 0 {
187+
if !stripped.is_empty() {
184188
result.push_str(stripped);
185189
result.push('\n');
186190
}
@@ -193,10 +197,11 @@ fn unquote_block_string(src: &str) -> Result<String, Error<Token, Token>> {
193197
}
194198
result.push('\n');
195199
}
196-
if result[last_line..].trim().len() == 0 {
200+
if result[last_line..].trim().is_empty() {
197201
result.truncate(last_line);
198202
}
199-
return Ok(result);
203+
204+
Ok(result)
200205
}
201206

202207
pub fn string_value<'a>(input: &mut TokenStream<'a>)
@@ -269,11 +274,16 @@ pub fn query<'a>(input: &mut TokenStream<'a>)
269274
.parse_stream(input)
270275
}
271276

277+
/// A set of attributes common to a Query and a Mutation
278+
type OperationCommon = (
279+
Option<String>,
280+
Vec<VariableDefinition>,
281+
Vec<Directive>,
282+
SelectionSet,
283+
);
284+
272285
pub fn operation_common<'a>(input: &mut TokenStream<'a>)
273-
-> ParseResult<
274-
(Option<String>, Vec<VariableDefinition>, Vec<Directive>,
275-
SelectionSet),
276-
TokenStream<'a>>
286+
-> ParseResult<OperationCommon, TokenStream<'a>>
277287
{
278288
optional(name())
279289
.and(optional(
@@ -368,6 +378,7 @@ pub fn parse_query(s: &str) -> Result<Document, QueryParseError> {
368378
.skip(eof())
369379
.parse_stream(&mut tokens)
370380
.map_err(|e| e.into_inner().error)?;
381+
371382
Ok(doc)
372383
}
373384

0 commit comments

Comments
 (0)