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

fix: Use default sections if config file has no [sections] #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions examples/clog-only.toml
Original file line number Diff line number Diff line change
@@ -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"
32 changes: 18 additions & 14 deletions src/clog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Vec<String>> {
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)]
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -143,7 +147,7 @@ impl TryFrom<RawCfg> 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,
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 15 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{fmt::ChangelogFormat, link_style::LinkStyle};
pub struct RawCfg {
pub clog: RawClogCfg,
#[serde(default)]
pub sections: IndexMap<String, Vec<String>>,
pub sections: Option<IndexMap<String, Vec<String>>>,
#[serde(default)]
pub components: HashMap<String, Vec<String>>,
}
Expand All @@ -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:?}");
Expand All @@ -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!(
Expand All @@ -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");
Expand Down