From faf0f30d3697e710746a556121ea93daf11e7c2d Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sun, 26 Nov 2023 00:22:08 -0500 Subject: [PATCH] Add separate utils module --- src/main.rs | 28 ++-------------------------- src/util.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 26 deletions(-) create mode 100644 src/util.rs diff --git a/src/main.rs b/src/main.rs index 6fd4aa5..789906b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ use clap::{Parser, Subcommand}; use std::io::{self, Write}; use std::path::PathBuf; -use std::process::exit; use std::time::SystemTime; use time_format::strftime_utc; @@ -15,20 +14,8 @@ mod datasets; use datasets::*; mod sync; use sync::*; - -const SECS_IN_HR: u64 = 3600; -const SECS_IN_DAY: u64 = SECS_IN_HR * 24; -const SECS_IN_WEEK: u64 = SECS_IN_DAY * 7; - -pub fn error(msg: &str, code: i32) -> ! { - println!("\nERROR: {}", msg); - exit(code) -} - -pub fn issue(msg: &str, code: i32) -> ! { - println!("\n{}", msg); - exit(code) -} +mod util; +use util::*; /// ## CLI Structs @@ -190,17 +177,6 @@ fn dataset_list() { } } -fn read_string() -> String { - let mut line = String::new(); - io::stdout() - .flush() - .unwrap_or_else(|_| error("Could not access stdout", 2)); - io::stdin() - .read_line(&mut line) - .unwrap_or_else(|_| error("Could not read from stdin", 2)); - line.trim().to_owned() -} - fn dataset_new(name: Option, auth_token: Option) { let config_dir = dirs::config_dir().expect("Could not determine config directory"); diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..5403663 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,27 @@ +use std::io::{self, Write}; +use std::process::exit; + +pub const SECS_IN_HR: u64 = 3600; +pub const SECS_IN_DAY: u64 = SECS_IN_HR * 24; +pub const SECS_IN_WEEK: u64 = SECS_IN_DAY * 7; + +pub fn error(msg: &str, code: i32) -> ! { + println!("\nERROR: {}", msg); + exit(code) +} + +pub fn issue(msg: &str, code: i32) -> ! { + println!("\n{}", msg); + exit(code) +} + +pub fn read_string() -> String { + let mut line = String::new(); + io::stdout() + .flush() + .unwrap_or_else(|_| error("Could not access stdout", 2)); + io::stdin() + .read_line(&mut line) + .unwrap_or_else(|_| error("Could not read from stdin", 2)); + line.trim().to_owned() +}