Skip to content

Commit

Permalink
slightly better practices?
Browse files Browse the repository at this point in the history
  • Loading branch information
jplsek committed Jul 5, 2024
1 parent 59ea461 commit 7e8b9cd
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 76 deletions.
18 changes: 7 additions & 11 deletions src/bin/git-add-coauthor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ struct Cli {
}

trait Add {
fn add(&self, initials: String, name: String, email: String) -> String;
fn add(&self, initials: &str, name: &str, email: &str) -> String;
}

impl<T: FileActions, U: ExitWithError> Add for GitMob<T, U> {
fn add(&self, initials: String, name: String, email: String) -> String {
fn add(&self, initials: &str, name: &str, email: &str) -> String {
let coauthors_path = self.get_coauthors_path();
let coauthors_path = coauthors_path.display();

let mut coauthors = self.get_all_coauthors();
coauthors.insert(
initials,
initials.to_string(),
Author {
name: name.clone(),
email,
name: name.to_string(),
email: email.to_string(),
},
);

Expand All @@ -43,7 +43,7 @@ fn main() {

let gm = GitMob::default();

println!("{}", gm.add(opts.initials, opts.name, opts.email));
println!("{}", gm.add(&opts.initials, &opts.name, &opts.email));
}

#[cfg(test)]
Expand Down Expand Up @@ -73,11 +73,7 @@ mod test {
"A B has been added to the {} file",
coauthors_path.display()
),
gm.add(
String::from("ab"),
String::from("A B"),
String::from("[email protected]"),
)
gm.add(&"ab", &"A B", &"[email protected]")
);
assert_eq!(expected_coauthors, gm.get_all_coauthors());
}
Expand Down
10 changes: 5 additions & 5 deletions src/bin/git-delete-coauthor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ struct Cli {
}

trait Delete {
fn delete(&self, initials: Vec<String>) -> String;
fn delete(&self, initials: &[String]) -> String;
}

impl<T: FileActions, U: ExitWithError> Delete for GitMob<T, U> {
fn delete(&self, initials: Vec<String>) -> String {
fn delete(&self, initials: &[String]) -> String {
let coauthors_path = self.get_coauthors_path();
let coauthors_path = coauthors_path.display();

Expand All @@ -37,7 +37,7 @@ fn main() {

let gm = GitMob::default();

print!("{}", gm.delete(opts.initials));
print!("{}", gm.delete(&opts.initials));
}

#[cfg(test)]
Expand Down Expand Up @@ -70,7 +70,7 @@ mod test {
});

gm.file_actions
.write(&coauthors_path, coauthors.to_string())
.write(&coauthors_path, &coauthors.to_string())
.unwrap();

let mut expected_coauthors = LinkedHashMap::new();
Expand All @@ -88,7 +88,7 @@ mod test {
coauthors_path.display(),
coauthors_path.display()
),
gm.delete(vec![String::from("cd"), String::from("ef")])
gm.delete(&[String::from("cd"), String::from("ef")])
);
assert_eq!(expected_coauthors, gm.get_all_coauthors());
}
Expand Down
24 changes: 12 additions & 12 deletions src/bin/git-edit-coauthor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ struct Cli {
}

trait Edit {
fn edit(&self, initials: String, name: Option<String>, email: Option<String>) -> String;
fn edit(&self, initials: &str, name: Option<String>, email: Option<String>) -> String;
}

impl<T: FileActions, U: ExitWithError> Edit for GitMob<T, U> {
fn edit(&self, initials: String, name: Option<String>, email: Option<String>) -> String {
fn edit(&self, initials: &str, name: Option<String>, email: Option<String>) -> String {
let coauthors_path = self.get_coauthors_path();

let mut coauthors = self.get_all_coauthors();
let coauthor = coauthors.get_mut(&initials);
let coauthor = coauthors.get_mut(initials);
match coauthor {
Some(coauthor) => {
if let Some(name) = name {
Expand All @@ -36,7 +36,7 @@ impl<T: FileActions, U: ExitWithError> Edit for GitMob<T, U> {
}
}
None => {
self.exit_with_error.message(format!(
self.exit_with_error.message(&format!(
"Author with initials \"{}\" not found in \"{}\"!",
initials,
coauthors_path.display()
Expand All @@ -55,7 +55,7 @@ fn main() {

let gm = GitMob::default();

println!("{}", gm.edit(opts.initials, opts.name, opts.email));
println!("{}", gm.edit(&opts.initials, opts.name, opts.email));
}

#[cfg(test)]
Expand Down Expand Up @@ -83,7 +83,7 @@ mod test {
});

gm.file_actions
.write(&gm.get_coauthors_path(), coauthors.to_string())
.write(&gm.get_coauthors_path(), &coauthors.to_string())
.unwrap();

let mut expected_coauthors = LinkedHashMap::new();
Expand All @@ -105,7 +105,7 @@ mod test {
assert_eq!(
"ab has been updated",
gm.edit(
String::from("ab"),
&String::from("ab"),
Some(String::from("C D")),
Some(String::from("[email protected]")),
)
Expand All @@ -127,7 +127,7 @@ mod test {
});

gm.file_actions
.write(&gm.get_coauthors_path(), coauthors.to_string())
.write(&gm.get_coauthors_path(), &coauthors.to_string())
.unwrap();

let mut expected_coauthors = LinkedHashMap::new();
Expand All @@ -141,7 +141,7 @@ mod test {

assert_eq!(
"ab has been updated",
gm.edit(String::from("ab"), Some(String::from("C D")), None)
gm.edit(&String::from("ab"), Some(String::from("C D")), None)
);
assert_eq!(expected_coauthors, gm.get_all_coauthors());
}
Expand All @@ -160,7 +160,7 @@ mod test {
});

gm.file_actions
.write(&gm.get_coauthors_path(), coauthors.to_string())
.write(&gm.get_coauthors_path(), &coauthors.to_string())
.unwrap();

let mut expected_coauthors = LinkedHashMap::new();
Expand All @@ -175,7 +175,7 @@ mod test {
assert_eq!(
"ab has been updated",
gm.edit(
String::from("ab"),
&String::from("ab"),
None,
Some(String::from("[email protected]"))
)
Expand All @@ -187,6 +187,6 @@ mod test {
#[should_panic]
fn test_edit_author_who_does_not_exist() {
let gm = get_git_mob();
gm.edit(String::from("ef"), None, None);
gm.edit(&String::from("ef"), None, None);
}
}
2 changes: 1 addition & 1 deletion src/bin/git-edit-coauthors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<T: FileActions, U: ExitWithError> Edit for GitMob<T, U> {
}
});
self.file_actions
.write(&coauthors_path, to_string_pretty(&s).unwrap())
.write(&coauthors_path, &to_string_pretty(&s).unwrap())
.unwrap();
}

Expand Down
6 changes: 3 additions & 3 deletions src/bin/git-mob-print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod test {
let authors =
"\n\nCo-authored-by: A B <[email protected]>\nCo-authored-by: C D <[email protected]>\n";

gm.write_gitmessage(vec![String::from("ab"), String::from("cd")]);
gm.write_gitmessage(&[String::from("ab"), String::from("cd")]);

assert_eq!(authors, gm.print());
}
Expand All @@ -58,11 +58,11 @@ mod test {
fn test_print_initials() {
let gm = get_git_mob();

gm.write_gitmessage(vec![]);
gm.write_gitmessage(&[]);

assert_eq!("\n", gm.print_initials());

gm.write_gitmessage(vec![String::from("ab"), String::from("cd")]);
gm.write_gitmessage(&[String::from("ab"), String::from("cd")]);

assert_eq!("ab,cd\n", gm.print_initials());
}
Expand Down
19 changes: 8 additions & 11 deletions src/bin/git-mob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Cli {
}

trait Mob {
fn mob(&self, users: Vec<String>) -> String;
fn mob(&self, users: &[String]) -> String;
fn list(&self) -> String;
}

Expand All @@ -31,7 +31,7 @@ impl<T: FileActions, U: ExitWithError> Mob for GitMob<T, U> {
format!("{}\n", initials.join("\n"))
}

fn mob(&self, initials: Vec<String>) -> String {
fn mob(&self, initials: &[String]) -> String {
// make sure to not accidentally "solo"
if initials.is_empty() {
return self.get_formatted_gitmessage();
Expand All @@ -50,7 +50,7 @@ fn main() {
if opts.list {
print!("{}", gm.list());
} else {
println!("{}", gm.mob(opts.initials));
println!("{}", gm.mob(&opts.initials));
}
}

Expand All @@ -68,15 +68,12 @@ mod test {

assert_eq!(
format!("{}\n{}", gm.get_git_user(), authors),
gm.mob(vec![String::from("ab"), String::from("cd")])
gm.mob(&[String::from("ab"), String::from("cd")])
);
assert_eq!(format!("\n\n{}", authors), gm.get_gitmessage());

// make sure empty vec doesn't reset gitmessage file
assert_eq!(
format!("{}\n{}", gm.get_git_user(), authors),
gm.mob(vec![])
);
assert_eq!(format!("{}\n{}", gm.get_git_user(), authors), gm.mob(&[]));
assert_eq!(format!("\n\n{}", authors), gm.get_gitmessage());
}

Expand All @@ -94,7 +91,7 @@ mod test {
#[should_panic]
fn test_mob_empty_authors() {
let gm = get_git_mob();
gm.mob(vec![String::from("ef")]);
gm.mob(&[String::from("ef")]);
}

#[test]
Expand All @@ -105,14 +102,14 @@ mod test {
gm.file_actions
.write(
&gm.get_coauthors_path(),
json!({
&json!({
"coauthors": {
}
})
.to_string(),
)
.unwrap();

gm.mob(vec![String::from("ab")]);
gm.mob(&[String::from("ab")]);
}
}
2 changes: 1 addition & 1 deletion src/bin/git-solo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait Solo {

impl<T: FileActions, U: ExitWithError> Solo for GitMob<T, U> {
fn solo(&self) -> String {
self.write_gitmessage(vec![]);
self.write_gitmessage(&[]);
self.get_formatted_gitmessage()
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/exit_with_error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::process::exit;

pub trait ExitWithError {
fn message<S: AsRef<str>>(&self, message: S) -> !;
fn message(&self, message: &str) -> !;
}

pub struct ExitWithErrorImpl();

impl ExitWithError for ExitWithErrorImpl {
fn message<S: AsRef<str>>(&self, message: S) -> ! {
println!("{}", message.as_ref());
fn message(&self, message: &str) -> ! {
println!("{}", message);
exit(1);
}
}
19 changes: 8 additions & 11 deletions src/file_actions.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
use std::error::Error;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

pub trait FileActions {
fn write<S: AsRef<str>>(&self, path: &Path, s: S) -> Result<(), Box<dyn Error>>;
fn read(&self, path: &Path) -> Result<String, Box<dyn Error>>;
fn write(&self, path: &Path, s: &str) -> Result<(), String>;
fn read(&self, path: &Path) -> Result<String, String>;
}

pub struct FileSystemActions();

impl FileActions for FileSystemActions {
fn write<S: AsRef<str>>(&self, path: &Path, s: S) -> Result<(), Box<dyn Error>> {
fn write(&self, path: &Path, s: &str) -> Result<(), String> {
let path_display = path.display();
if let Err(why) = fs::write(path, s.as_ref().as_bytes()) {
return Err(Box::from(format!(
"couldn't write to {path_display}: {why}"
)));
if let Err(why) = fs::write(path, s.as_bytes()) {
return Err(format!("couldn't write to {path_display}: {why}"));
}
Ok(())
}

fn read(&self, path: &Path) -> Result<String, Box<dyn Error>> {
fn read(&self, path: &Path) -> Result<String, String> {
let path_display = path.display();
let mut file = match File::open(path) {
Err(why) => return Err(Box::from(format!("couldn't open {path_display}: {why}"))),
Err(why) => return Err(format!("couldn't open {path_display}: {why}")),
Ok(file) => file,
};

let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => Err(Box::from(format!("couldn't read {path_display}: {why}"))),
Err(why) => Err(format!("couldn't read {path_display}: {why}")),
Ok(_) => Ok(s),
}
}
Expand Down
Loading

0 comments on commit 7e8b9cd

Please sign in to comment.