summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/logging.rs11
-rw-r--r--src/main.rs2
-rw-r--r--src/posts.rs27
-rw-r--r--src/user.rs8
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();
+}