Skip to content

Commit 4eb2275

Browse files
authored
ci: Add formatting check (#85)
* ci: Add formatting check - Run `cargo fmt` for rust code formatting - Run `taplo fmt` for toml formatting * style: Apply rust formatting Run `cargo fmt`
1 parent 381f414 commit 4eb2275

File tree

11 files changed

+104
-112
lines changed

11 files changed

+104
-112
lines changed

.github/workflows/ci.yml

+22
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,25 @@ jobs:
1919

2020
- name: Test Rust
2121
run: cargo test
22+
23+
rustfmt:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- uses: actions/checkout@v2
27+
28+
- name: Cache Rust
29+
uses: Swatinem/rust-cache@v1
30+
31+
- run: cargo fmt --all --check
32+
33+
toml_validation:
34+
runs-on: ubuntu-latest
35+
container:
36+
image: tamasfe/taplo:0.8.1
37+
steps:
38+
- name: Checkout sources
39+
uses: actions/checkout@v4
40+
- name: taplo lint
41+
run: taplo lint
42+
- name: taplo fmt
43+
run: taplo fmt --check --diff

src/format.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::default::Default;
33

44
use crate::common::Directive;
55

6-
76
#[derive(Debug, PartialEq)]
87
pub(crate) struct Formatter<'a> {
98
buf: String,
@@ -171,13 +170,16 @@ impl<'a> Formatter<'a> {
171170
}
172171

173172
fn dec_indent(&mut self) {
174-
self.indent = self.indent.checked_sub(self.style.indent)
173+
self.indent = self
174+
.indent
175+
.checked_sub(self.style.indent)
175176
.expect("negative indent");
176177
}
177178
}
178179

