From 117ee9935e7682067634137a6408700fa4e88c4b Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Sun, 18 Jul 2021 01:33:22 +0100 Subject: [PATCH] Run 'cargo fmt' --- src/auth.rs | 109 +++++++++++++++++++++++++++++++++++-------------- src/file_io.rs | 27 ++++++------ src/message.rs | 4 +- 3 files changed, 95 insertions(+), 45 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 4d3b80c..461d2dd 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,9 +1,8 @@ extern crate log; +use crate::file_io::{append_json, read_json, write_json}; use crate::user::User; -use crate::file_io::{read_json, append_json, write_json}; extern crate sha1; - #[get("/")] pub fn index() -> &'static str { "API Info: @@ -25,20 +24,21 @@ pub fn index() -> &'static str { #[post("/api/register///")] pub fn register_user(name: String, pin: i32, pronouns: String) -> String { let mut users: Vec = read_json(); // Create an array of users out of parsed json - for i in &users { // loop through elements of the vector + for i in &users { + // loop through elements of the vector if i.name == name.to_lowercase() { warn!("Cannot create user {}! User is already in system.", i.name); return "User already exists!".to_string(); }; - }; + } let pin_hashed = sha1::Sha1::from(&pin.to_string()).digest().to_string(); // hash the pin - users.push( User { + users.push(User { name: name.to_string().to_lowercase(), pin_hashed: pin_hashed, pronouns: pronouns.to_string().to_lowercase(), - session_token: "NULL".to_string() + session_token: "NULL".to_string(), }); // append the user to the vec // append to the json file @@ -47,8 +47,16 @@ pub fn register_user(name: String, pin: i32, pronouns: String) -> String { Ok(()) => info!("Succesfully appended to json"), }; - info!("succesfully created user {} with pin hash {}", users[users.len()-1].name.to_string(), users[users.len()-1].pin_hashed); - return format!("User {} registered with pin hash: {}", users[users.len()-1].name.to_string().to_lowercase(), users[users.len()-1].pin_hashed); + info!( + "succesfully created user {} with pin hash {}", + users[users.len() - 1].name.to_string(), + users[users.len() - 1].pin_hashed + ); + return format!( + "User {} registered with pin hash: {}", + users[users.len() - 1].name.to_string().to_lowercase(), + users[users.len() - 1].pin_hashed + ); } // Check if pin matches user @@ -56,7 +64,8 @@ pub fn register_user(name: String, pin: i32, pronouns: String) -> String { pub fn check_pin(name: String, pin: i32) -> String { let users: Vec = read_json(); let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string(); - for i in &users { // loop through the vector + for i in &users { + // loop through the vector if i.name == name.to_lowercase() { if i.pin_hashed == hashed_pin_input { info!("pin correct for user {}", i.name); @@ -66,8 +75,11 @@ pub fn check_pin(name: String, pin: i32) -> String { return "Incorrect pin".to_string(); }; }; - }; - warn!("cannot check pin for user {} as they do not exist", name.to_string().to_lowercase()); + } + warn!( + "cannot check pin for user {} as they do not exist", + name.to_string().to_lowercase() + ); return format!("User {} does not exist.", name.to_string().to_lowercase()); } @@ -80,35 +92,60 @@ pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> String // Loop over elements in vector for i in 0..users.len() { - if users[i].name == name.to_lowercase() { // make sure name exists - if users[i].pin_hashed == hashed_pin_input { // check if pin is correct + if users[i].name == name.to_lowercase() { + // make sure name exists + if users[i].pin_hashed == hashed_pin_input { + // check if pin is correct // Check wether to change name or name+pin if users[i].name == new_name.to_lowercase() { // check if new name already exists - users[i].pin_hashed = sha1::Sha1::from(&new_pin.to_string()).digest().to_string(); - match write_json(&users) { - Err(why) => panic!("Cannot write to json! {}", why), - Ok(()) => info!("succesfully wrote to json file"), - } - info!("Changed pin of {}", name.to_string().to_lowercase()); - return format!("User {}'s new pin hash is {}.", name.to_string().to_lowercase(), users[i].pin_hashed); + users[i].pin_hashed = + sha1::Sha1::from(&new_pin.to_string()).digest().to_string(); + match write_json(&users) { + Err(why) => panic!("Cannot write to json! {}", why), + Ok(()) => info!("succesfully wrote to json file"), + } + info!("Changed pin of {}", name.to_string().to_lowercase()); + return format!( + "User {}'s new pin hash is {}.", + name.to_string().to_lowercase(), + users[i].pin_hashed + ); } else { // check if new name already exists for n in &users { if n.name == new_name.to_lowercase() { - warn!("Could not change name of {} to {}, as new name is already taken.", name.to_lowercase(), new_name.to_lowercase()); - return format!("New name {} is already taken!", new_name.to_lowercase()); + warn!( + "Could not change name of {} to {}, as new name is already taken.", + name.to_lowercase(), + new_name.to_lowercase() + ); + return format!( + "New name {} is already taken!", + new_name.to_lowercase() + ); } } users[i].name = new_name.to_string().to_lowercase(); - users[i].pin_hashed = sha1::Sha1::from(&new_pin.to_string()).digest().to_string(); - + users[i].pin_hashed = + sha1::Sha1::from(&new_pin.to_string()).digest().to_string(); + match write_json(&users) { Err(why) => panic!("couldn't write to json file! {}", why), Ok(()) => info!("succesfully wrote to json file"), } - info!("Changed name of {} to {}. New pin hash is {}", name.to_string(), users[i].name.to_string(), users[i].pin_hashed.to_string()); - return format!("User previously known as {} is now {}. Pin hash, if different, is {}", name.to_string(), users[i].name.to_string(), users[i].pin_hashed.to_string()); + info!( + "Changed name of {} to {}. New pin hash is {}", + name.to_string(), + users[i].name.to_string(), + users[i].pin_hashed.to_string() + ); + return format!( + "User previously known as {} is now {}. Pin hash, if different, is {}", + name.to_string(), + users[i].name.to_string(), + users[i].pin_hashed.to_string() + ); } } else { warn!("Incorrect pin given for user {}!", name.to_string()); @@ -116,14 +153,20 @@ pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> String } } } - warn!("User {} not found, could not change pin and/or name.", name.to_string()); + warn!( + "User {} not found, could not change pin and/or name.", + name.to_string() + ); return format!("User {} not found.", name.to_string()); } #[get("/api/users/")] pub fn get_user(name: String) -> String { let users: Vec = read_json(); - let found_user = users.iter().filter(|u| u.name == name.to_lowercase()).next(); + let found_user = users + .iter() + .filter(|u| u.name == name.to_lowercase()) + .next(); match found_user { Some(user) => format!("User {}", &user.name), @@ -135,7 +178,10 @@ pub fn get_user(name: String) -> String { #[get("/api/about/name/")] pub fn get_user_name(name: String) -> String { let users: Vec = read_json(); - let found_user = users.iter().filter(|u| u.name == name.to_lowercase()).next(); + let found_user = users + .iter() + .filter(|u| u.name == name.to_lowercase()) + .next(); match found_user { Some(user) => user.name.to_string(), @@ -146,7 +192,10 @@ pub fn get_user_name(name: String) -> String { #[get("/api/about/pronouns/")] pub fn get_user_pronouns(name: String) -> String { let users: Vec = read_json(); - let found_user = users.iter().filter(|u| u.name == name.to_lowercase()).next(); + let found_user = users + .iter() + .filter(|u| u.name == name.to_lowercase()) + .next(); match found_user { Some(user) => user.pronouns.to_string(), diff --git a/src/file_io.rs b/src/file_io.rs index 0efecdc..94dacca 100644 --- a/src/file_io.rs +++ b/src/file_io.rs @@ -7,7 +7,9 @@ use crate::user::User; use serde_json::Result; fn read_lines

