Skip to content

Commit

Permalink
[♻️ refactor] Remove Box<dyn> and start using impl
Browse files Browse the repository at this point in the history
Using impl in function parameters of commands is more efficient and more applicable in those conditions.
  • Loading branch information
iamlucasvieira committed Jan 25, 2024
1 parent a845c54 commit 7882f29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl AppConfig {
}
}

pub fn list(d: &Box<dyn data::DataFile>) -> Result<()> {
pub fn list(d: &impl data::DataFile) -> Result<()> {
d.sorted_ids().iter().for_each(|id| {
println!("{}: {}", id, d.get(*id).unwrap());
});
Expand Down Expand Up @@ -73,15 +73,15 @@ pub fn init(app_config: &AppConfig) -> Result<()> {
Ok(())
}

pub fn add(d: &mut Box<dyn data::DataFile>, app_config: &AppConfig, content: String) -> Result<()> {
pub fn add(d: &mut impl data::DataFile, app_config: &AppConfig, content: String) -> Result<()> {
let id = d.sorted_ids().last().unwrap_or(&0) + 1;
d.add(id, &content)?;
let lines = d.as_string()?;
data::write_file(&app_config.data_file_path(), &lines)?;
Ok(())
}

pub fn remove(d: &mut Box<dyn data::DataFile>, app_config: &AppConfig, id: u32) -> Result<()> {
pub fn remove(d: &mut impl data::DataFile, app_config: &AppConfig, id: u32) -> Result<()> {
d.remove(id)?;
let lines = d.as_string()?;
data::write_file(&app_config.data_file_path(), &lines)?;
Expand Down Expand Up @@ -138,14 +138,14 @@ mod tests {

#[test]
fn test_list() {
let mut box_memo_data: Box<dyn data::DataFile> = Box::new(MemoData::new());
assert_eq!(list(&mut box_memo_data).is_ok(), true);
let memo_data = MemoData::new();
assert_eq!(list(&memo_data).is_ok(), true);
}

#[test]
fn test_list_empty() {
let mut box_memo_data: Box<dyn data::DataFile> = Box::new(MemoData::new());
assert_eq!(list(&mut box_memo_data).is_ok(), true);
let memo_data = MemoData::new();
assert_eq!(list(&memo_data).is_ok(), true);
}

#[test]
Expand Down Expand Up @@ -183,10 +183,10 @@ mod tests {
// Create file
std::fs::File::create(&app_config.data_file_path()).unwrap();

let mut box_memo_data: Box<dyn data::DataFile> = Box::new(MemoData::new());
let mut memo_data = MemoData::new();
let content = "test".to_string();

assert_eq!(add(&mut box_memo_data, &app_config, content).is_ok(), true);
assert_eq!(add(&mut memo_data, &app_config, content).is_ok(), true);
}

#[test]
Expand All @@ -198,11 +198,10 @@ mod tests {
// Create file
std::fs::File::create(&app_config.data_file_path()).unwrap();

let mut d = MemoData::new();
let mut memo_data = MemoData::new();
let content = "test".to_string();
data::DataFile::add(&mut d, 1, &content).unwrap();
data::DataFile::add(&mut memo_data, 1, &content).unwrap();

let mut box_memo_data: Box<dyn data::DataFile> = Box::new(d);
assert_eq!(remove(&mut box_memo_data, &app_config, 1).is_ok(), true);
assert_eq!(remove(&mut memo_data, &app_config, 1).is_ok(), true);
}
}
8 changes: 3 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,24 @@ fn main() {
return; // Optionally exit if the data file cannot be loaded
}

let mut box_memo_data: Box<dyn data::DataFile> = Box::new(memo_data);

// Handle message
let has_message = cli.message.is_some();
if let Some(message) = cli.message {
if let Err(e) = app::add(&mut box_memo_data, &app_config, message.join(" ")) {
if let Err(e) = app::add(&mut memo_data, &app_config, message.join(" ")) {
eprintln!("Error: {}", e);
}
}

// Handle remove
if let Some(id) = cli.remove {
if let Err(e) = app::remove(&mut box_memo_data, &app_config, id) {
if let Err(e) = app::remove(&mut memo_data, &app_config, id) {
eprintln!("Error: {}", e);
}
}

// Handle list
if cli.list || !has_message {
if let Err(e) = app::list(&mut box_memo_data) {
if let Err(e) = app::list(&memo_data) {
eprintln!("Error: {}", e);
}
}
Expand Down

0 comments on commit 7882f29

Please sign in to comment.