Skip to content

Commit 695a1a2

Browse files
committed
refactor(config): Use BufReader to read and parse contents of the config file
1 parent 593f952 commit 695a1a2

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/commit_pattern/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ impl CommitPattern {
8383
]
8484
}
8585
}
86+
87+
impl Default for CommitPattern {
88+
fn default() -> Self {
89+
Self {
90+
config: Config::default(),
91+
commit_types: Self::commit_types(),
92+
commit_scopes: Self::commit_scopes(),
93+
skip_commit: vec![],
94+
msg: Messages::default(),
95+
}
96+
}
97+
}

src/config/mod.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22
mod tests;
33

44
use anyhow::{anyhow, Context, Result};
5-
use std::fs;
6-
use std::path::Path;
5+
use std::fs::File;
76
use std::path::PathBuf;
87

98
use crate::commit_pattern::CommitPattern;
109

11-
fn get_config_path_content(config_path: impl AsRef<Path>) -> Result<String> {
12-
let content = fs::read_to_string(config_path)?;
13-
Ok(content)
14-
}
15-
1610
fn select_custom_config_path(config: Option<PathBuf>) -> Result<PathBuf> {
1711
match config {
1812
Some(config_path) => {
@@ -62,7 +56,17 @@ fn get_config_path() -> Result<PathBuf> {
6256

6357
pub fn get_pattern(config_path: Option<PathBuf>) -> Result<CommitPattern> {
6458
let selected_config_path = select_custom_config_path(config_path)?;
65-
let pattern_str =
66-
get_config_path_content(selected_config_path).unwrap_or_else(|_| "{}".to_owned());
67-
serde_json::from_str(&pattern_str).context("Failed to parse commit pattern from file")
59+
if selected_config_path.exists() {
60+
let file = File::open(&selected_config_path).context(format!(
61+
"Could not open config file: {}",
62+
selected_config_path.display()
63+
))?;
64+
let reader = std::io::BufReader::new(file);
65+
Ok(serde_json::from_reader(reader).context(format!(
66+
"Could not parse config file: {}",
67+
selected_config_path.display()
68+
))?)
69+
} else {
70+
Ok(CommitPattern::default())
71+
}
6872
}

0 commit comments

Comments
 (0)