From b66089fc9408947ef444adcbffb9eebe6ea6e767 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Tue, 6 Aug 2024 23:01:59 +0300 Subject: [PATCH] refactor: create helpers file --- src/helpers.rs | 28 ++++++++++++++++++++++++++++ src/main.rs | 25 ++----------------------- src/task.rs | 2 +- 3 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 src/helpers.rs diff --git a/src/helpers.rs b/src/helpers.rs new file mode 100644 index 0000000..e161e42 --- /dev/null +++ b/src/helpers.rs @@ -0,0 +1,28 @@ +use std::fmt::Display; +use std::io::{stdin, stdout, Write}; + +use log::{debug, error, info, trace, warn}; + +pub fn some_non_empty(str: &str) -> Option { + if str.is_empty() { None } else { Some(str.to_owned()) } +} + +pub fn or_print(result: Result) -> Option { + match result { + Ok(value) => Some(value), + Err(error) => { + warn!("{}", error); + None + } + } +} + +pub fn prompt(prompt: &str) -> Option { + print!("{} ", prompt); + stdout().flush().unwrap(); + match stdin().lines().next() { + Some(Ok(line)) => Some(line), + _ => None, + } +} + diff --git a/src/main.rs b/src/main.rs index 9ffb634..afdc928 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,10 +16,12 @@ use log::{debug, error, info, trace, warn}; use nostr_sdk::prelude::*; use xdg::BaseDirectories; +use crate::helpers::*; use crate::kinds::TRACKING_KIND; use crate::task::State; use crate::tasks::Tasks; +mod helpers; mod task; mod tasks; mod kinds; @@ -79,29 +81,6 @@ impl Drop for EventSender { } } -fn some_non_empty(str: &str) -> Option { - if str.is_empty() { None } else { Some(str.to_owned()) } -} - -fn or_print(result: Result) -> Option { - match result { - Ok(value) => Some(value), - Err(error) => { - warn!("{}", error); - None - } - } -} - -fn prompt(prompt: &str) -> Option { - print!("{} ", prompt); - stdout().flush().unwrap(); - match stdin().lines().next() { - Some(Ok(line)) => Some(line), - _ => None, - } -} - #[tokio::main] async fn main() { colog::init(); diff --git a/src/task.rs b/src/task.rs index e782ed3..4f294d1 100644 --- a/src/task.rs +++ b/src/task.rs @@ -8,8 +8,8 @@ use itertools::Itertools; use log::{debug, error, info, trace, warn}; use nostr_sdk::{Alphabet, Event, EventBuilder, EventId, Kind, Tag, TagStandard, Timestamp}; +use crate::helpers::some_non_empty; use crate::kinds::is_hashtag; -use crate::some_non_empty; #[derive(Debug, Clone, PartialEq)] pub(crate) struct Task {