From d1efe9f5011584523d08bc188323ff0a217428b8 Mon Sep 17 00:00:00 2001 From: Ivan Kozik Date: Mon, 20 Jun 2022 09:29:24 +0000 Subject: [PATCH 1/2] 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. --- src/registry/export_sdl.rs | 65 ++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/src/registry/export_sdl.rs b/src/registry/export_sdl.rs index 5d9a8b858..6e39b56d1 100644 --- a/src/registry/export_sdl.rs +++ b/src/registry/export_sdl.rs @@ -12,6 +12,7 @@ pub struct SDLExportOptions { sorted_arguments: bool, sorted_enum_values: bool, federation: bool, + prefer_single_line_descriptions: bool, } impl SDLExportOptions { @@ -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 { @@ -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() { @@ -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(); } @@ -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 { @@ -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 { @@ -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(); @@ -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(); @@ -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(); } @@ -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(); @@ -366,6 +372,25 @@ 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" }; + 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!( From c17b12ed414f0ac5fc3c0076dd0eb9a385e28d8e Mon Sep 17 00:00:00 2001 From: Ivan Kozik Date: Mon, 20 Jun 2022 12:20:34 +0000 Subject: [PATCH 2/2] export_sdl: escape " in single-line descriptions I confirmed that graphql-code-generator properly handles SDL with this escaping. --- src/registry/export_sdl.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/registry/export_sdl.rs b/src/registry/export_sdl.rs index 6e39b56d1..ba60b343c 100644 --- a/src/registry/export_sdl.rs +++ b/src/registry/export_sdl.rs @@ -380,6 +380,7 @@ fn export_description( ) { 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 {