(filename: P) -> io::Result>> -where P: AsRef, { +where + P: AsRef, +{ let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) } @@ -35,22 +37,23 @@ pub fn read_json() -> Vec { } // Function to append the last value of the users vector to the file -pub fn append_json(users_list: &Vec) -> Result<()> { +pub fn append_json(users_list: &Vec) -> Result<()> { // Create a file to write to let path = Path::new("users.json"); let display = path.display(); let mut file = match OpenOptions::new() - .write(true) - .create(true) - .append(true) - .open(&path){ + .write(true) + .create(true) + .append(true) + .open(&path) + { Err(why) => panic!("couldn't create {}: {}", display, why), Ok(file) => file, }; // Serialize the last user value - let users_json = serde_json::to_string(&users_list[users_list.len()-1])?; + let users_json = serde_json::to_string(&users_list[users_list.len() - 1])?; // Write to the file match file.write_all(users_json.as_bytes()) { @@ -71,19 +74,17 @@ pub fn write_json(users_list: &Vec) -> Result<()> { let path = Path::new("users.json"); let display = path.display(); - let mut file = match OpenOptions::new() - .write(true) - .create(true) - .open(&path){ + let mut file = match OpenOptions::new().write(true).create(true).open(&path) { Err(why) => panic!("couldn't create {}: {}", display, why), Ok(file) => file, }; - + let mut users_json = String::new(); for i in 0..users_list.len() { // Serialize the users users_json += &serde_json::to_string(&users_list[i])?; - if i != users_list.len()-1 { // don't append newline if it's the last element + if i != users_list.len() - 1 { + // don't append newline if it's the last element users_json += "\n"; } } diff --git a/src/message.rs b/src/message.rs index 9020842..45179ac 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,7 +1,7 @@ -use uuid::Uuid; -use chrono::prelude::*; use crate::user::User; +use chrono::prelude::*; use serde::{Deserialize, Serialize}; +use uuid::Uuid; #[derive(Deserialize, Serialize)] pub struct Message<'r> {