Skip to content

Allow to disable quotes around objects' keys #22

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 26, 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
1 change: 1 addition & 0 deletions doku/src/printers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl<'a> Printer<'a> {
vis: self.visibility,
fmt: fmt.as_ref(),
out: &mut out,
is_key: Default::default(),
parent: Default::default(),
example: Default::default(),
flat: Default::default(),
Expand Down
8 changes: 8 additions & 0 deletions doku/src/printers/json/ctxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Ctxt<'fmt, 'ty, 'out> {
pub vis: Visibility,
pub fmt: &'fmt Formatting,
pub out: &'out mut Output,
pub is_key: bool,

/// Parent of `ty`.
///
Expand Down Expand Up @@ -62,6 +63,7 @@ impl<'fmt, 'ty, 'out> Ctxt<'fmt, 'ty, 'out> {
vis: self.vis,
fmt: self.fmt,
out: self.out,
is_key: false,
parent: self.parent,
example: self.example,
flat: self.flat,
Expand Down Expand Up @@ -127,6 +129,7 @@ impl<'fmt, 'ty, 'out> Ctxt<'fmt, 'ty, 'out> {
vis: self.vis,
fmt,
out: self.out,
is_key: false,
parent: self.parent,
example: self.example,
flat: self.flat,
Expand All @@ -144,6 +147,11 @@ impl<'fmt, 'ty, 'out> Ctxt<'fmt, 'ty, 'out> {
self
}

pub fn set_is_key(mut self) -> Self {
self.is_key = true;
self
}

pub fn example(&self) -> Option<Example> {
self.example.or(self.ty.example)
}
Expand Down
6 changes: 5 additions & 1 deletion doku/src/printers/json/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ mod doc_comments;
mod enums_style;
mod indent_style;
mod layout;
mod objects_style;
mod values_style;

pub use self::{
auto_comments::*, comments_style::*, doc_comments::*, enums_style::*,
indent_style::*, layout::*, values_style::*,
indent_style::*, layout::*, objects_style::*, values_style::*,
};

use crate::*;
Expand Down Expand Up @@ -38,6 +39,9 @@ pub struct Formatting {
/// Determines whether the document should contain one or two columns.
pub layout: Layout,

/// Determines how objects should get displayed.
pub objects_style: ObjectsStyle,

/// Determines how values should get displayed.
pub values_style: ValuesStyle,
}
Expand Down
52 changes: 52 additions & 0 deletions doku/src/printers/json/formatting/objects_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::*;

/// Determines the objects style.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ObjectsStyle {
/// Whether to use quotes to surround keys
///
/// ```
/// use doku::Document;
///
/// #[derive(Document)]
/// struct Person {
/// name: String,
/// }
///
/// let fmt_no_quotes = doku::json::Formatting {
/// objects_style: doku::json::ObjectsStyle { surround_keys_with_quotes: false },
/// ..Default::default()
/// };
///
/// let doc = doku::to_json_fmt::<Person>(&fmt_no_quotes);
///
/// doku::assert_doc!(r#"
/// {
/// name: "string"
/// }
/// "#, doc);
///
/// let fmt_quotes = doku::json::Formatting {
/// objects_style: doku::json::ObjectsStyle { surround_keys_with_quotes: true },
/// ..Default::default()
/// };
///
/// let doc = doku::to_json_fmt::<Person>(&fmt_quotes);
///
/// doku::assert_doc!(r#"
/// {
/// "name": "string"
/// }
/// "#, doc);
/// ```
pub surround_keys_with_quotes: bool,
}

impl Default for ObjectsStyle {
fn default() -> Self {
Self {
surround_keys_with_quotes: true,
}
}
}
12 changes: 12 additions & 0 deletions doku/src/printers/json/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ impl Output {
}
}

pub fn write_key_and_separator(&mut self, key: impl ToString) {
if self.fmt.objects_style.surround_keys_with_quotes {
self.write_char('"');
self.write(key);
self.write_char('"');
} else {
self.write(key);
}
self.write(':');
self.write(' ');
}

pub fn write(&mut self, str: impl ToString) {
for ch in str.to_string().chars() {
self.write_char(ch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
}

self.out.inc_indent();
self.out.write(format!(r#""{}": "{}""#, tag, variant.id));
self.out.write_key_and_separator(tag);
self.out.write(format!(r#""{}""#, variant.id));

if let Fields::Named { .. } | Fields::Unnamed { .. } =
variant.fields
{
self.out.writeln(",");
self.out.write(format!(r#""{}": "#, content));
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 @@ -46,7 +46,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
}

self.out.inc_indent();
self.out.write(format!(r#""{}": "#, variant.id));
self.out.write_key_and_separator(variant.id);
self.print_fields(&variant.fields, None);
self.out.ln();
self.out.dec_indent();
Expand Down
45 changes: 35 additions & 10 deletions doku/src/printers/json/print_enum/commented/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,50 +72,74 @@ fn render_variant(
tag: Tag,
variant: &Variant,
) -> String {
let quote = if ctxt.fmt.objects_style.surround_keys_with_quotes {
"\""
} else {
""
};

match tag {
Tag::Adjacent { tag, content } => match &variant.fields {
Fields::Unit => {
format!("{{\n\t\"{}\": \"{}\"\n}}", tag, variant.id)
format!(
"{{\n\t{q}{}{q}: \"{}\"\n}}",
tag,
variant.id,
q = quote
)
}

fields => format!(
"{{\n\t\"{}\": \"{}\",\n\t\"{}\": {}\n}}",
"{{\n\t{q}{}{q}: \"{}\",\n\t{q}{}{q}: {}\n}}",
tag,
variant.id,
content,
render_variant_fields(ctxt, fields, false, true)
render_variant_fields(ctxt, fields, false, true),
q = quote
),
},

Tag::Internal { tag } => {
if let Fields::Named { fields } = &variant.fields {
if fields.is_empty() {
format!("{{\n\t\"{}\": \"{}\"\n}}", tag, variant.id)
format!(
"{{\n\t{q}{}{q}: \"{}\"\n}}",
tag,
variant.id,
q = quote
)
} else {
format!(
"{{\n\t\"{}\": \"{}\",\n\t{}\n}}",
"{{\n\t{q}{}{q}: \"{}\",\n\t{}\n}}",
tag,
variant.id,
render_variant_fields(
ctxt,
&variant.fields,
true,
true
)
true,
),
q = quote
)
}
} else {
format!("{{\n\t\"{}\": \"{}\"\n}}", tag, variant.id)
format!(
"{{\n\t{q}{}{q}: \"{}\"\n}}",
tag,
variant.id,
q = quote
)
}
}

Tag::External => match &variant.fields {
Fields::Unit => format!("\"{}\"", variant.id),

fields => format!(
"{{\n\t\"{}\": {}\n}}",
"{{\n\t{q}{}{q}: {}\n}}",
variant.id,
render_variant_fields(ctxt, fields, false, true)
render_variant_fields(ctxt, fields, false, true),
q = quote
),
},

Expand Down Expand Up @@ -151,6 +175,7 @@ fn render_variant_fields(
vis: ctxt.vis,
fmt: &fmt,
out: &mut out,
is_key: Default::default(),
parent: Default::default(),
example: Default::default(),
flat,
Expand Down
16 changes: 12 additions & 4 deletions doku/src/printers/json/print_enum/commented/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ pub(super) fn sketch(
fn sketch_variant(ctxt: &mut Ctxt<'_, '_, '_>, tag: Tag, variant: &Variant) {
match tag {
Tag::Adjacent { tag, content } => {
ctxt.out.write(format!(r#"{{ "{}": "{}""#, tag, variant.id));
ctxt.out.write("{ ");
ctxt.out.write_key_and_separator(tag);
ctxt.out.write(format!(r#""{}""#, variant.id));

if let Fields::Named { .. } | Fields::Unnamed { .. } =
variant.fields
{
ctxt.out.write(format!(r#", "{}": ..."#, content));
ctxt.out.write(", ");
ctxt.out.write_key_and_separator(content);
ctxt.out.write("...");
}

ctxt.out.write(" }");
}

Tag::Internal { tag } => {
ctxt.out.write(format!(r#"{{ "{}": "{}""#, tag, variant.id));
ctxt.out.write("{ ");
ctxt.out.write_key_and_separator(tag);
ctxt.out.write(format!(r#""{}""#, variant.id));

if let Fields::Named { .. } | Fields::Unnamed { .. } =
variant.fields
Expand All @@ -48,7 +54,9 @@ fn sketch_variant(ctxt: &mut Ctxt<'_, '_, '_>, tag: Tag, variant: &Variant) {

Tag::External => match variant.fields {
Fields::Named { .. } | Fields::Unnamed { .. } => {
ctxt.out.write(format!(r#"{{ "{}": ... }}"#, variant.id));
ctxt.out.write("{ ");
ctxt.out.write_key_and_separator(variant.id);
ctxt.out.write("... }")
}

Fields::Unit => {
Expand Down
2 changes: 1 addition & 1 deletion doku/src/printers/json/print_enum/separated/condensed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(super) fn print(
Tag::Adjacent { tag, .. } | Tag::Internal { tag } => {
ctxt.out.writeln("{");
ctxt.out.inc_indent();
ctxt.out.write(format!(r#""{}": "#, tag));
ctxt.out.write_key_and_separator(tag);
}

Tag::External => {
Expand Down
10 changes: 6 additions & 4 deletions doku/src/printers/json/print_enum/separated/multiline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ fn print_variant<'ty>(
Tag::Adjacent { tag, content } => {
ctxt.out.writeln("{");
ctxt.out.inc_indent();
ctxt.out.write(format!(r#""{}": "{}","#, tag, variant.id));
ctxt.out.write_key_and_separator(tag);
ctxt.out.write(format!(r#""{}","#, variant.id));

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

Expand All @@ -58,7 +59,8 @@ fn print_variant<'ty>(
Tag::Internal { tag } => {
ctxt.out.writeln("{");
ctxt.out.inc_indent();
ctxt.out.writeln(format!(r#""{}": "{}","#, tag, variant.id));
ctxt.out.write_key_and_separator(tag);
ctxt.out.writeln(format!(r#""{}","#, variant.id));

if let Fields::Named { .. } | Fields::Unnamed { .. } =
variant.fields
Expand All @@ -78,7 +80,7 @@ fn print_variant<'ty>(
Fields::Named { .. } | Fields::Unnamed { .. } => {
ctxt.out.writeln("{");
ctxt.out.inc_indent();
ctxt.out.write(format!(r#""{}": "#, variant.id));
ctxt.out.write_key_and_separator(variant.id);
ctxt.print_fields(&variant.fields, None);
ctxt.out.ln();
ctxt.out.dec_indent();
Expand Down
7 changes: 4 additions & 3 deletions doku/src/printers/json/print_fields/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
variants, but it seems like we're not",
);

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

return;
Expand All @@ -70,7 +71,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
.with_flat()
.print();
} else {
self.out.write(format!("\"{}\": ", field_name));
self.out.write_key_and_separator(field_name);
self.nested().with_ty(&field.ty).with_val(field_val).print();
}
}
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 @@ -10,7 +10,7 @@ impl<'ty> Ctxt<'_, 'ty, '_> {
if let Some(example) = self.first_example() {
self.out.write(example);
} else {
self.nested().with_ty(key).print();
self.nested().with_ty(key).set_is_key().print();
self.out.write(": ");
self.nested().with_ty(value).print();
self.out.writeln(",");
Expand Down
10 changes: 6 additions & 4 deletions doku/src/printers/json/print_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ impl Ctxt<'_, '_, '_> {
}

pub(super) fn print_string(&mut self) {
self.print_scalar(&format!(
"\"{}\"",
self.first_example().unwrap_or("string")
));
let first_example = self.first_example().unwrap_or("string");
if !self.is_key || self.fmt.objects_style.surround_keys_with_quotes {
self.print_scalar(&format!("\"{}\"", first_example));
} else {
self.print_scalar(first_example);
}
}

fn print_scalar(&mut self, val: &str) {
Expand Down
Loading