diff --git a/examples/clog-only.toml b/examples/clog-only.toml new file mode 100644 index 0000000..20f9127 --- /dev/null +++ b/examples/clog-only.toml @@ -0,0 +1,48 @@ +[clog] +# A repository link with the trailing '.git' which will be used to generate +# all commit and issue links +repository = "https://github.com/clog-tool/clog-lib" +# A constant release title +subtitle = "my awesome title" + +# specify the style of commit links to generate, defaults to "github" if omitted +link-style = "github" + +# The preferred way to set a constant changelog. This file will be read for old changelog +# data, then prepended to for new changelog data. It's the equivilant to setting +# both infile and outfile to the same file. +# +# Do not use with outfile or infile fields! +# +# Defaults to stdout when omitted +changelog = "mychangelog.md" + +# This sets an output file only! If it exists already, new changelog data will be +# prepended, if not it will be created. +# +# This is useful in conjunction with the infile field if you have a separate file +# that you would like to append after newly created clog data +# +# Defaults to stdout when omitted +outfile = "MyChangelog.md" + +# This sets the input file old! Any data inside this file will be appended to any +# new data that clog picks up +# +# This is useful in conjunction with the outfile field where you may wish to read +# from one file and append that data to the clog output in another +infile = "My_old_changelog.md" + +# This sets the output format. There are two options "json" or "markdown" and +# defaults to "markdown" when omitted +output-format = "json" + +# If you use tags, you can set the following if you wish to only pick +# up changes since your latest tag +from-latest-tag = true + +# The working or project directory +git-work-tree = "/myproject" + +# the git metadata directory +git-dir = "/myproject/.git" diff --git a/src/clog.rs b/src/clog.rs index 411ac10..04310af 100644 --- a/src/clog.rs +++ b/src/clog.rs @@ -27,6 +27,21 @@ fn regex_default() -> Regex { regex!(r"^([^:\(]+?)(?:\(([^\)]*?)?\))?:(.*)") } fn closes_regex_default() -> Regex { regex!(r"(?:Closes|Fixes|Resolves)\s((?:#(\d+)(?:,\s)?)+)") } fn breaks_regex_default() -> Regex { regex!(r"(?:Breaks|Broke)\s((?:#(\d+)(?:,\s)?)+)") } fn breaking_regex_default() -> Regex { regex!(r"(?i:breaking)") } +fn sections_default() -> IndexMap> { + let mut sections = IndexMap::new(); + sections.insert( + "Features".to_owned(), + vec!["ft".to_owned(), "feat".to_owned()], + ); + sections.insert( + "Bug Fixes".to_owned(), + vec!["fx".to_owned(), "fix".to_owned()], + ); + sections.insert("Performance".to_owned(), vec!["perf".to_owned()]); + sections.insert("Unknown".to_owned(), vec!["unk".to_owned()]); + sections.insert("Breaking Changes".to_owned(), vec!["breaks".to_owned()]); + sections +} /// The base struct used to set options and interact with the library. #[derive(Debug, Clone)] @@ -87,18 +102,7 @@ pub struct Clog { impl Default for Clog { fn default() -> Self { debug!("Creating default clog with Clog::default()"); - let mut sections = IndexMap::new(); - sections.insert( - "Features".to_owned(), - vec!["ft".to_owned(), "feat".to_owned()], - ); - sections.insert( - "Bug Fixes".to_owned(), - vec!["fx".to_owned(), "fix".to_owned()], - ); - sections.insert("Performance".to_owned(), vec!["perf".to_owned()]); - sections.insert("Unknown".to_owned(), vec!["unk".to_owned()]); - sections.insert("Breaking Changes".to_owned(), vec!["breaks".to_owned()]); + let sections = sections_default(); Clog { grep: format!( @@ -143,7 +147,7 @@ impl TryFrom for Clog { subtitle: cfg.clog.subtitle, infile: cfg.clog.changelog.clone().or(cfg.clog.infile), outfile: cfg.clog.changelog.or(cfg.clog.outfile), - section_map: cfg.sections, + section_map: cfg.sections.unwrap_or_else(sections_default), component_map: cfg.components, out_format: cfg.clog.output_format, git_dir: cfg.clog.git_dir, @@ -604,7 +608,7 @@ impl Clog { Ok(Commit { hash: hash.to_string(), - subject: subject.unwrap().to_owned(), + subject: subject.unwrap_or_default().to_owned(), component: component.unwrap_or_default(), closes, breaks, diff --git a/src/config.rs b/src/config.rs index d8050a3..8b1ddac 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,7 +9,7 @@ use crate::{fmt::ChangelogFormat, link_style::LinkStyle}; pub struct RawCfg { pub clog: RawClogCfg, #[serde(default)] - pub sections: IndexMap>, + pub sections: Option>>, #[serde(default)] pub components: HashMap>, } @@ -33,7 +33,7 @@ mod tests { use super::*; #[test] - fn from_config() { + fn from_config_full() { let cfg = include_str!("../examples/clog.toml"); let res = toml::from_str(cfg); assert!(res.is_ok(), "{res:?}"); @@ -52,12 +52,13 @@ mod tests { assert_eq!(cfg.clog.git_work_tree, Some("/myproject".into())); assert_eq!(cfg.clog.git_dir, Some("/myproject/.git".into())); assert!(cfg.clog.from_latest_tag); + assert!(cfg.sections.is_some()); assert_eq!( - cfg.sections.get("MySection"), + cfg.sections.as_ref().unwrap().get("MySection"), Some(&vec!["mysec".into(), "ms".into()]) ); assert_eq!( - cfg.sections.get("Another Section"), + cfg.sections.as_ref().unwrap().get("Another Section"), Some(&vec!["another".into()]) ); assert_eq!( @@ -66,6 +67,16 @@ mod tests { ); } + #[test] + fn from_config_no_sections() { + let cfg = include_str!("../examples/clog-only.toml"); + let res = toml::from_str(cfg); + assert!(res.is_ok(), "{res:?}"); + let cfg: RawCfg = res.unwrap(); + + assert!(cfg.sections.is_none()); + } + #[test] fn dogfood_config() { let cfg = include_str!("../.clog.toml");