Skip to content

Allow to disable trailing commas #24

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

Merged
merged 2 commits into from
Sep 28, 2022
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
10 changes: 4 additions & 6 deletions doku-derive/src/expand/expand_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,10 @@ impl Field {
self.example = quote! {
Some(::doku::Example::Literal(#literal_example))
};
} else {
if !examples.is_empty() {
self.example = quote! {
Some(::doku::Example::from(&[#(#examples,)*][..]))
};
}
} else if !examples.is_empty() {
self.example = quote! {
Some(::doku::Example::from(&[#(#examples,)*][..]))
};
}

if !metas.is_empty() {
Expand Down
8 changes: 4 additions & 4 deletions doku-derive/src/expand/expand_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn expand_fields(fields: &syn::Fields) -> Result<TokenStream2> {
match fields {
syn::Fields::Named(inner) => expand_named_fields(inner),
syn::Fields::Unnamed(inner) => expand_unnamed_fields(inner),
syn::Fields::Unit => expand_unit_fields(),
syn::Fields::Unit => Ok(expand_unit_fields()),
}
}

Expand Down Expand Up @@ -36,8 +36,8 @@ fn expand_unnamed_fields(fields: &syn::FieldsUnnamed) -> Result<TokenStream2> {
})
}

fn expand_unit_fields() -> Result<TokenStream2> {
Ok(quote! {
fn expand_unit_fields() -> TokenStream2 {
quote! {
::doku::Fields::Unit
})
}
}
46 changes: 44 additions & 2 deletions doku/src/printers/json/formatting/objects_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct ObjectsStyle {
/// }
///
/// let fmt_no_quotes = doku::json::Formatting {
/// objects_style: doku::json::ObjectsStyle { surround_keys_with_quotes: false },
/// objects_style: doku::json::ObjectsStyle { surround_keys_with_quotes: false, ..Default::default() },
/// ..Default::default()
/// };
///
Expand All @@ -28,7 +28,7 @@ pub struct ObjectsStyle {
/// "#, doc);
///
/// let fmt_quotes = doku::json::Formatting {
/// objects_style: doku::json::ObjectsStyle { surround_keys_with_quotes: true },
/// objects_style: doku::json::ObjectsStyle { surround_keys_with_quotes: true, ..Default::default() },
/// ..Default::default()
/// };
///
Expand All @@ -41,12 +41,54 @@ pub struct ObjectsStyle {
/// "#, doc);
/// ```
pub surround_keys_with_quotes: bool,

/// Whether to use comma to separate properties
///
/// ```
/// use doku::Document;
///
/// #[derive(Document)]
/// struct Person {
/// name: String,
/// address: String,
/// }
///
/// let fmt_no_quotes = doku::json::Formatting {
/// objects_style: doku::json::ObjectsStyle { use_comma_as_separator: false, ..Default::default() },
/// ..Default::default()
/// };
///
/// let doc = doku::to_json_fmt::<Person>(&fmt_no_quotes);
///
/// doku::assert_doc!(r#"
/// {
/// "name": "string"
/// "address": "string"
/// }
/// "#, doc);
///
/// let fmt_quotes = doku::json::Formatting {
/// objects_style: doku::json::ObjectsStyle { use_comma_as_separator: true, ..Default::default() },
/// ..Default::default()
/// };
///
/// let doc = doku::to_json_fmt::<Person>(&fmt_quotes);
///
/// doku::assert_doc!(r#"
/// {
/// "name": "string",
/// "address": "string"
/// }
/// "#, doc);
/// ```
pub use_comma_as_separator: bool,
}

impl Default for ObjectsStyle {
fn default() -> Self {
Self {
surround_keys_with_quotes: true,
use_comma_as_separator: true,
}
}
}
11 changes: 9 additions & 2 deletions doku/src/printers/json/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ impl Output {
} else {
self.write(key);
}
self.write(':');
self.write(' ');
self.write_char(':');
self.write_char(' ');
}

pub fn write_property_separator_ln(&mut self) {
if self.fmt.objects_style.use_comma_as_separator {
self.write_char(',');
}
self.ln();
}

pub fn write(&mut self, str: impl ToString) {
Expand Down
4 changes: 2 additions & 2 deletions doku/src/printers/json/print_array/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
.with_example(Some(*example))
.print();

self.out.writeln(",");
self.out.write_property_separator_ln();

// TODO if `size.is_some()` and this is the last example, the comma should not be printed
}
Expand All @@ -28,7 +28,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
}
} else {
self.nested().with_ty(ty).print();
self.out.writeln(",");
self.out.write_property_separator_ln();
self.out.writeln("/* ... */");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {

for (variant_idx, variant) in variants {
if variant_idx > 0 {
self.out.writeln(",");
self.out.write_property_separator_ln();
}

let comment = variant.comment.or_else(|| {
Expand All @@ -50,7 +50,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
if let Fields::Named { .. } | Fields::Unnamed { .. } =
variant.fields
{
self.out.writeln(",");
self.out.write_property_separator_ln();
self.out.write_key_and_separator(content);
self.print_fields(&variant.fields, None);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {

for (variant_idx, variant) in variants {
if variant_idx > 0 {
self.out.writeln(",");
self.out.write_property_separator_ln();
}

let comment = variant.comment.or_else(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {

for (variant_idx, variant) in variants.iter().enumerate() {
if variant_idx > 0 {
self.out.writeln(",");
self.out.write_property_separator_ln();
}

self.print_fields(fields_obj, Some(variant));
Expand Down
11 changes: 9 additions & 2 deletions doku/src/printers/json/print_enum/commented/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ fn render_variant(
tag: Tag,
variant: &Variant,
) -> String {
let comma = if ctxt.fmt.objects_style.use_comma_as_separator {
","
} else {
""
};
let quote = if ctxt.fmt.objects_style.surround_keys_with_quotes {
"\""
} else {
Expand All @@ -90,11 +95,12 @@ fn render_variant(
}

fields => format!(
"{{\n\t{q}{}{q}: \"{}\",\n\t{q}{}{q}: {}\n}}",
"{{\n\t{q}{}{q}: \"{}\"{c}\n\t{q}{}{q}: {}\n}}",
tag,
variant.id,
content,
render_variant_fields(ctxt, fields, false, true),
c = comma,
q = quote
),
},
Expand All @@ -110,7 +116,7 @@ fn render_variant(
)
} else {
format!(
"{{\n\t{q}{}{q}: \"{}\",\n\t{}\n}}",
"{{\n\t{q}{}{q}: \"{}\"{c}\n\t{}\n}}",
tag,
variant.id,
render_variant_fields(
Expand All @@ -119,6 +125,7 @@ fn render_variant(
true,
true,
),
c = comma,
q = quote
)
}
Expand Down
9 changes: 5 additions & 4 deletions doku/src/printers/json/print_enum/separated/multiline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ fn print_variant<'ty>(
ctxt.out.writeln("{");
ctxt.out.inc_indent();
ctxt.out.write_key_and_separator(tag);
ctxt.out.write(format!(r#""{}","#, variant.id));
ctxt.out.write(format!(r#""{}""#, variant.id));
ctxt.out.write_property_separator_ln();

if let Fields::Named { .. } | Fields::Unnamed { .. } =
variant.fields
{
ctxt.out.ln();
ctxt.out.write_key_and_separator(content);
ctxt.print_fields(&variant.fields, None);
ctxt.out.ln();
}

ctxt.out.ln();
ctxt.out.dec_indent();
ctxt.out.write("}");
}
Expand All @@ -60,7 +60,8 @@ fn print_variant<'ty>(
ctxt.out.writeln("{");
ctxt.out.inc_indent();
ctxt.out.write_key_and_separator(tag);
ctxt.out.writeln(format!(r#""{}","#, variant.id));
ctxt.out.write(format!(r#""{}""#, variant.id));
ctxt.out.write_property_separator_ln();

if let Fields::Named { .. } | Fields::Unnamed { .. } =
variant.fields
Expand Down
5 changes: 3 additions & 2 deletions doku/src/printers/json/print_fields/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
variant: Option<&'ty Variant>,
) {
if field_id > 0 {
self.out.writeln(",");
self.out.write_property_separator_ln();
}

let field_val = self
Expand All @@ -57,7 +57,8 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
);

self.out.write_key_and_separator(tag);
self.out.writeln(format!(r#""{}","#, variant.id));
self.out.write(format!(r#""{}""#, variant.id));
self.out.write_property_separator_ln();
self.out.write_key_and_separator(field_name);
self.print_fields(&variant.fields, None);

Expand Down
2 changes: 1 addition & 1 deletion doku/src/printers/json/print_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
self.nested().with_ty(key).set_is_key().print();
self.out.write(": ");
self.nested().with_ty(value).print();
self.out.writeln(",");
self.out.write_property_separator_ln();
self.out.write("/* ... */");
}

Expand Down
2 changes: 1 addition & 1 deletion doku/src/printers/json/print_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {

for (field_id, field) in fields.iter().enumerate() {
if field_id > 0 {
self.out.writeln(",");
self.out.write_property_separator_ln();
}

self.nested().with_ty(&field).print();
Expand Down
19 changes: 19 additions & 0 deletions doku/tests/printers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ mod prelude {
doku::to_json::<$ty>()
}};

(@assert to_json_without_comma($ty:ty)) => {{
printer_test!(@assert to_json_without_comma($ty, {}))
}};

(@assert to_json_without_comma($ty:ty, $fmt:tt)) => {{
let fmt = serde_json::json!($fmt);
let mut fmt: doku::json::Formatting = serde_json::from_value(fmt).expect("Given formatting is not valid");
fmt.objects_style.use_comma_as_separator = false;

doku::to_json_fmt::<$ty>(&fmt)
}};

(@assert to_json_without_key_quotes($ty:ty)) => {{
printer_test!(@assert to_json_without_key_quotes($ty, {}))
}};
Expand Down Expand Up @@ -110,6 +122,13 @@ mod prelude {
doku::to_json_val(&<$ty>::default())
}};

(@assert to_json_val_without_comma($ty:ty)) => {{
let mut fmt = doku::json::Formatting::default();
fmt.objects_style.use_comma_as_separator = false;

doku::to_json_fmt_val(&fmt, &<$ty>::default())
}};

(@assert to_json_val_without_key_quotes($ty:ty)) => {{
let mut fmt = doku::json::Formatting::default();
fmt.objects_style.surround_keys_with_quotes = false;
Expand Down
8 changes: 4 additions & 4 deletions doku/tests/printers/enum/nested/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ printer_test! {
"enums_style": "Separated",
}),

"output.without-key-quotes.commented.json" => to_json_without_key_quotes(Ty, {
"output.without-key-quotes.commented.json" => to_json_without_key_quotes(Ty, {
"enums_style": "Commented",
}),
}),

"output.without-key-quotes.separated.json" => to_json_without_key_quotes(Ty, {
"output.without-key-quotes.separated.json" => to_json_without_key_quotes(Ty, {
"enums_style": "Separated",
}),
}),
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ printer_test! {
"enums_style": "Separated",
}),

"output.without-comma.with-user-comments-wrapped.commented.json" => to_json_without_comma(WithUserCommentsWrapped, {
"enums_style": "Commented",
}),

"output.without-comma.with-user-comments-wrapped.separated.json" => to_json_without_comma(WithUserCommentsWrapped, {
"enums_style": "Separated",
}),

"output.without-key-quotes.with-user-comments-wrapped.commented.json" => to_json_without_key_quotes(WithUserCommentsWrapped, {
"enums_style": "Commented",
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Possible variants:
// - {
// "t": "Foo"
// "c": {
// // Some comment
// "a": "string"
// }
// }
// = This is `Foo`
// - {
// "t": "Bar"
// "c": {
// // Some comment
// "a": "string"
// // Some comment
// "b": 123
// }
// }
// = This is `Bar`
"value": { "t": "Foo", "c": ... }
}
Loading