From 1d5c8fc65e96caa230e4952607af425579120e7a Mon Sep 17 00:00:00 2001 From: Casper Meijn Date: Mon, 9 Dec 2024 06:53:21 +0100 Subject: [PATCH 1/2] ci: Add formatting check - Run `cargo fmt` for rust code formatting - Run `taplo fmt` for toml formatting --- .github/workflows/ci.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4425dc6..e502937 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,3 +19,25 @@ jobs: - name: Test Rust run: cargo test + + rustfmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Cache Rust + uses: Swatinem/rust-cache@v1 + + - run: cargo fmt --all --check + + toml_validation: + runs-on: ubuntu-latest + container: + image: tamasfe/taplo:0.8.1 + steps: + - name: Checkout sources + uses: actions/checkout@v4 + - name: taplo lint + run: taplo lint + - name: taplo fmt + run: taplo fmt --check --diff From 6e94fee3f816e42ac97539cf99ad8720d171bbd3 Mon Sep 17 00:00:00 2001 From: Casper Meijn Date: Mon, 9 Dec 2024 06:58:54 +0100 Subject: [PATCH 2/2] style: Apply rust formatting Run `cargo fmt` --- src/format.rs | 12 ++++--- src/lib.rs | 15 +++++---- src/query/ast.rs | 4 +-- src/query/error.rs | 3 +- src/query/format.rs | 57 +++++++++++--------------------- src/query/grammar.rs | 15 +++------ src/query/mod.rs | 4 +-- src/schema/ast.rs | 79 ++++++++++++++++++++------------------------ src/schema/error.rs | 3 +- src/schema/mod.rs | 2 +- 10 files changed, 82 insertions(+), 112 deletions(-) diff --git a/src/format.rs b/src/format.rs index 0983391..de24163 100644 --- a/src/format.rs +++ b/src/format.rs @@ -3,7 +3,6 @@ use std::default::Default; use crate::common::Directive; - #[derive(Debug, PartialEq)] pub(crate) struct Formatter<'a> { buf: String, @@ -171,13 +170,16 @@ impl<'a> Formatter<'a> { } fn dec_indent(&mut self) { - self.indent = self.indent.checked_sub(self.style.indent) + self.indent = self + .indent + .checked_sub(self.style.indent) .expect("negative indent"); } } -pub(crate) fn format_directives<'a, T>(dirs: &[Directive<'a, T>], f: &mut Formatter) - where T: crate::common::Text<'a>, +pub(crate) fn format_directives<'a, T>(dirs: &[Directive<'a, T>], f: &mut Formatter) +where + T: crate::common::Text<'a>, { for dir in dirs { f.write(" "); @@ -198,7 +200,7 @@ macro_rules! impl_display { ('a $($typ: ident, )+) => { $( - impl<'a, T> fmt::Display for $typ<'a, T> + impl<'a, T> fmt::Display for $typ<'a, T> where T: Text<'a>, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/src/lib.rs b/src/lib.rs index 01a767e..561cf66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,20 +91,21 @@ //! #![warn(missing_debug_implementations)] -#[cfg(test)] #[macro_use] extern crate pretty_assertions; - +#[cfg(test)] +#[macro_use] +extern crate pretty_assertions; mod common; #[macro_use] mod format; -mod position; -mod tokenizer; mod helpers; +mod position; pub mod query; pub mod schema; +mod tokenizer; +pub use crate::format::Style; +pub use crate::position::Pos; +pub use crate::query::minify_query; pub use crate::query::parse_query; pub use crate::schema::parse_schema; -pub use crate::query::minify_query; -pub use crate::position::Pos; -pub use crate::format::Style; diff --git a/src/query/ast.rs b/src/query/ast.rs index af5be1d..1be1c7f 100644 --- a/src/query/ast.rs +++ b/src/query/ast.rs @@ -5,8 +5,8 @@ //! //! [graphql grammar]: http://facebook.github.io/graphql/October2016/#sec-Appendix-Grammar-Summary //! +pub use crate::common::{Directive, Number, Text, Type, Value}; use crate::position::Pos; -pub use crate::common::{Directive, Number, Value, Text, Type}; /// Root of query data #[derive(Debug, Clone, PartialEq)] @@ -17,7 +17,7 @@ pub struct Document<'a, T: Text<'a>> { impl<'a> Document<'a, String> { pub fn into_static(self) -> Document<'static, String> { // To support both reference and owned values in the AST, - // all string data is represented with the ::common::Str<'a, T: Text<'a>> + // all string data is represented with the ::common::Str<'a, T: Text<'a>> // wrapper type. // This type must carry the lifetime of the query string, // and is stored in a PhantomData value on the Str type. diff --git a/src/query/error.rs b/src/query/error.rs index 6907e55..4a6b662 100644 --- a/src/query/error.rs +++ b/src/query/error.rs @@ -1,12 +1,11 @@ use combine::easy::Errors; use thiserror::Error; -use crate::tokenizer::Token; use crate::position::Pos; +use crate::tokenizer::Token; pub type InternalError<'a> = Errors, Token<'a>, Pos>; - /// Error parsing query /// /// This structure is opaque for forward compatibility. We are exploring a diff --git a/src/query/format.rs b/src/query/format.rs index cca0c9a..872dd3d 100644 --- a/src/query/format.rs +++ b/src/query/format.rs @@ -4,8 +4,7 @@ use crate::format::{format_directives, Displayable, Formatter, Style}; use crate::query::ast::*; -impl<'a, T: Text<'a>> Document<'a, T> -{ +impl<'a, T: Text<'a>> Document<'a, T> { /// Format a document according to style pub fn format(&self, style: &Style) -> String { let mut formatter = Formatter::new(style); @@ -21,8 +20,7 @@ fn to_string(v: &T) -> String { formatter.into_string() } -impl<'a, T: Text<'a>> Displayable for Document<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Document<'a, T> { fn display(&self, f: &mut Formatter) { for item in &self.definitions { item.display(f); @@ -30,8 +28,7 @@ impl<'a, T: Text<'a>> Displayable for Document<'a, T> } } -impl<'a, T: Text<'a>> Displayable for Definition<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Definition<'a, T> { fn display(&self, f: &mut Formatter) { match *self { Definition::Operation(ref op) => op.display(f), @@ -40,8 +37,7 @@ impl<'a, T: Text<'a>> Displayable for Definition<'a, T> } } -impl<'a, T: Text<'a>> Displayable for OperationDefinition<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for OperationDefinition<'a, T> { fn display(&self, f: &mut Formatter) { match *self { OperationDefinition::SelectionSet(ref set) => set.display(f), @@ -52,8 +48,7 @@ impl<'a, T: Text<'a>> Displayable for OperationDefinition<'a, T> } } -impl<'a, T: Text<'a>> Displayable for FragmentDefinition<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for FragmentDefinition<'a, T> { fn display(&self, f: &mut Formatter) { f.margin(); f.indent(); @@ -71,8 +66,7 @@ impl<'a, T: Text<'a>> Displayable for FragmentDefinition<'a, T> } } -impl<'a, T: Text<'a>> Displayable for SelectionSet<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for SelectionSet<'a, T> { fn display(&self, f: &mut Formatter) { f.margin(); f.indent(); @@ -84,8 +78,7 @@ impl<'a, T: Text<'a>> Displayable for SelectionSet<'a, T> } } -impl<'a, T: Text<'a>> Displayable for Selection<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Selection<'a, T> { fn display(&self, f: &mut Formatter) { match *self { Selection::Field(ref fld) => fld.display(f), @@ -95,8 +88,7 @@ impl<'a, T: Text<'a>> Displayable for Selection<'a, T> } } -fn format_arguments<'a, T: Text<'a>>(arguments: &[(T::Value, Value<'a, T>)], f: &mut Formatter) -{ +fn format_arguments<'a, T: Text<'a>>(arguments: &[(T::Value, Value<'a, T>)], f: &mut Formatter) { if !arguments.is_empty() { f.start_argument_block('('); f.start_argument(); @@ -114,8 +106,7 @@ fn format_arguments<'a, T: Text<'a>>(arguments: &[(T::Value, Value<'a, T>)], f: } } -impl<'a, T: Text<'a>> Displayable for Field<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Field<'a, T> { fn display(&self, f: &mut Formatter) { f.indent(); if let Some(ref alias) = self.alias { @@ -138,8 +129,7 @@ impl<'a, T: Text<'a>> Displayable for Field<'a, T> } } -impl<'a, T: Text<'a>> Displayable for Query<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Query<'a, T> { fn display(&self, f: &mut Formatter) { f.margin(); f.indent(); @@ -167,8 +157,7 @@ impl<'a, T: Text<'a>> Displayable for Query<'a, T> } } -impl<'a, T: Text<'a>> Displayable for Mutation<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Mutation<'a, T> { fn display(&self, f: &mut Formatter) { f.margin(); f.indent(); @@ -196,8 +185,7 @@ impl<'a, T: Text<'a>> Displayable for Mutation<'a, T> } } -impl<'a, T: Text<'a>> Displayable for Subscription<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Subscription<'a, T> { fn display(&self, f: &mut Formatter) { f.margin(); f.indent(); @@ -223,8 +211,7 @@ impl<'a, T: Text<'a>> Displayable for Subscription<'a, T> } } -impl<'a, T: Text<'a>> Displayable for VariableDefinition<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for VariableDefinition<'a, T> { fn display(&self, f: &mut Formatter) { f.write("$"); f.write(self.name.as_ref()); @@ -237,8 +224,7 @@ impl<'a, T: Text<'a>> Displayable for VariableDefinition<'a, T> } } -impl<'a, T: Text<'a>> Displayable for Type<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Type<'a, T> { fn display(&self, f: &mut Formatter) { match *self { Type::NamedType(ref name) => f.write(name.as_ref()), @@ -255,8 +241,7 @@ impl<'a, T: Text<'a>> Displayable for Type<'a, T> } } -impl<'a, T: Text<'a>> Displayable for Value<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Value<'a, T> { fn display(&self, f: &mut Formatter) { match *self { Value::Variable(ref name) => { @@ -303,8 +288,7 @@ impl<'a, T: Text<'a>> Displayable for Value<'a, T> } } -impl<'a, T: Text<'a>> Displayable for InlineFragment<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for InlineFragment<'a, T> { fn display(&self, f: &mut Formatter) { f.indent(); f.write("..."); @@ -322,8 +306,7 @@ impl<'a, T: Text<'a>> Displayable for InlineFragment<'a, T> } } -impl<'a, T: Text<'a>> Displayable for TypeCondition<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for TypeCondition<'a, T> { fn display(&self, f: &mut Formatter) { match *self { TypeCondition::On(ref name) => { @@ -334,8 +317,7 @@ impl<'a, T: Text<'a>> Displayable for TypeCondition<'a, T> } } -impl<'a, T: Text<'a>> Displayable for FragmentSpread<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for FragmentSpread<'a, T> { fn display(&self, f: &mut Formatter) { f.indent(); f.write("..."); @@ -345,8 +327,7 @@ impl<'a, T: Text<'a>> Displayable for FragmentSpread<'a, T> } } -impl<'a, T: Text<'a>> Displayable for Directive<'a, T> -{ +impl<'a, T: Text<'a>> Displayable for Directive<'a, T> { fn display(&self, f: &mut Formatter) { f.write("@"); f.write(self.name.as_ref()); diff --git a/src/query/grammar.rs b/src/query/grammar.rs index 6849ee2..cf31bec 100644 --- a/src/query/grammar.rs +++ b/src/query/grammar.rs @@ -100,8 +100,7 @@ where pub fn query<'a, T: Text<'a>>( input: &mut TokenStream<'a>, -) -> StdParseResult, TokenStream<'a>> -{ +) -> StdParseResult, TokenStream<'a>> { position() .skip(ident("query")) .and(parser(operation_common)) @@ -129,8 +128,7 @@ type OperationCommon<'a, T: Text<'a>> = ( pub fn operation_common<'a, T: Text<'a>>( input: &mut TokenStream<'a>, -) -> StdParseResult, TokenStream<'a>> -{ +) -> StdParseResult, TokenStream<'a>> { optional(name::<'a, T>()) .and( optional( @@ -164,8 +162,7 @@ pub fn operation_common<'a, T: Text<'a>>( pub fn mutation<'a, T: Text<'a>>( input: &mut TokenStream<'a>, -) -> StdParseResult, TokenStream<'a>> -{ +) -> StdParseResult, TokenStream<'a>> { position() .skip(ident("mutation")) .and(parser(operation_common)) @@ -184,8 +181,7 @@ pub fn mutation<'a, T: Text<'a>>( pub fn subscription<'a, T: Text<'a>>( input: &mut TokenStream<'a>, -) -> StdParseResult, TokenStream<'a>> -{ +) -> StdParseResult, TokenStream<'a>> { position() .skip(ident("subscription")) .and(parser(operation_common)) @@ -219,8 +215,7 @@ where pub fn fragment_definition<'a, T: Text<'a>>( input: &mut TokenStream<'a>, -) -> StdParseResult, TokenStream<'a>> -{ +) -> StdParseResult, TokenStream<'a>> { ( position().skip(ident("fragment")), name::<'a, T>(), diff --git a/src/query/mod.rs b/src/query/mod.rs index c5bbfb5..bff05e0 100644 --- a/src/query/mod.rs +++ b/src/query/mod.rs @@ -6,7 +6,7 @@ mod format; mod grammar; mod minify; -pub use self::grammar::{parse_query, consume_definition}; -pub use self::error::ParseError; pub use self::ast::*; +pub use self::error::ParseError; +pub use self::grammar::{consume_definition, parse_query}; pub use self::minify::minify_query; diff --git a/src/schema/ast.rs b/src/schema/ast.rs index 732b191..db2908f 100644 --- a/src/schema/ast.rs +++ b/src/schema/ast.rs @@ -2,12 +2,11 @@ use std::str::FromStr; use thiserror::Error; -pub use crate::common::{Directive, Type, Value, Text}; +pub use crate::common::{Directive, Text, Type, Value}; use crate::position::Pos; #[derive(Debug, Clone, Default, PartialEq)] -pub struct Document<'a, T: Text<'a>> -{ +pub struct Document<'a, T: Text<'a>> { pub definitions: Vec>, } @@ -30,7 +29,6 @@ impl<'a> Document<'a, String> { } } - #[derive(Debug, Clone, PartialEq)] pub enum Definition<'a, T: Text<'a>> { SchemaDefinition(SchemaDefinition<'a, T>), @@ -77,7 +75,8 @@ pub struct ScalarType<'a, T: Text<'a>> { } impl<'a, T> ScalarType<'a, T> - where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -97,7 +96,8 @@ pub struct ScalarTypeExtension<'a, T: Text<'a>> { } impl<'a, T> ScalarTypeExtension<'a, T> - where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -119,7 +119,8 @@ pub struct ObjectType<'a, T: Text<'a>> { } impl<'a, T> ObjectType<'a, T> - where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -143,7 +144,8 @@ pub struct ObjectTypeExtension<'a, T: Text<'a>> { } impl<'a, T> ObjectTypeExtension<'a, T> - where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -187,7 +189,8 @@ pub struct InterfaceType<'a, T: Text<'a>> { } impl<'a, T> InterfaceType<'a, T> - where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -211,7 +214,8 @@ pub struct InterfaceTypeExtension<'a, T: Text<'a>> { } impl<'a, T> InterfaceTypeExtension<'a, T> -where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -234,7 +238,8 @@ pub struct UnionType<'a, T: Text<'a>> { } impl<'a, T> UnionType<'a, T> -where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -256,7 +261,8 @@ pub struct UnionTypeExtension<'a, T: Text<'a>> { } impl<'a, T> UnionTypeExtension<'a, T> -where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -278,7 +284,8 @@ pub struct EnumType<'a, T: Text<'a>> { } impl<'a, T> EnumType<'a, T> -where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -300,7 +307,8 @@ pub struct EnumValue<'a, T: Text<'a>> { } impl<'a, T> EnumValue<'a, T> -where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -321,7 +329,8 @@ pub struct EnumTypeExtension<'a, T: Text<'a>> { } impl<'a, T> EnumTypeExtension<'a, T> -where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -343,7 +352,8 @@ pub struct InputObjectType<'a, T: Text<'a>> { } impl<'a, T> InputObjectType<'a, T> -where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -365,7 +375,8 @@ pub struct InputObjectTypeExtension<'a, T: Text<'a>> { } impl<'a, T> InputObjectTypeExtension<'a, T> -where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -414,7 +425,8 @@ pub struct DirectiveDefinition<'a, T: Text<'a>> { } impl<'a, T> DirectiveDefinition<'a, T> -where T: Text<'a> +where + T: Text<'a>, { pub fn new(name: T::Value) -> Self { Self { @@ -459,28 +471,11 @@ impl DirectiveLocation { pub fn is_query(&self) -> bool { use self::DirectiveLocation::*; match *self { - Query - | Mutation - | Subscription - | Field - | FragmentDefinition - | FragmentSpread - | InlineFragment - => true, - - Schema - | Scalar - | Object - | FieldDefinition - | ArgumentDefinition - | Interface - | Union - | Enum - | EnumValue - | InputObject - | InputFieldDefinition - | VariableDefinition - => false, + Query | Mutation | Subscription | Field | FragmentDefinition | FragmentSpread + | InlineFragment => true, + + Schema | Scalar | Object | FieldDefinition | ArgumentDefinition | Interface | Union + | Enum | EnumValue | InputObject | InputFieldDefinition | VariableDefinition => false, } } @@ -494,11 +489,9 @@ impl DirectiveLocation { #[error("invalid directive location")] pub struct InvalidDirectiveLocation; - impl FromStr for DirectiveLocation { type Err = InvalidDirectiveLocation; - fn from_str(s: &str) -> Result - { + fn from_str(s: &str) -> Result { use self::DirectiveLocation::*; let val = match s { "QUERY" => Query, diff --git a/src/schema/error.rs b/src/schema/error.rs index 53ad541..3c19b57 100644 --- a/src/schema/error.rs +++ b/src/schema/error.rs @@ -1,12 +1,11 @@ use combine::easy::Errors; use thiserror::Error; -use crate::tokenizer::Token; use crate::position::Pos; +use crate::tokenizer::Token; pub type InternalError<'a> = Errors, Token<'a>, Pos>; - /// Error parsing schema /// /// This structure is opaque for forward compatibility. We are exploring a diff --git a/src/schema/mod.rs b/src/schema/mod.rs index 49d5e9d..80716d2 100644 --- a/src/schema/mod.rs +++ b/src/schema/mod.rs @@ -1,9 +1,9 @@ //! Schema definition language AST and utility //! mod ast; -mod grammar; mod error; mod format; +mod grammar; pub use self::ast::*; pub use self::error::ParseError;