diff options
author | Tim Oram <dev@mitmaro.ca> | 2023-08-08 21:49:16 -0230 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2023-08-11 10:24:03 -0230 |
commit | 81d8b6e62f54ca9bae49ee1e9b68b7452d4a87e9 (patch) | |
tree | 6020d39da1482c55646e77b7428d40af7bc4f13f | |
parent | dd31e71f7dfe9716e850f545c690a6c32804dd27 (diff) |
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.
-rw-r--r-- | src/core/src/application.rs | 7 | ||||
-rw-r--r-- | src/todo_file/src/lib.rs | 25 | ||||
-rw-r--r-- | src/todo_file/src/testutil.rs | 4 | ||||
-rw-r--r-- | src/todo_file/src/todo_file_options.rs | 18 |
4 files changed, 42 insertions, 12 deletions
diff --git a/src/core/src/application.rs b/src/core/src/application.rs index 192bdd7..577e496 100644 --- a/src/core/src/application.rs +++ b/src/core/src/application.rs @@ -7,7 +7,7 @@ use git::Repository; use input::{Event, EventHandler, EventReaderFn}; use parking_lot::Mutex; use runtime::{Runtime, ThreadStatuses, Threadable}; -use todo_file::TodoFile; +use todo_file::{TodoFile, TodoFileOptions}; use view::View; use crate::{ @@ -153,7 +153,10 @@ where ModuleProvider: module::ModuleProvider + Send + 'static } fn load_todo_file(filepath: &str, config: &Config) -> Result<TodoFile, Exit> { - let mut todo_file = TodoFile::new(filepath, config.undo_limit, config.git.comment_char.as_str()); + let mut todo_file = TodoFile::new( + filepath, + TodoFileOptions::new(config.undo_limit, config.git.comment_char.as_str()), + ); todo_file .load_file() .map_err(|err| Exit::new(ExitStatus::FileReadError, err.to_string().as_str()))?; diff --git a/src/todo_file/src/lib.rs b/src/todo_file/src/lib.rs index a90fa9a..cb37d0a 100644 --- a/src/todo_file/src/lib.rs +++ b/src/todo_file/src/lib.rs @@ -138,6 +138,7 @@ mod line_parser; mod search; #[cfg(not(tarpaulin_include))] pub mod testutil; +mod todo_file_options; mod utils; use std::{ @@ -149,7 +150,13 @@ use std::{ pub use version_track::Version; -pub use self::{action::Action, edit_content::EditContext, line::Line, search::Search}; +pub use self::{ + action::Action, + edit_content::EditContext, + line::Line, + search::Search, + todo_file_options::TodoFileOptions, +}; use self::{ history::{History, HistoryItem}, utils::{remove_range, swap_range_down, swap_range_up}, @@ -162,11 +169,11 @@ use crate::{ /// Represents a rebase file. #[derive(Debug)] pub struct TodoFile { - comment_char: String, filepath: PathBuf, history: History, is_noop: bool, lines: Vec<Line>, + options: TodoFileOptions, selected_line_index: usize, version: Version, } @@ -175,13 +182,15 @@ impl TodoFile { /// Create a new instance. #[must_use] #[inline] - pub fn new<Path: AsRef<std::path::Path>>(path: Path, undo_limit: u32, comment_char: &str) -> Self { + pub fn new<Path: AsRef<std::path::Path>>(path: Path, options: TodoFileOptions) -> Self { + let history = History::new(options.undo_limit); + Self { - comment_char: String::from(comment_char), filepath: PathBuf::from(path.as_ref()), - history: History::new(undo_limit), - lines: vec![], + history, is_noop: false, + lines: vec![], + options, selected_line_index: 0, version: Version::new(), } @@ -220,7 +229,7 @@ impl TodoFile { })? .lines() .filter_map(|l| { - if l.starts_with(self.comment_char.as_str()) || l.is_empty() { + if l.starts_with(self.options.comment_prefix.as_str()) || l.is_empty() { None } else { @@ -502,7 +511,7 @@ mod tests { .tempfile() .unwrap(); write!(todo_file_path.as_file(), "{}", file_contents.join("\n")).unwrap(); - let mut todo_file = TodoFile::new(todo_file_path.path().to_str().unwrap(), 1, "#"); + let mut todo_file = TodoFile::new(todo_file_path.path().to_str().unwrap(), TodoFileOptions::new(1, "#")); todo_file.load_file().unwrap(); (todo_file, todo_file_path) } diff --git a/src/todo_file/src/testutil.rs b/src/todo_file/src/testutil.rs index 417b619..3f945c8 100644 --- a/src/todo_file/src/testutil.rs +++ b/src/todo_file/src/testutil.rs @@ -7,7 +7,7 @@ use std::{ use tempfile::{Builder, NamedTempFile}; -use crate::{Line, TodoFile}; +use crate::{Line, TodoFile, TodoFileOptions}; /// Context for `with_todo_file` pub struct TodoFileTestContext { @@ -95,7 +95,7 @@ where C: FnOnce(TodoFileTestContext) { .tempfile_in(git_repo_dir.as_path()) .unwrap(); - let mut todo_file = TodoFile::new(git_todo_file.path().to_str().unwrap(), 1, "#"); + let mut todo_file = TodoFile::new(git_todo_file.path().to_str().unwrap(), TodoFileOptions::new(1, "#")); todo_file.set_lines(lines.iter().map(|l| Line::parse(l).unwrap()).collect()); callback(TodoFileTestContext { git_todo_file: RefCell::new(git_todo_file), diff --git a/src/todo_file/src/todo_file_options.rs b/src/todo_file/src/todo_file_options.rs new file mode 100644 index 0000000..8dc9ed4 --- /dev/null +++ b/src/todo_file/src/todo_file_options.rs @@ -0,0 +1,18 @@ +/// Options for `TodoFile` +#[derive(Debug, Clone)] +pub struct TodoFileOptions { + pub(crate) undo_limit: u32, + pub(crate) comment_prefix: String, +} + +impl TodoFileOptions { + /// Create a new instance of `TodoFileOptions` + #[must_use] + #[inline] + pub fn new(undo_limit: u32, comment_prefix: &str) -> Self { + Self { + undo_limit, + comment_prefix: String::from(comment_prefix), + } + } +} |