Skip to content

Commit

Permalink
Add a prefer_single_line_descriptions option on SDLExportOptions (#955)
Browse files Browse the repository at this point in the history
* Add a prefer_single_line_descriptions option on SDLExportOptions

The default export uses three lines for each description. Single-line
descriptions improve the readability of the exported SDL when there are
many short descriptions.

* export_sdl: escape " in single-line descriptions

I confirmed that graphql-code-generator properly handles SDL with this escaping.
  • Loading branch information
ivan committed Jun 25, 2022
1 parent 44c7a3c commit 2d79376
Showing 1 changed file with 46 additions and 20 deletions.
66 changes: 46 additions & 20 deletions src/registry/export_sdl.rs
Expand Up @@ -12,6 +12,7 @@ pub struct SDLExportOptions {
sorted_arguments: bool,
sorted_enum_values: bool,
federation: bool,
prefer_single_line_descriptions: bool,
}

impl SDLExportOptions {
Expand Down Expand Up @@ -60,6 +61,16 @@ impl SDLExportOptions {
..self
}
}

/// When possible, write one-line instead of three-line descriptions
#[inline]
#[must_use]
pub fn prefer_single_line_descriptions(self) -> Self {
Self {
prefer_single_line_descriptions: true,
..self
}
}
}

impl Registry {
Expand Down Expand Up @@ -124,13 +135,8 @@ impl Registry {
continue;
}

if field.description.is_some() {
writeln!(
sdl,
"\t\"\"\"\n\t{}\n\t\"\"\"",
field.description.unwrap().replace('\n', "\n\t")
)
.ok();
if let Some(description) = field.description {
export_description(sdl, options, false, description);
}

if !field.args.is_empty() {
Expand Down Expand Up @@ -180,8 +186,8 @@ impl Registry {
export_scalar = false;
}
if export_scalar {
if description.is_some() {
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
if let Some(description) = description {
export_description(sdl, options, true, description);
}
writeln!(sdl, "scalar {}", name).ok();
}
Expand Down Expand Up @@ -218,8 +224,8 @@ impl Registry {
}
}

if description.is_some() {
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
if let Some(description) = description {
export_description(sdl, options, true, description);
}

if options.federation && *extends {
Expand Down Expand Up @@ -249,8 +255,8 @@ impl Registry {
description,
..
} => {
if description.is_some() {
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
if let Some(description) = description {
export_description(sdl, options, true, description);
}

if options.federation && *extends {
Expand All @@ -277,8 +283,8 @@ impl Registry {
description,
..
} => {
if description.is_some() {
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
if let Some(description) = description {
export_description(sdl, options, true, description);
}

write!(sdl, "enum {} ", name).ok();
Expand All @@ -304,8 +310,8 @@ impl Registry {
oneof,
..
} => {
if description.is_some() {
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
if let Some(description) = description {
export_description(sdl, options, true, description);
}

write!(sdl, "input {} ", name).ok();
Expand All @@ -322,7 +328,7 @@ impl Registry {

for field in fields {
if let Some(description) = field.description {
writeln!(sdl, "\t\"\"\"\n\t{}\n\t\"\"\"", description).ok();
export_description(sdl, options, false, description);
}
writeln!(sdl, "\t{}", export_input_value(&field)).ok();
}
Expand All @@ -335,8 +341,8 @@ impl Registry {
description,
..
} => {
if description.is_some() {
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description.unwrap()).ok();
if let Some(description) = description {
export_description(sdl, options, true, description);
}

write!(sdl, "union {} =", name).ok();
Expand Down Expand Up @@ -370,6 +376,26 @@ impl Registry {
}
}

fn export_description(
sdl: &mut String,
options: &SDLExportOptions,
top_level: bool,
description: &str,
) {
if options.prefer_single_line_descriptions && !description.contains('\n') {
let tab = if top_level { "" } else { "\t" };
let description = description.replace('"', r#"\""#);
writeln!(sdl, "{}\"{}\"", tab, description).ok();
} else {
if top_level {
writeln!(sdl, "\"\"\"\n{}\n\"\"\"", description).ok();
} else {
let description = description.replace('\n', "\n\t");
writeln!(sdl, "\t\"\"\"\n\t{}\n\t\"\"\"", description).ok();
}
}
}

fn export_input_value(input_value: &MetaInputValue) -> String {
if let Some(default_value) = &input_value.default_value {
format!(
Expand Down

0 comments on commit 2d79376

Please sign in to comment.