refactor: clippy warnings

main
phga 2 years ago
parent b0e46a5ed6
commit a5b5af6625
Signed by: phga
GPG Key ID: 5249548AA705F019

@ -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!");

@ -33,7 +33,7 @@ impl Todo {
}
pub fn to_json(&self) -> Result<String, serde_json::Error> {
Ok(serde_json::to_string(self)?)
serde_json::to_string(self)
}
pub fn set_user_id(&mut self, user_id: Uuid) {

@ -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);

@ -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");

@ -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<Option<User>> {
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<Option<User>> {
@ -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<Vec<User>> {
@ -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)),
}
}
}

@ -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!(

@ -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) => {

Loading…
Cancel
Save