diff --git a/rust_solid_cassandra/backend/src/main.rs b/rust_solid_cassandra/backend/src/main.rs index dd76601..6fdd578 100644 --- a/rust_solid_cassandra/backend/src/main.rs +++ b/rust_solid_cassandra/backend/src/main.rs @@ -56,8 +56,8 @@ async fn main() -> io::Result<()> { // External ENV params let socket_addr: SocketAddrV4 = format!( "{}:{}", - env::var("WEBSERVER_ADDRESS").unwrap_or(DEFAULT_WEBSERVER_ADDRESS.to_string()), - env::var("WEBSERVER_PORT").unwrap_or(DEFAULT_WEBSERVER_PORT.to_string()) + env::var("WEBSERVER_ADDRESS").unwrap_or_else(|_| DEFAULT_WEBSERVER_ADDRESS.to_string()), + env::var("WEBSERVER_PORT").unwrap_or_else(|_| DEFAULT_WEBSERVER_PORT.to_string()) ) .parse() .expect("A valid socket address. Check your ENV variables!"); diff --git a/rust_solid_cassandra/backend/src/model/todo.rs b/rust_solid_cassandra/backend/src/model/todo.rs index 0a8a707..a722f85 100644 --- a/rust_solid_cassandra/backend/src/model/todo.rs +++ b/rust_solid_cassandra/backend/src/model/todo.rs @@ -33,7 +33,7 @@ impl Todo { } pub fn to_json(&self) -> Result { - Ok(serde_json::to_string(self)?) + serde_json::to_string(self) } pub fn set_user_id(&mut self, user_id: Uuid) { diff --git a/rust_solid_cassandra/backend/src/repo.rs b/rust_solid_cassandra/backend/src/repo.rs index 635f4af..028b3e9 100644 --- a/rust_solid_cassandra/backend/src/repo.rs +++ b/rust_solid_cassandra/backend/src/repo.rs @@ -16,9 +16,9 @@ static DEFAULT_CASSANDRA_SERVER_ADDRESS: &str = "127.0.0.1"; /// Waits for the cassandra database to become available -> then returns a session. pub fn init() -> Session { let keyspace_name = - env::var("CASSANDRA_KEYSPACE_NAME").unwrap_or(DEFAULT_CASSANDRA_KEYSPACE_NAME.to_string()); + env::var("CASSANDRA_KEYSPACE_NAME").unwrap_or_else(|_| DEFAULT_CASSANDRA_KEYSPACE_NAME.to_string()); let cassandra_server_addr = env::var("CASSANDRA_SERVER_ADDRESS") - .unwrap_or(DEFAULT_CASSANDRA_SERVER_ADDRESS.to_string()); + .unwrap_or_else(|_| DEFAULT_CASSANDRA_SERVER_ADDRESS.to_string()); // Definitely set it so other modules can use it env::set_var("CASSANDRA_KEYSPACE_NAME", &keyspace_name); diff --git a/rust_solid_cassandra/backend/src/repo/todo_repository.rs b/rust_solid_cassandra/backend/src/repo/todo_repository.rs index d790a53..795cd69 100644 --- a/rust_solid_cassandra/backend/src/repo/todo_repository.rs +++ b/rust_solid_cassandra/backend/src/repo/todo_repository.rs @@ -89,7 +89,7 @@ impl TodoRepository { self.table )); - let uuid = cassandra_cpp::Uuid::from(user_id.clone()); + let uuid = cassandra_cpp::Uuid::from(*user_id); query.bind_uuid(0, uuid).expect("Bind user_id"); Ok(self @@ -114,8 +114,8 @@ impl TodoRepository { self.table )); - let id_uuid = cassandra_cpp::Uuid::from(id.clone()); - let user_id_uuid = cassandra_cpp::Uuid::from(user_id.clone()); + let id_uuid = cassandra_cpp::Uuid::from(*id); + let user_id_uuid = cassandra_cpp::Uuid::from(*user_id); query.bind_uuid(0, id_uuid).expect("Bind id"); query.bind_uuid(1, user_id_uuid).expect("Bind user_id"); diff --git a/rust_solid_cassandra/backend/src/repo/user_repository.rs b/rust_solid_cassandra/backend/src/repo/user_repository.rs index 1b394f3..fe29969 100644 --- a/rust_solid_cassandra/backend/src/repo/user_repository.rs +++ b/rust_solid_cassandra/backend/src/repo/user_repository.rs @@ -71,7 +71,7 @@ impl UserRepository { } pub fn create(&self, user: &User) -> Result<()> { - if let Some(_) = self.read_by_login(user.login())? { + if self.read_by_login(user.login())?.is_some() { return Err(Box::new(UserAlreadyExistsError)); } @@ -81,7 +81,7 @@ impl UserRepository { self.table )); - let uuid = cassandra_cpp::Uuid::from(user.id().clone()); + let uuid = cassandra_cpp::Uuid::from(*user.id()); query.bind_uuid(0, uuid).expect("Binds the id"); query.bind_string(1, user.login()).expect("Binds the login"); query.bind_string(2, user.hash()).expect("Binds the hash"); @@ -91,15 +91,17 @@ impl UserRepository { } pub fn read(&self, id: &uuid::Uuid) -> Result> { - let uuid = cassandra_cpp::Uuid::from(id.clone()); + let uuid = cassandra_cpp::Uuid::from(*id); // There is no OR in cassandra statements let mut query = stmt!(&format!("SELECT JSON * FROM {} WHERE id = ?", self.table)); query.bind_uuid(0, uuid).expect("Binds id"); - Ok(match self.session.execute(&query).wait()?.first_row() { - Some(row) => Some(User::from_json(&format!("{row}"))), - None => None, - }) + Ok(self + .session + .execute(&query) + .wait()? + .first_row() + .map(|row| User::from_json(&format!("{row}")))) } pub fn read_by_login(&self, login: &str) -> Result> { @@ -108,10 +110,12 @@ impl UserRepository { self.table )); query.bind_string(0, login).expect("Binds login"); - Ok(match self.session.execute(&query).wait()?.first_row() { - Some(row) => Some(User::from_json(&format!("{row}"))), - None => None, - }) + Ok(self + .session + .execute(&query) + .wait()? + .first_row() + .map(|row| User::from_json(&format!("{row}")))) } pub fn read_all(&self) -> Result> { @@ -126,7 +130,7 @@ impl UserRepository { } pub fn update(&self, user: &User) -> Result<()> { - match self.read(&user.id())? { + match self.read(user.id())? { Some(u) => { log::info!("Modifying {u:?} to represent {user:?}"); @@ -136,7 +140,7 @@ impl UserRepository { self.table )); - let uuid = cassandra_cpp::Uuid::from(user.id().clone()); + let uuid = cassandra_cpp::Uuid::from(*user.id()); query.bind_string(0, user.login()).expect("Binds the login"); query.bind_string(1, user.hash()).expect("Binds the hash"); query.bind_string(2, user.salt()).expect("Binds the salt"); @@ -147,5 +151,4 @@ impl UserRepository { None => Err(Box::new(UserDoesNotExistError)), } } - } diff --git a/rust_solid_cassandra/backend/src/routes.rs b/rust_solid_cassandra/backend/src/routes.rs index 0c0b289..0712cd0 100644 --- a/rust_solid_cassandra/backend/src/routes.rs +++ b/rust_solid_cassandra/backend/src/routes.rs @@ -66,7 +66,7 @@ pub async fn post_login( log::debug!("Received {payload:?}"); match repo.read_by_login(&payload.login) { Ok(Some(user)) => { - let hash = sha256::digest(String::from(format!("{}{}", payload.password, user.salt()))); + let hash = sha256::digest(format!("{}{}", payload.password, user.salt())); if hash == user.hash() { log::debug!("User successfully logged in: {payload:?} == {user:?}"); // TODO: Mayb handle more gracefully @@ -75,7 +75,7 @@ pub async fn post_login( // Create answer for frontend let res = ActiveUser { - id: user.id().clone(), + id: *user.id(), login: String::from(user.login()), }; // let res = format!( diff --git a/rust_solid_cassandra/backend/src/routes/todo_routes.rs b/rust_solid_cassandra/backend/src/routes/todo_routes.rs index 8db27b9..7395793 100644 --- a/rust_solid_cassandra/backend/src/routes/todo_routes.rs +++ b/rust_solid_cassandra/backend/src/routes/todo_routes.rs @@ -42,7 +42,7 @@ pub async fn put_todo( let mut todo = todo; // To set user_id from session match get_user_from_session(session) { Some(user) => { - todo.set_user_id(user.id().clone()); + todo.set_user_id(*user.id()); match repo.create(&todo) { Ok(_) => HttpResponse::Ok().finish(), Err(err) => {