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

Add a prefer_single_line_descriptions option on SDLExportOptions #955

Merged
merged 2 commits into from Jun 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -366,6 +372,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