Skip to content

Commit

Permalink
ci: Add formatting check (#85)
Browse files Browse the repository at this point in the history
* ci: Add formatting check

- Run `cargo fmt` for rust code formatting
- Run `taplo fmt` for toml formatting

* style: Apply rust formatting

Run `cargo fmt`
  • Loading branch information
caspermeijn authored Dec 13, 2024
1 parent 381f414 commit 4eb2275
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 112 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 7 additions & 5 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::default::Default;

use crate::common::Directive;


#[derive(Debug, PartialEq)]
pub(crate) struct Formatter<'a> {
buf: String,
Expand Down Expand Up @@ -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(" ");
Expand All @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 2 additions & 2 deletions src/query/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions src/query/error.rs
Original file line number Diff line number Diff line change
@@ -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>, Token<'a>, Pos>;


/// Error parsing query
///
/// This structure is opaque for forward compatibility. We are exploring a
Expand Down
57 changes: 19 additions & 38 deletions src/query/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -21,17 +20,15 @@ fn to_string<T: Displayable>(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);
}
}
}

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),
Expand All @@ -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),
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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),
Expand All @@ -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();
Expand All @@ -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 {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand All @@ -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()),
Expand All @@ -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) => {
Expand Down Expand Up @@ -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("...");
Expand All @@ -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) => {
Expand All @@ -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("...");
Expand All @@ -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());
Expand Down
15 changes: 5 additions & 10 deletions src/query/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ where

pub fn query<'a, T: Text<'a>>(
input: &mut TokenStream<'a>,
) -> StdParseResult<Query<'a, T>, TokenStream<'a>>
{
) -> StdParseResult<Query<'a, T>, TokenStream<'a>> {
position()
.skip(ident("query"))
.and(parser(operation_common))
Expand Down Expand Up @@ -129,8 +128,7 @@ type OperationCommon<'a, T: Text<'a>> = (

pub fn operation_common<'a, T: Text<'a>>(
input: &mut TokenStream<'a>,
) -> StdParseResult<OperationCommon<'a, T>, TokenStream<'a>>
{
) -> StdParseResult<OperationCommon<'a, T>, TokenStream<'a>> {
optional(name::<'a, T>())
.and(
optional(
Expand Down Expand Up @@ -164,8 +162,7 @@ pub fn operation_common<'a, T: Text<'a>>(

pub fn mutation<'a, T: Text<'a>>(
input: &mut TokenStream<'a>,
) -> StdParseResult<Mutation<'a, T>, TokenStream<'a>>
{
) -> StdParseResult<Mutation<'a, T>, TokenStream<'a>> {
position()
.skip(ident("mutation"))
.and(parser(operation_common))
Expand All @@ -184,8 +181,7 @@ pub fn mutation<'a, T: Text<'a>>(

pub fn subscription<'a, T: Text<'a>>(
input: &mut TokenStream<'a>,
) -> StdParseResult<Subscription<'a, T>, TokenStream<'a>>
{
) -> StdParseResult<Subscription<'a, T>, TokenStream<'a>> {
position()
.skip(ident("subscription"))
.and(parser(operation_common))
Expand Down Expand Up @@ -219,8 +215,7 @@ where

pub fn fragment_definition<'a, T: Text<'a>>(
input: &mut TokenStream<'a>,
) -> StdParseResult<FragmentDefinition<'a, T>, TokenStream<'a>>
{
) -> StdParseResult<FragmentDefinition<'a, T>, TokenStream<'a>> {
(
position().skip(ident("fragment")),
name::<'a, T>(),
Expand Down
4 changes: 2 additions & 2 deletions src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading

0 comments on commit 4eb2275

Please sign in to comment.