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 builder derive and non_exhaustive for option structs #292

Merged
merged 1 commit into from
Jun 17, 2023
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
73 changes: 73 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ shell-words = { version = "1.0", optional = true }
slug = "0.1.4"
emojis = { version = "0.5.2", optional = true }
arbitrary = { version = "1", optional = true, features = ["derive"] }
derive_builder = "0.12.0"

[dev-dependencies]
ntest = "0.9"
Expand Down
22 changes: 12 additions & 10 deletions examples/s-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const INDENT: usize = 4;
const CLOSE_NEWLINE: bool = false;

use comrak::nodes::{AstNode, NodeValue};
use comrak::{parse_document, Arena, ExtensionOptions, Options};
use comrak::{parse_document, Arena, ExtensionOptionsBuilder, Options};
use std::env;
use std::error::Error;
use std::fs::File;
Expand Down Expand Up @@ -74,15 +74,17 @@ fn iter_nodes<'a, W: Write>(
fn dump(source: &str) -> io::Result<()> {
let arena = Arena::new();

let mut extension = ExtensionOptions::default();
extension.strikethrough = true;
extension.tagfilter = true;
extension.table = true;
extension.autolink = true;
extension.tasklist = true;
extension.superscript = true;
extension.footnotes = true;
extension.description_lists = true;
let extension = ExtensionOptionsBuilder::default()
.strikethrough(true)
.tagfilter(true)
.table(true)
.autolink(true)
.tasklist(true)
.superscript(true)
.footnotes(true)
.description_lists(true)
.build()
.unwrap();

let opts = Options {
extension,
Expand Down
73 changes: 73 additions & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ pub use html::format_document as format_html;
pub use html::format_document_with_plugins as format_html_with_plugins;
pub use html::Anchorizer;
pub use parser::{
parse_document, parse_document_with_broken_link_callback, ExtensionOptions, ListStyleType,
Options, ParseOptions, Plugins, RenderOptions, RenderPlugins,
parse_document, parse_document_with_broken_link_callback, ExtensionOptions,
ExtensionOptionsBuilder, ListStyleType, Options, ParseOptions, ParseOptionsBuilder, Plugins,
PluginsBuilder, RenderOptions, RenderOptionsBuilder, RenderPlugins, RenderPluginsBuilder,
};
pub use typed_arena::Arena;
pub use xml::format_document as format_xml;
Expand Down
63 changes: 35 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! The `comrak` binary.

use comrak::{
adapters::SyntaxHighlighterAdapter, plugins::syntect::SyntectAdapter, Arena, ExtensionOptions,
ListStyleType, Options, ParseOptions, Plugins, RenderOptions,
adapters::SyntaxHighlighterAdapter, plugins::syntect::SyntectAdapter, Arena,
ExtensionOptionsBuilder, ListStyleType, Options, ParseOptionsBuilder, Plugins,
RenderOptionsBuilder,
};
use std::boxed::Box;
use std::env;
Expand Down Expand Up @@ -194,36 +195,42 @@ fn main() -> Result<(), Box<dyn Error>> {

let exts = &cli.extensions;

let mut extension = ExtensionOptions::default();
extension.strikethrough = exts.contains(&Extension::Strikethrough) || cli.gfm;
extension.tagfilter = exts.contains(&Extension::Tagfilter) || cli.gfm;
extension.table = exts.contains(&Extension::Table) || cli.gfm;
extension.autolink = exts.contains(&Extension::Autolink) || cli.gfm;
extension.tasklist = exts.contains(&Extension::Tasklist) || cli.gfm;
extension.superscript = exts.contains(&Extension::Superscript);
extension.header_ids = cli.header_ids;
extension.footnotes = exts.contains(&Extension::Footnotes);
extension.description_lists = exts.contains(&Extension::DescriptionLists);
extension.front_matter_delimiter = cli.front_matter_delimiter;
let mut extension = ExtensionOptionsBuilder::default();
extension
.strikethrough(exts.contains(&Extension::Strikethrough) || cli.gfm)
.tagfilter(exts.contains(&Extension::Tagfilter) || cli.gfm)
.table(exts.contains(&Extension::Table) || cli.gfm)
.autolink(exts.contains(&Extension::Autolink) || cli.gfm)
.tasklist(exts.contains(&Extension::Tasklist) || cli.gfm)
.superscript(exts.contains(&Extension::Superscript))
.header_ids(cli.header_ids)
.footnotes(exts.contains(&Extension::Footnotes))
.description_lists(exts.contains(&Extension::DescriptionLists))
.front_matter_delimiter(cli.front_matter_delimiter);

#[cfg(feature = "shortcodes")]
{
extension.shortcodes = cli.gemojis;
extension.shortcodes(cli.gemojis);
}

let mut parse = ParseOptions::default();
parse.smart = cli.smart;
parse.default_info_string = cli.default_info_string;
parse.relaxed_tasklist_matching = cli.relaxed_tasklist_character;

let mut render = RenderOptions::default();
render.hardbreaks = cli.hardbreaks;
render.github_pre_lang = cli.github_pre_lang || cli.gfm;
render.full_info_string = cli.full_info_string;
render.width = cli.width;
render.unsafe_ = cli.unsafe_;
render.escape = cli.escape;
render.list_style = cli.list_style.into();
render.sourcepos = cli.sourcepos;
let extension = extension.build()?;

let parse = ParseOptionsBuilder::default()
.smart(cli.smart)
.default_info_string(cli.default_info_string)
.relaxed_tasklist_matching(cli.relaxed_tasklist_character)
.build()?;

let render = RenderOptionsBuilder::default()
.hardbreaks(cli.hardbreaks)
.github_pre_lang(cli.github_pre_lang || cli.gfm)
.full_info_string(cli.full_info_string)
.width(cli.width)
.unsafe_(cli.unsafe_)
.escape(cli.escape)
.list_style(cli.list_style.into())
.sourcepos(cli.sourcepos)
.build()?;

let options = Options {
extension,
Expand Down
16 changes: 11 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::nodes::{
};
use crate::scanners;
use crate::strings::{self, split_off_front_matter, Case};
use derive_builder::Builder;
use std::cell::RefCell;
use std::cmp::min;
use std::collections::HashMap;
Expand Down Expand Up @@ -151,7 +152,8 @@ pub struct Options {
}

#[non_exhaustive]
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, Builder)]
#[builder(default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
/// Options to select extensions.
pub struct ExtensionOptions {
Expand Down Expand Up @@ -347,7 +349,8 @@ pub struct ExtensionOptions {
}

#[non_exhaustive]
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, Builder)]
#[builder(default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
/// Options for parser functions.
pub struct ParseOptions {
Expand Down Expand Up @@ -384,7 +387,8 @@ pub struct ParseOptions {
}

#[non_exhaustive]
#[derive(Default, Debug, Clone, Copy)]
#[derive(Default, Debug, Clone, Copy, Builder)]
#[builder(default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
/// Options for formatter functions.
pub struct RenderOptions {
Expand Down Expand Up @@ -533,15 +537,17 @@ pub struct RenderOptions {
}

#[non_exhaustive]
#[derive(Default, Debug)]
#[derive(Default, Debug, Clone, Builder)]
#[builder(default)]
/// Umbrella plugins struct.
pub struct Plugins<'p> {
/// Configure render-time plugins.
pub render: RenderPlugins<'p>,
}

#[non_exhaustive]
#[derive(Default)]
#[derive(Default, Clone, Builder)]
#[builder(default)]
/// Plugins for alternative rendering.
pub struct RenderPlugins<'p> {
/// Provide a syntax highlighter adapter implementation for syntax
Expand Down