diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-09-04 13:27:17 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-09-04 13:27:17 -0400 |
commit | e2c857a86b36c4b569698dd02323aae324f5409d (patch) | |
tree | d46320f942e9a6cbf5ba50e567cbb42f7826918a | |
parent | 8b3c06f839f14eca6d76da89ffde9fb270fc7b10 (diff) | |
download | clinte-e2c857a86b36c4b569698dd02323aae324f5409d.tar.gz |
getting user name at single location
-rw-r--r-- | src/logging.rs | 11 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/posts.rs | 27 | ||||
-rw-r--r-- | src/user.rs | 8 |
4 files changed, 21 insertions, 27 deletions
diff --git a/src/logging.rs b/src/logging.rs index 7b8bd2e..c489b63 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -3,16 +3,11 @@ use std::fs::File; use chrono::offset::Utc; use simplelog::*; -use users; + +use crate::user; lazy_static! { - static ref FILE: String = format!( - "/tmp/clinte_{}.log", - users::get_current_username() - .unwrap() - .into_string() - .unwrap() - ); + static ref FILE: String = format!("/tmp/clinte_{}.log", *user::NAME); } pub fn init() { diff --git a/src/main.rs b/src/main.rs index 2e760ae..fcb7535 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,8 +7,10 @@ use clap; use log::info; mod db; +mod ed; mod logging; mod posts; +mod user; fn main() { let arg_matches = clap::App::new("clinte") diff --git a/src/posts.rs b/src/posts.rs index f8b1d6c..27327ff 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -2,21 +2,20 @@ use std::error::Error; use std::io; use rusqlite; -use users; use crate::db; +use crate::user; type Result<T> = std::result::Result<T, Box<dyn Error>>; // Executes the sql statement that inserts a new post // Broken off for unit testing. pub fn exec_new(stmt: &mut rusqlite::Statement, title: &str, body: &str) -> Result<()> { - let user = users::get_current_username() - .unwrap() - .into_string() - .unwrap(); - - stmt.execute_named(&[(":title", &title), (":author", &user), (":body", &body)])?; + stmt.execute_named(&[ + (":title", &title), + (":author", &*user::NAME), + (":body", &body), + ])?; Ok(()) } @@ -103,11 +102,6 @@ pub fn display(db: &db::Conn) { // First handler to update posts. pub fn update_handler(db: &db::Conn, id: u32) { - let cur_user = users::get_current_username() - .unwrap() - .into_string() - .unwrap(); - let id_num_in = if id == 0 { println!(); println!("ID number of your post to edit?"); @@ -132,7 +126,7 @@ pub fn update_handler(db: &db::Conn, id: u32) { }) .unwrap(); - if cur_user != row[1] { + if *user::NAME != row[1] { println!(); println!("Username mismatch - can't update_handler post!"); return; @@ -180,11 +174,6 @@ pub fn exec_stmt_no_params(stmt: &mut rusqlite::Statement) -> Result<()> { // First handler to remove a post pub fn delete_handler(db: &db::Conn, id: u32) { - let cur_user = users::get_current_username() - .unwrap() - .into_string() - .unwrap(); - let id_num_in: u32 = if id == 0 { println!(); println!("ID of the post to delete?"); @@ -205,7 +194,7 @@ pub fn delete_handler(db: &db::Conn, id: u32) { .query_row(rusqlite::NO_PARAMS, |row| row.get(2)) .unwrap(); - if cur_user != user_in_post { + if *user::NAME != user_in_post { println!(); println!("Users don't match. Can't delete!"); println!(); diff --git a/src/user.rs b/src/user.rs new file mode 100644 index 0000000..7aac1b4 --- /dev/null +++ b/src/user.rs @@ -0,0 +1,8 @@ +use users; + +lazy_static! { + pub static ref NAME: String = users::get_current_username() + .unwrap() + .into_string() + .unwrap(); +} |