Skip to content

Allow to specify comment style #21

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 1 commit into from
Sep 23, 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
8 changes: 6 additions & 2 deletions doku/src/printers/json/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
mod auto_comments;
mod comments_style;
mod doc_comments;
mod enums_style;
mod indent_style;
mod layout;
mod values_style;

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

use crate::*;
Expand All @@ -22,6 +23,9 @@ pub struct Formatting {
/// displayed.
pub auto_comments: AutoComments,

/// Determines how comments should get displayed.
pub comments_style: CommentsStyle,

/// Determines if doc-comments should get displayed.
pub doc_comments: DocComments,

Expand Down
41 changes: 41 additions & 0 deletions doku/src/printers/json/formatting/comments_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::*;

/// Determines the comments style.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CommentsStyle {
/// String to use as comment separator
///
/// ```
/// use doku::Document;
///
/// #[derive(Document)]
/// struct Person {
/// /// First name
/// name: String,
/// }
///
/// let fmt = doku::json::Formatting {
/// comments_style: doku::json::CommentsStyle { separator: "#".to_owned() },
/// ..Default::default()
/// };
///
/// let doc = doku::to_json_fmt::<Person>(&fmt);
///
/// doku::assert_doc!(r#"
/// {
/// ## First name
/// "name": "string"
/// }
/// "#, doc);
/// ```
pub separator: String,
}

impl Default for CommentsStyle {
fn default() -> Self {
Self {
separator: "//".to_owned(),
}
}
}
7 changes: 6 additions & 1 deletion doku/src/printers/json/output/layouts/one_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ pub fn render(out: Output) -> String {

for comment in comments {
swrite!(result, for 0..indent, " ");
swrite!(result, "// {}\n", comment);
swrite!(
result,
"{} {}\n",
&out.fmt.comments_style.separator,
comment
);
}

swrite!(result, for 0..indent, " ");
Expand Down
7 changes: 6 additions & 1 deletion doku/src/printers/json/output/layouts/two_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ pub fn render(out: Output, align: bool, spacing: usize) -> String {
swrite!(result, for 0..spacing, " ");
}

swrite!(result, "// {}", comment);
swrite!(
result,
"{} {}",
&out.fmt.comments_style.separator,
comment
);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions doku/tests/printers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ mod r#enum;
#[path = "printers/features/mod.rs"]
mod features;

#[allow(dead_code)]
#[path = "printers/formatting/mod.rs"]
mod formatting;

#[allow(dead_code)]
#[path = "printers/map/mod.rs"]
mod map;
Expand Down
25 changes: 25 additions & 0 deletions doku/tests/printers/formatting/comments_style/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::prelude::*;

#[derive(Document)]
struct Person {
/// First name
name: String,
}

printer_test! {
"output.default_separator_one_column.json" => to_json_fmt(Person, {
}),

"output.default_separator_two_columns.json" => to_json_fmt(Person, {
"layout": { "TwoColumns": { "align": false, "spacing": 2 }}
}),

"output.custom_separator_one_column.json" => to_json_fmt(Person, {
"comments_style": { "separator": "#" },
}),

"output.custom_separator_two_columns.json" => to_json_fmt(Person, {
"comments_style": { "separator": "#" },
"layout": { "TwoColumns": { "align": false, "spacing": 2 }}
}),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
# First name
"name": "string"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "string" # First name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
// First name
"name": "string"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "string" // First name
}
1 change: 1 addition & 0 deletions doku/tests/printers/formatting/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod comments_style;