Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sorting in cynic-parser pretty printing #1038

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cynic-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod span;
mod parser;

#[cfg(feature = "print")]
mod printing;
pub mod printing;

pub use self::{
errors::Error, executable::ExecutableDocument, span::Span, type_system::TypeSystemDocument,
Expand Down
3 changes: 3 additions & 0 deletions cynic-parser/src/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ mod indent;
#[cfg(feature = "pretty")]
mod pretty;

#[cfg(feature = "pretty")]
pub use pretty::PrettyPrinter;

fn escape_string(src: &str) -> String {
let mut dest = String::with_capacity(src.len());

Expand Down
11 changes: 11 additions & 0 deletions cynic-parser/src/printing/pretty/executable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ use crate::common::OperationType;
use crate::executable::*;
use crate::printing::escape_string;

use super::printer::PrettyOptions;

type Allocator<'a> = pretty::Arena<'a>;

impl crate::ExecutableDocument {
pub fn to_string_pretty(&self) -> String {
self.pretty_printer().to_string()
}

// TODO: Make this public at some point?
fn pretty_printer(&self) -> super::PrettyPrinter<'_> {
super::PrettyPrinter::new_executable(self)
}

pub(super) fn pretty_print(&self, _options: &PrettyOptions) -> String {
let allocator = pretty::Arena::new();

let use_short_form = {
Expand Down
4 changes: 4 additions & 0 deletions cynic-parser/src/printing/pretty/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
mod executable;
mod printer;
mod sorting;
mod type_system;

pub use printer::PrettyPrinter;
49 changes: 49 additions & 0 deletions cynic-parser/src/printing/pretty/printer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::fmt;

use crate::{ExecutableDocument, TypeSystemDocument};

// TODO: Document me?
pub struct PrettyPrinter<'a> {
document: Document<'a>,
options: PrettyOptions,
}

#[derive(Default, Clone, Copy)]
pub(super) struct PrettyOptions {
pub(super) sort: bool,
}

impl<'a> PrettyPrinter<'a> {
pub(super) fn new_type_system(document: &'a TypeSystemDocument) -> Self {
PrettyPrinter {
document: Document::TypeSystem(document),
options: Default::default(),
}
}

pub(super) fn new_executable(document: &'a ExecutableDocument) -> Self {
PrettyPrinter {
document: Document::Executable(document),
options: Default::default(),
}
}

pub fn sorted(mut self) -> Self {
self.options.sort = true;
self
}
}

impl fmt::Display for PrettyPrinter<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.document {
Document::Executable(doc) => write!(f, "{}", doc.pretty_print(&self.options)),
Document::TypeSystem(doc) => write!(f, "{}", doc.pretty_print(&self.options)),
}
}
}

enum Document<'a> {
Executable(&'a ExecutableDocument),
TypeSystem(&'a TypeSystemDocument),
}
18 changes: 18 additions & 0 deletions cynic-parser/src/printing/pretty/sorting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::type_system::Definition;

#[derive(PartialOrd, Ord, PartialEq, Eq)]
pub(super) enum DefinitionSortKey<'a> {
Directive(&'a str),
Type(&'a str, bool),
Schema(bool),
}

pub(super) fn sort_key_for<'a>(definition: &Definition<'a>) -> DefinitionSortKey<'a> {
match definition {
Definition::Schema(_) => DefinitionSortKey::Schema(false),
Definition::SchemaExtension(_) => DefinitionSortKey::Schema(true),
Definition::Type(def) => DefinitionSortKey::Type(def.name(), false),
Definition::TypeExtension(def) => DefinitionSortKey::Type(def.name(), true),
Definition::Directive(def) => DefinitionSortKey::Directive(def.name()),
}
}
Loading
Loading