Skip to content

Commit 6c96070

Browse files
committed
refactor: remove unwrap in a regex matching snippet
1 parent 7f60e2b commit 6c96070

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

src/commit.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,23 @@ pub fn is_valid_commit_type(commit_type: &str, config: &config::Config) -> bool
108108
}
109109

110110
/// Check if the issue key in the commit message is valid.
111-
pub fn is_valid_issue_key(issue_key: &Option<String>, config: &config::Config) -> bool {
111+
pub fn is_valid_issue_key(issue_key: &Option<String>, config: &config::Config) -> Result<bool> {
112112
if let Some(lint_config) = &config.lint {
113113
if let Some(issue_key_config) = &lint_config.issue_key_missing {
114114
if let Some(enabled) = issue_key_config.enabled {
115115
if !enabled {
116-
return true; // If linting is disabled, any issue key is valid
116+
return Ok(true); // If linting is disabled, any issue key is valid
117117
}
118118
}
119119
if let Some(issue_key_pattern) = &issue_key_config.pattern {
120-
let re = regex::Regex::new(issue_key_pattern).unwrap();
121-
return re.is_match(&issue_key.as_ref().unwrap_or(&"".to_string()));
120+
let re = regex::Regex::new(issue_key_pattern).map_err(|e| {
121+
anyhow::anyhow!("Invalid issue_key pattern '{}': {}", issue_key_pattern, e)
122+
})?;
123+
return Ok(re.is_match(&issue_key.as_ref().unwrap_or(&"".to_string())));
122124
}
123125
}
124126
}
125-
true
127+
Ok(true)
126128
}
127129

128130
pub fn is_valid_scope(scope: &Option<String>, config: &config::Config) -> bool {
@@ -240,7 +242,7 @@ pub fn handle_commit(
240242
return Err(anyhow::anyhow!("Aborted: Invalid commit type."));
241243
}
242244

243-
if !is_valid_issue_key(&params.issue, config) {
245+
if !is_valid_issue_key(&params.issue, config)? {
244246
println!(
245247
"{}",
246248
"Issue reference is required by your .tbdflow.yml config.".red()

0 commit comments

Comments
 (0)