179-
pub(crate) fn format_directives<'a, T>(dirs: &[Directive<'a, T>], f: &mut Formatter)
180-
where T: crate::common::Text<'a>,
180+
pub(crate) fn format_directives<'a, T>(dirs: &[Directive<'a, T>], f: &mut Formatter)
181+
where
182+
T: crate::common::Text<'a>,
181183
{
182184
for dir in dirs {
183185
f.write(" ");
@@ -198,7 +200,7 @@ macro_rules! impl_display {
198200

199201
('a $($typ: ident, )+) => {
200202
$(
201-
impl<'a, T> fmt::Display for $typ<'a, T>
203+
impl<'a, T> fmt::Display for $typ<'a, T>
202204
where T: Text<'a>,
203205
{
204206
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/lib.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,21 @@
9191
//!
9292
#![warn(missing_debug_implementations)]
9393

94-
#[cfg(test)] #[macro_use] extern crate pretty_assertions;
95-
94+
#[cfg(test)]
95+
#[macro_use]
96+
extern crate pretty_assertions;
9697

9798
mod common;
9899
#[macro_use]
99100
mod format;
100-
mod position;
101-
mod tokenizer;
102101
mod helpers;
102+
mod position;
103103
pub mod query;
104104
pub mod schema;
105+
mod tokenizer;
105106

107+
pub use crate::format::Style;
108+
pub use crate::position::Pos;
109+
pub use crate::query::minify_query;
106110
pub use crate::query::parse_query;
107111
pub use crate::schema::parse_schema;
108-
pub use crate::query::minify_query;
109-
pub use crate::position::Pos;
110-
pub use crate::format::Style;

src/query/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//!
66
//! [graphql grammar]: http://facebook.github.io/graphql/October2016/#sec-Appendix-Grammar-Summary
77
//!
8+
pub use crate::common::{Directive, Number, Text, Type, Value};
89
use crate::position::Pos;
9-
pub use crate::common::{Directive, Number, Value, Text, Type};
1010

1111
/// Root of query data
1212
#[derive(Debug, Clone, PartialEq)]
@@ -17,7 +17,7 @@ pub struct Document<'a, T: Text<'a>> {
1717
impl<'a> Document<'a, String> {
1818
pub fn into_static(self) -> Document<'static, String> {
1919
// To support both reference and owned values in the AST,
20-
// all string data is represented with the ::common::Str<'a, T: Text<'a>>
20+
// all string data is represented with the ::common::Str<'a, T: Text<'a>>
2121
// wrapper type.
2222
// This type must carry the lifetime of the query string,
2323
// and is stored in a PhantomData value on the Str type.

src/query/error.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use combine::easy::Errors;
22
use thiserror::Error;
33

4-
use crate::tokenizer::Token;
54
use crate::position::Pos;
5+
use crate::tokenizer::Token;
66

77
pub type InternalError<'a> = Errors<Token<'a>, Token<'a>, Pos>;
88

9-
109
/// Error parsing query
1110
///
1211
/// This structure is opaque for forward compatibility. We are exploring a

src/query/format.rs

+19-38
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use crate::format::{format_directives, Displayable, Formatter, Style};
44

55
use crate::query::ast::*;
66

7-
impl<'a, T: Text<'a>> Document<'a, T>
8-
{
7+
impl<'a, T: Text<'a>> Document<'a, T> {
98
/// Format a document according to style
109
pub fn format(&self, style: &Style) -> String {
1110
let mut formatter = Formatter::new(style);
@@ -21,17 +20,15 @@ fn to_string<T: Displayable>(v: &T) -> String {
2120
formatter.into_string()
2221
}
2322

24-
impl<'a, T: Text<'a>> Displayable for Document<'a, T>
25-
{
23+
impl<'a, T: Text<'a>> Displayable for Document<'a, T> {
2624
fn display(&self, f: &mut Formatter) {
2725
for item in &self.definitions {
2826
item.display(f);
2927
}
3028
}
3129
}
3230

33-
impl<'a, T: Text<'a>> Displayable for Definition<'a, T>
34-
{
31+
impl<'a, T: Text<'a>> Displayable for Definition<'a, T> {
3532
fn display(&self, f: &mut Formatter) {
3633
match *self {
3734
Definition::Operation(ref op) => op.display(f),
@@ -40,8 +37,7 @@ impl<'a, T: Text<'a>> Displayable for Definition<'a, T>
4037
}
4138
}
4239

43-
impl<'a, T: Text<'a>> Displayable for OperationDefinition<'a, T>
44-
{
40+
impl<'a, T: Text<'a>> Displayable for OperationDefinition<'a, T> {
4541
fn display(&self, f: &mut Formatter) {
4642
match *self {
4743
OperationDefinition::SelectionSet(ref set) => set.display(f),
@@ -52,8 +48,7 @@ impl<'a, T: Text<'a>> Displayable for OperationDefinition<'a, T>
5248
}
5349
}
5450

55-
impl<'a, T: Text<'a>> Displayable for FragmentDefinition<'a, T>
56-
{
51+
impl<'a, T: Text<'a>> Displayable for FragmentDefinition<'a, T> {
5752
fn display(&self, f: &mut Formatter) {
5853
f.margin();
5954
f.indent();
@@ -71,8 +66,7 @@ impl<'a, T: Text<'a>> Displayable for FragmentDefinition<'a, T>
7166
}
7267
}
7368

74-
impl<'a, T: Text<'a>> Displayable for SelectionSet<'a, T>
75-
{
69+
impl<'a, T: Text<'a>> Displayable for SelectionSet<'a, T> {
7670
fn display(&self, f: &mut Formatter) {
7771
f.margin();
7872
f.indent();
@@ -84,8 +78,7 @@ impl<'a, T: Text<'a>> Displayable for SelectionSet<'a, T>
8478
}
8579
}
8680

87-
impl<'a, T: Text<'a>> Displayable for Selection<'a, T>
88-
{
81+
impl<'a, T: Text<'a>> Displayable for Selection<'a, T> {
8982
fn display(&self, f: &mut Formatter) {
9083
match *self {
9184
Selection::Field(ref fld) => fld.display(f),
@@ -95,8 +88,7 @@ impl<'a, T: Text<'a>> Displayable for Selection<'a, T>
9588
}
9689
}
9790

98-
fn format_arguments<'a, T: Text<'a>>(arguments: &[(T::Value, Value<'a, T>)], f: &mut Formatter)
99-
{
91+
fn format_arguments<'a, T: Text<'a>>(arguments: &[(T::Value, Value<'a, T>)], f: &mut Formatter) {
10092
if !arguments.is_empty() {
10193
f.start_argument_block('(');
10294
f.start_argument();
@@ -114,8 +106,7 @@ fn format_arguments<'a, T: Text<'a>>(arguments: &[(T::Value, Value<'a, T>)], f:
114106
}
115107
}
116108

117-
impl<'a, T: Text<'a>> Displayable for Field<'a, T>
118-
{
109+
impl<'a, T: Text<'a>> Displayable for Field<'a, T> {
119110
fn display(&self, f: &mut Formatter) {
120111
f.indent();
121112
if let Some(ref alias) = self.alias {
@@ -138,8 +129,7 @@ impl<'a, T: Text<'a>> Displayable for Field<'a, T>
138129
}
139130
}
140131

141-
impl<'a, T: Text<'a>> Displayable for Query<'a, T>
142-
{
132+
impl<'a, T: Text<'a>> Displayable for Query<'a, T> {
143133
fn display(&self, f: &mut Formatter) {
144134
f.margin();
145135
f.indent();
@@ -167,8 +157,7 @@ impl<'a, T: Text<'a>> Displayable for Query<'a, T>
167157
}
168158
}
169159

170-
impl<'a, T: Text<'a>> Displayable for Mutation<'a, T>
171-
{
160+
impl<'a, T: Text<'a>> Displayable for Mutation<'a, T> {
172161
fn display(&self, f: &mut Formatter) {
173162
f.margin();
174163
f.indent();
@@ -196,8 +185,7 @@ impl<'a, T: Text<'a>> Displayable for Mutation<'a, T>
196185
}
197186
}
198187

199-
impl<'a, T: Text<'a>> Displayable for Subscription<'a, T>
200-
{
188+
impl<'a, T: Text<'a>> Displayable for Subscription<'a, T> {
201189
fn display(&self, f: &mut Formatter) {
202190
f.margin();
203191
f.indent();
@@ -223,8 +211,7 @@ impl<'a, T: Text<'a>> Displayable for Subscription<'a, T>
223211
}
224212
}
225213

226-
impl<'a, T: Text<'a>> Displayable for VariableDefinition<'a, T>
227-
{
214+
impl<'a, T: Text<'a>> Displayable for VariableDefinition<'a, T> {
228215
fn display(&self, f: &mut Formatter) {
229216
f.write("$");
230217
f.write(self.name.as_ref());
@@ -237,8 +224,7 @@ impl<'a, T: Text<'a>> Displayable for VariableDefinition<'a, T>
237224
}
238225
}
239226

240-
impl<'a, T: Text<'a>> Displayable for Type<'a, T>
241-
{
227+
impl<'a, T: Text<'a>> Displayable for Type<'a, T> {
242228
fn display(&self, f: &mut Formatter) {
243229
match *self {
244230
Type::NamedType(ref name) => f.write(name.as_ref()),
@@ -255,8 +241,7 @@ impl<'a, T: Text<'a>> Displayable for Type<'a, T>
255241
}
256242
}
257243

258-
impl<'a, T: Text<'a>> Displayable for Value<'a, T>
259-
{
244+
impl<'a, T: Text<'a>> Displayable for Value<'a, T> {
260245
fn display(&self, f: &mut Formatter) {
261246
match *self {
262247
Value::Variable(ref name) => {
@@ -303,8 +288,7 @@ impl<'a, T: Text<'a>> Displayable for Value<'a, T>
303288
}
304289
}
305290

306-
impl<'a, T: Text<'a>> Displayable for InlineFragment<'a, T>
307-
{
291+
impl<'a, T: Text<'a>> Displayable for InlineFragment<'a, T> {
308292
fn display(&self, f: &mut Formatter) {
309293
f.indent();
310294
f.write("...");
@@ -322,8 +306,7 @@ impl<'a, T: Text<'a>> Displayable for InlineFragment<'a, T>
322306
}
323307
}
324308

325-
impl<'a, T: Text<'a>> Displayable for TypeCondition<'a, T>
326-
{
309+
impl<'a, T: Text<'a>> Displayable for TypeCondition<'a, T> {
327310
fn display(&self, f: &mut Formatter) {
328311
match *self {
329312
TypeCondition::On(ref name) => {
@@ -334,8 +317,7 @@ impl<'a, T: Text<'a>> Displayable for TypeCondition<'a, T>
334317
}
335318
}
336319

337-
impl<'a, T: Text<'a>> Displayable for FragmentSpread<'a, T>
338-
{
320+
impl<'a, T: Text<'a>> Displayable for FragmentSpread<'a, T> {
339321
fn display(&self, f: &mut Formatter) {
340322
f.indent();
341323
f.write("...");
@@ -345,8 +327,7 @@ impl<'a, T: Text<'a>> Displayable for FragmentSpread<'a, T>
345327
}
346328
}
347329

348-
impl<'a, T: Text<'a>> Displayable for Directive<'a, T>
349-
{
330+
impl<'a, T: Text<'a>> Displayable for Directive<'a, T> {
350331
fn display(&self, f: &mut Formatter) {
351332
f.write("@");
352333
f.write(self.name.as_ref());

src/query/grammar.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ where
100100

101101
pub fn query<'a, T: Text<'a>>(
102102
input: &mut TokenStream<'a>,
103-
) -> StdParseResult<Query<'a, T>, TokenStream<'a>>
104-
{
103+
) -> StdParseResult<Query<'a, T>, TokenStream<'a>> {
105104
position()
106105
.skip(ident("query"))
107106
.and(parser(operation_common))
@@ -129,8 +128,7 @@ type OperationCommon<'a, T: Text<'a>> = (
129128

130129
pub fn operation_common<'a, T: Text<'a>>(
131130
input: &mut TokenStream<'a>,
132-
) -> StdParseResult<OperationCommon<'a, T>, TokenStream<'a>>
133-
{
131+
) -> StdParseResult<OperationCommon<'a, T>, TokenStream<'a>> {
134132
optional(name::<'a, T>())
135133
.and(
136134
optional(
@@ -164,8 +162,7 @@ pub fn operation_common<'a, T: Text<'a>>(
164162

165163
pub fn mutation<'a, T: Text<'a>>(
166164
input: &mut TokenStream<'a>,
167-
) -> StdParseResult<Mutation<'a, T>, TokenStream<'a>>
168-
{
165+
) -> StdParseResult<Mutation<'a, T>, TokenStream<'a>> {
169166
position()
170167
.skip(ident("mutation"))
171168
.and(parser(operation_common))
@@ -184,8 +181,7 @@ pub fn mutation<'a, T: Text<'a>>(
184181

185182
pub fn subscription<'a, T: Text<'a>>(
186183
input: &mut TokenStream<'a>,
187-
) -> StdParseResult<Subscription<'a, T>, TokenStream<'a>>
188-
{
184+
) -> StdParseResult<Subscription<'a, T>, TokenStream<'a>> {
189185
position()
190186
.skip(ident("subscription"))
191187
.and(parser(operation_common))
@@ -219,8 +215,7 @@ where
219215

220216
pub fn fragment_definition<'a, T: Text<'a>>(
221217
input: &mut TokenStream<'a>,
222-
) -> StdParseResult<FragmentDefinition<'a, T>, TokenStream<'a>>
223-
{
218+
) -> StdParseResult<FragmentDefinition<'a, T>, TokenStream<'a>> {
224219
(
225220
position().skip(ident("fragment")),
226221
name::<'a, T>(),

src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod format;
66
mod grammar;
77
mod minify;
88

9-
pub use self::grammar::{parse_query, consume_definition};
10-
pub use self::error::ParseError;
119
pub use self::ast::*;
10+
pub use self::error::ParseError;
11+
pub use self::grammar::{consume_definition, parse_query};
1212
pub use self::minify::minify_query;

0 commit comments

Comments
 (0)