refactor: clippy warnings
This commit is contained in:
parent
b0e46a5ed6
commit
a5b5af6625
@ -56,8 +56,8 @@ async fn main() -> io::Result<()> {
|
|||||||
// External ENV params
|
// External ENV params
|
||||||
let socket_addr: SocketAddrV4 = format!(
|
let socket_addr: SocketAddrV4 = format!(
|
||||||
"{}:{}",
|
"{}:{}",
|
||||||
env::var("WEBSERVER_ADDRESS").unwrap_or(DEFAULT_WEBSERVER_ADDRESS.to_string()),
|
env::var("WEBSERVER_ADDRESS").unwrap_or_else(|_| DEFAULT_WEBSERVER_ADDRESS.to_string()),
|
||||||
env::var("WEBSERVER_PORT").unwrap_or(DEFAULT_WEBSERVER_PORT.to_string())
|
env::var("WEBSERVER_PORT").unwrap_or_else(|_| DEFAULT_WEBSERVER_PORT.to_string())
|
||||||
)
|
)
|
||||||
.parse()
|
.parse()
|
||||||
.expect("A valid socket address. Check your ENV variables!");
|
.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> {
|
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) {
|
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.
|
/// Waits for the cassandra database to become available -> then returns a session.
|
||||||
pub fn init() -> Session {
|
pub fn init() -> Session {
|
||||||
let keyspace_name =
|
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")
|
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
|
// Definitely set it so other modules can use it
|
||||||
env::set_var("CASSANDRA_KEYSPACE_NAME", &keyspace_name);
|
env::set_var("CASSANDRA_KEYSPACE_NAME", &keyspace_name);
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ impl TodoRepository {
|
|||||||
self.table
|
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");
|
query.bind_uuid(0, uuid).expect("Bind user_id");
|
||||||
|
|
||||||
Ok(self
|
Ok(self
|
||||||
@ -114,8 +114,8 @@ impl TodoRepository {
|
|||||||
self.table
|
self.table
|
||||||
));
|
));
|
||||||
|
|
||||||
let id_uuid = cassandra_cpp::Uuid::from(id.clone());
|
let id_uuid = cassandra_cpp::Uuid::from(*id);
|
||||||
let user_id_uuid = cassandra_cpp::Uuid::from(user_id.clone());
|
let user_id_uuid = cassandra_cpp::Uuid::from(*user_id);
|
||||||
query.bind_uuid(0, id_uuid).expect("Bind id");
|
query.bind_uuid(0, id_uuid).expect("Bind id");
|
||||||
query.bind_uuid(1, user_id_uuid).expect("Bind user_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<()> {
|
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));
|
return Err(Box::new(UserAlreadyExistsError));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ impl UserRepository {
|
|||||||
self.table
|
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_uuid(0, uuid).expect("Binds the id");
|
||||||
query.bind_string(1, user.login()).expect("Binds the login");
|
query.bind_string(1, user.login()).expect("Binds the login");
|
||||||
query.bind_string(2, user.hash()).expect("Binds the hash");
|
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>> {
|
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
|
// There is no OR in cassandra statements
|
||||||
let mut query = stmt!(&format!("SELECT JSON * FROM {} WHERE id = ?", self.table));
|
let mut query = stmt!(&format!("SELECT JSON * FROM {} WHERE id = ?", self.table));
|
||||||
query.bind_uuid(0, uuid).expect("Binds id");
|
query.bind_uuid(0, uuid).expect("Binds id");
|
||||||
|
|
||||||
Ok(match self.session.execute(&query).wait()?.first_row() {
|
Ok(self
|
||||||
Some(row) => Some(User::from_json(&format!("{row}"))),
|
.session
|
||||||
None => None,
|
.execute(&query)
|
||||||
})
|
.wait()?
|
||||||
|
.first_row()
|
||||||
|
.map(|row| User::from_json(&format!("{row}"))))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_by_login(&self, login: &str) -> Result<Option<User>> {
|
pub fn read_by_login(&self, login: &str) -> Result<Option<User>> {
|
||||||
@ -108,10 +110,12 @@ impl UserRepository {
|
|||||||
self.table
|
self.table
|
||||||
));
|
));
|
||||||
query.bind_string(0, login).expect("Binds login");
|
query.bind_string(0, login).expect("Binds login");
|
||||||
Ok(match self.session.execute(&query).wait()?.first_row() {
|
Ok(self
|
||||||
Some(row) => Some(User::from_json(&format!("{row}"))),
|
.session
|
||||||
None => None,
|
.execute(&query)
|
||||||
})
|
.wait()?
|
||||||
|
.first_row()
|
||||||
|
.map(|row| User::from_json(&format!("{row}"))))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_all(&self) -> Result<Vec<User>> {
|
pub fn read_all(&self) -> Result<Vec<User>> {
|
||||||
@ -126,7 +130,7 @@ impl UserRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&self, user: &User) -> Result<()> {
|
pub fn update(&self, user: &User) -> Result<()> {
|
||||||
match self.read(&user.id())? {
|
match self.read(user.id())? {
|
||||||
Some(u) => {
|
Some(u) => {
|
||||||
log::info!("Modifying {u:?} to represent {user:?}");
|
log::info!("Modifying {u:?} to represent {user:?}");
|
||||||
|
|
||||||
@ -136,7 +140,7 @@ impl UserRepository {
|
|||||||
self.table
|
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(0, user.login()).expect("Binds the login");
|
||||||
query.bind_string(1, user.hash()).expect("Binds the hash");
|
query.bind_string(1, user.hash()).expect("Binds the hash");
|
||||||
query.bind_string(2, user.salt()).expect("Binds the salt");
|
query.bind_string(2, user.salt()).expect("Binds the salt");
|
||||||
@ -147,5 +151,4 @@ impl UserRepository {
|
|||||||
None => Err(Box::new(UserDoesNotExistError)),
|
None => Err(Box::new(UserDoesNotExistError)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ pub async fn post_login(
|
|||||||
log::debug!("Received {payload:?}");
|
log::debug!("Received {payload:?}");
|
||||||
match repo.read_by_login(&payload.login) {
|
match repo.read_by_login(&payload.login) {
|
||||||
Ok(Some(user)) => {
|
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() {
|
if hash == user.hash() {
|
||||||
log::debug!("User successfully logged in: {payload:?} == {user:?}");
|
log::debug!("User successfully logged in: {payload:?} == {user:?}");
|
||||||
// TODO: Mayb handle more gracefully
|
// TODO: Mayb handle more gracefully
|
||||||
@ -75,7 +75,7 @@ pub async fn post_login(
|
|||||||
|
|
||||||
// Create answer for frontend
|
// Create answer for frontend
|
||||||
let res = ActiveUser {
|
let res = ActiveUser {
|
||||||
id: user.id().clone(),
|
id: *user.id(),
|
||||||
login: String::from(user.login()),
|
login: String::from(user.login()),
|
||||||
};
|
};
|
||||||
// let res = format!(
|
// let res = format!(
|
||||||
|
@ -42,7 +42,7 @@ pub async fn put_todo(
|
|||||||
let mut todo = todo; // To set user_id from session
|
let mut todo = todo; // To set user_id from session
|
||||||
match get_user_from_session(session) {
|
match get_user_from_session(session) {
|
||||||
Some(user) => {
|
Some(user) => {
|
||||||
todo.set_user_id(user.id().clone());
|
todo.set_user_id(*user.id());
|
||||||
match repo.create(&todo) {
|
match repo.create(&todo) {
|
||||||
Ok(_) => HttpResponse::Ok().finish(),
|
Ok(_) => HttpResponse::Ok().finish(),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user