Skip to content

Commit 81d8b6e

Browse files
committed
Refactor TodoFile to use an options struct
The TodoFile struct was using discrete options in the new function, but this is not sustainable as new options are added. This adds a new TodoFileOptions struct, that contains the TodoFile options.
1 parent dd31e71 commit 81d8b6e

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

src/core/src/application.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use git::Repository;
77
use input::{Event, EventHandler, EventReaderFn};
88
use parking_lot::Mutex;
99
use runtime::{Runtime, ThreadStatuses, Threadable};
10-
use todo_file::TodoFile;
10+
use todo_file::{TodoFile, TodoFileOptions};
1111
use view::View;
1212

1313
use crate::{
@@ -153,7 +153,10 @@ where ModuleProvider: module::ModuleProvider + Send + 'static
153153
}
154154

155155
fn load_todo_file(filepath: &str, config: &Config) -> Result<TodoFile, Exit> {
156-
let mut todo_file = TodoFile::new(filepath, config.undo_limit, config.git.comment_char.as_str());
156+
let mut todo_file = TodoFile::new(
157+
filepath,
158+
TodoFileOptions::new(config.undo_limit, config.git.comment_char.as_str()),
159+
);
157160
todo_file
158161
.load_file()
159162
.map_err(|err| Exit::new(ExitStatus::FileReadError, err.to_string().as_str()))?;

src/todo_file/src/lib.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ mod line_parser;
138138
mod search;
139139
#[cfg(not(tarpaulin_include))]
140140
pub mod testutil;
141+
mod todo_file_options;
141142
mod utils;
142143

143144
use std::{
@@ -149,7 +150,13 @@ use std::{
149150

150151
pub use version_track::Version;
151152

152-
pub use self::{action::Action, edit_content::EditContext, line::Line, search::Search};
153+
pub use self::{
154+
action::Action,
155+
edit_content::EditContext,
156+
line::Line,
157+
search::Search,
158+
todo_file_options::TodoFileOptions,
159+
};
153160
use self::{
154161
history::{History, HistoryItem},
155162
utils::{remove_range, swap_range_down, swap_range_up},
@@ -162,11 +169,11 @@ use crate::{
162169
/// Represents a rebase file.
163170
#[derive(Debug)]
164171
pub struct TodoFile {
165-
comment_char: String,
166172
filepath: PathBuf,
167173
history: History,
168174
is_noop: bool,
169175
lines: Vec<Line>,
176+
options: TodoFileOptions,
170177
selected_line_index: usize,
171178
version: Version,
172179
}
@@ -175,13 +182,15 @@ impl TodoFile {
175182
/// Create a new instance.
176183
#[must_use]
177184
#[inline]
178-
pub fn new<Path: AsRef<std::path::Path>>(path: Path, undo_limit: u32, comment_char: &str) -> Self {
185+
pub fn new<Path: AsRef<std::path::Path>>(path: Path, options: TodoFileOptions) -> Self {
186+
let history = History::new(options.undo_limit);
187+
179188
Self {
180-
comment_char: String::from(comment_char),
181189
filepath: PathBuf::from(path.as_ref()),
182-
history: History::new(undo_limit),
183-
lines: vec![],
190+
history,
184191
is_noop: false,
192+
lines: vec![],
193+
options,
185194
selected_line_index: 0,
186195
version: Version::new(),
187196
}
@@ -220,7 +229,7 @@ impl TodoFile {
220229
})?
221230
.lines()
222231
.filter_map(|l| {
223-
if l.starts_with(self.comment_char.as_str()) || l.is_empty() {
232+
if l.starts_with(self.options.comment_prefix.as_str()) || l.is_empty() {
224233
None
225234
}
226235
else {
@@ -502,7 +511,7 @@ mod tests {
502511
.tempfile()
503512
.unwrap();
504513
write!(todo_file_path.as_file(), "{}", file_contents.join("\n")).unwrap();
505-
let mut todo_file = TodoFile::new(todo_file_path.path().to_str().unwrap(), 1, "#");
514+
let mut todo_file = TodoFile::new(todo_file_path.path().to_str().unwrap(), TodoFileOptions::new(1, "#"));
506515
todo_file.load_file().unwrap();
507516
(todo_file, todo_file_path)
508517
}

src/todo_file/src/testutil.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77

88
use tempfile::{Builder, NamedTempFile};
99

10-
use crate::{Line, TodoFile};
10+
use crate::{Line, TodoFile, TodoFileOptions};
1111

1212
/// Context for `with_todo_file`
1313
pub struct TodoFileTestContext {
@@ -95,7 +95,7 @@ where C: FnOnce(TodoFileTestContext) {
9595
.tempfile_in(git_repo_dir.as_path())
9696
.unwrap();
9797

98-
let mut todo_file = TodoFile::new(git_todo_file.path().to_str().unwrap(), 1, "#");
98+
let mut todo_file = TodoFile::new(git_todo_file.path().to_str().unwrap(), TodoFileOptions::new(1, "#"));
9999
todo_file.set_lines(lines.iter().map(|l| Line::parse(l).unwrap()).collect());
100100
callback(TodoFileTestContext {
101101
git_todo_file: RefCell::new(git_todo_file),
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// Options for `TodoFile`
2+
#[derive(Debug, Clone)]
3+
pub struct TodoFileOptions {
4+
pub(crate) undo_limit: u32,
5+
pub(crate) comment_prefix: String,
6+
}
7+
8+
impl TodoFileOptions {
9+
/// Create a new instance of `TodoFileOptions`
10+
#[must_use]
11+
#[inline]
12+
pub fn new(undo_limit: u32, comment_prefix: &str) -> Self {
13+
Self {
14+
undo_limit,
15+
comment_prefix: String::from(comment_prefix),
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)