Skip to content

Commit

Permalink
refactor: fix lint warning
Browse files Browse the repository at this point in the history
-I'm going to take Clippy's advise and not needlessly pass by value
- I used build and unwrap though it's not doing much...
  - and I don't know how to catch it

However, it does break the pattern a bit.
  • Loading branch information
jasonribble committed Jul 24, 2024
1 parent a266539 commit b83784c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ mod tests {
.return_once(|_| Ok(()));

let edits =
models::ContactBuilder::new(1).set_email(Some("new_email@example.com".to_string()));
models::ContactBuilder::new(1).set_email(&Some("new_email@example.com".to_string()));

let result = mock_contact_repo.update(edits).await;

Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ async fn main() -> anyhow::Result<()> {
}
Commands::Edit(value) => {
let contact = ContactBuilder::new(value.id)
.set_email(value.email.clone())
.set_email(&value.email)
.set_first_name(value.first_name.clone())
.set_last_name(value.last_name.clone())
.set_phone_number(value.phone_number.clone())
.set_display_name(value.display_name.clone());
.set_phone_number(&value.phone_number)
.set_display_name(value.display_name.clone())
.build()
.unwrap();

let _ = contact_repo.update(contact).await;
}
Commands::Show => {
let contacts = contact_repo.get_all().await?;

println!("{contacts:?}")
println!("{contacts:?}");
}
Commands::Get(value) => {
let id = value.id;
Expand Down
12 changes: 6 additions & 6 deletions src/models/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Builder {
self
}

pub fn set_email(mut self, email: Option<String>) -> Self {
pub fn set_email(mut self, email: &Option<String>) -> Self {
let email: &str = email.as_deref().unwrap_or("");

if utils::is_not_valid_email(email) {
Expand All @@ -80,7 +80,7 @@ impl Builder {
self
}

pub fn set_phone_number(mut self, phone_number: Option<String>) -> Self {
pub fn set_phone_number(mut self, phone_number: &Option<String>) -> Self {
let phone_number: &str = phone_number.as_deref().unwrap_or("");

if utils::is_not_valid_phone_number(phone_number) {
Expand Down Expand Up @@ -149,7 +149,7 @@ mod tests {
fn test_contact_update_builder() {
let edits = Builder::new(1)
.set_display_name(Some("Nickname".to_string()))
.set_phone_number(Some("123-233-1221".to_string()))
.set_phone_number(&Some("123-233-1221".to_string()))
.build()
.unwrap();

Expand All @@ -166,7 +166,7 @@ mod tests {
let edits = Builder::new(2)
.set_first_name(Some("Mary".to_string()))
.set_last_name(Some("Smith".to_string()))
.set_email(Some("new@email.com".to_string()))
.set_email(&Some("new@email.com".to_string()))
.build()
.unwrap();

Expand Down Expand Up @@ -200,15 +200,15 @@ mod tests {
#[test]
fn test_invalid_email_builder() {
let result = Builder::new(1)
.set_email(Some("invalid@example".to_string()))
.set_email(&Some("invalid@example".to_string()))
.build();
assert!(result.is_err());
}

#[test]
fn test_invalid_builder_phone_number() {
let result = Builder::new(1)
.set_phone_number(Some("invalid number".to_string()))
.set_phone_number(&Some("invalid number".to_string()))
.build();
assert!(result.is_err());
}
Expand Down

0 comments on commit b83784c

Please sign in to comment.