Skip to content

Commit

Permalink
fix(#666): Add builder for non-exhaustive structs (#688)
Browse files Browse the repository at this point in the history
* Add builder for DirectorySourceOptions

* Use derive_builder crate

* Add derive builder for other structs
  • Loading branch information
hadizamani021 authored Dec 25, 2024
1 parent 0df66fa commit 891426f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ path = "src/cli.rs"
[dependencies]
log = { version = "0.4.0" }
thiserror = "2"
derive_builder = "0.20.2"
pest = "2.1.0"
pest_derive = "2.1.0"
serde = "1.0.0"
Expand Down
5 changes: 5 additions & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ use crate::template::{Template, TemplateOptions};
#[cfg(feature = "dir_source")]
use walkdir::WalkDir;

#[cfg(feature = "dir_source")]
use derive_builder::Builder;

#[cfg(feature = "script_helper")]
use rhai::Engine;

Expand Down Expand Up @@ -102,9 +105,11 @@ fn rhai_engine() -> Engine {

/// Options for importing template files from a directory.
#[non_exhaustive]
#[derive(Builder)]
#[cfg(feature = "dir_source")]
pub struct DirectorySourceOptions {
/// The name extension for template files
#[builder(setter(into))]
pub tpl_extension: String,
/// Whether to include hidden files (file name that starts with `.`)
pub hidden: bool,
Expand Down
19 changes: 14 additions & 5 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::grammar::{HandlebarsParser, Rule};
use crate::json::path::{parse_json_path_from_iter, Path};
use crate::support;

use derive_builder::Builder;

use self::TemplateElement::{
Comment, DecoratorBlock, DecoratorExpression, Expression, HelperBlock, HtmlExpression,
PartialBlock, PartialExpression, RawString,
Expand All @@ -23,8 +25,9 @@ pub struct TemplateMapping(pub usize, pub usize);

/// A handlebars template
#[non_exhaustive]
#[derive(PartialEq, Eq, Clone, Debug, Default)]
#[derive(Builder, PartialEq, Eq, Clone, Debug, Default)]
pub struct Template {
#[builder(setter(into, strip_option), default)]
pub name: Option<String>,
pub elements: Vec<TemplateElement>,
pub mapping: Vec<TemplateMapping>,
Expand All @@ -44,7 +47,7 @@ impl TemplateOptions {
}

#[non_exhaustive]
#[derive(PartialEq, Eq, Clone, Debug)]
#[derive(Builder, PartialEq, Eq, Clone, Debug)]
pub struct Subexpression {
// we use box here avoid resursive struct definition
pub element: Box<TemplateElement>,
Expand Down Expand Up @@ -113,11 +116,12 @@ pub enum BlockParam {
}

#[non_exhaustive]
#[derive(PartialEq, Eq, Clone, Debug)]
#[derive(Builder, PartialEq, Eq, Clone, Debug)]
pub struct ExpressionSpec {
pub name: Parameter,
pub params: Vec<Parameter>,
pub hash: HashMap<String, Parameter>,
#[builder(setter(strip_option), default)]
pub block_param: Option<BlockParam>,
pub omit_pre_ws: bool,
pub omit_pro_ws: bool,
Expand All @@ -135,13 +139,16 @@ pub enum Parameter {
}

#[non_exhaustive]
#[derive(PartialEq, Eq, Clone, Debug)]
#[derive(Builder, PartialEq, Eq, Clone, Debug)]
pub struct HelperTemplate {
pub name: Parameter,
pub params: Vec<Parameter>,
pub hash: HashMap<String, Parameter>,
#[builder(setter(strip_option), default)]
pub block_param: Option<BlockParam>,
#[builder(setter(strip_option), default)]
pub template: Option<Template>,
#[builder(setter(strip_option), default)]
pub inverse: Option<Template>,
pub block: bool,
pub chain: bool,
Expand Down Expand Up @@ -277,13 +284,15 @@ impl HelperTemplate {
}

#[non_exhaustive]
#[derive(PartialEq, Eq, Clone, Debug)]
#[derive(Builder, PartialEq, Eq, Clone, Debug)]
pub struct DecoratorTemplate {
pub name: Parameter,
pub params: Vec<Parameter>,
pub hash: HashMap<String, Parameter>,
#[builder(setter(strip_option), default)]
pub template: Option<Template>,
// for partial indent
#[builder(setter(into, strip_option), default)]
pub indent: Option<String>,
pub(crate) indent_before_write: bool,
}
Expand Down

0 comments on commit 891426f

Please sign in to comment.