forked from janek/mostr
1
0
Fork 0

refactor: create helpers file

This commit is contained in:
xeruf 2024-08-06 23:01:59 +03:00
parent f4f1b56f02
commit b66089fc94
3 changed files with 31 additions and 24 deletions

28
src/helpers.rs Normal file
View File

@ -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<String> {
if str.is_empty() { None } else { Some(str.to_owned()) }
}
pub fn or_print<T, U: Display>(result: Result<T, U>) -> Option<T> {
match result {
Ok(value) => Some(value),
Err(error) => {
warn!("{}", error);
None
}
}
}
pub fn prompt(prompt: &str) -> Option<String> {
print!("{} ", prompt);
stdout().flush().unwrap();
match stdin().lines().next() {
Some(Ok(line)) => Some(line),
_ => None,
}
}

View File

@ -16,10 +16,12 @@ use log::{debug, error, info, trace, warn};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use xdg::BaseDirectories; use xdg::BaseDirectories;
use crate::helpers::*;
use crate::kinds::TRACKING_KIND; use crate::kinds::TRACKING_KIND;
use crate::task::State; use crate::task::State;
use crate::tasks::Tasks; use crate::tasks::Tasks;
mod helpers;
mod task; mod task;
mod tasks; mod tasks;
mod kinds; mod kinds;
@ -79,29 +81,6 @@ impl Drop for EventSender {
} }
} }
fn some_non_empty(str: &str) -> Option<String> {
if str.is_empty() { None } else { Some(str.to_owned()) }
}
fn or_print<T, U: Display>(result: Result<T, U>) -> Option<T> {
match result {
Ok(value) => Some(value),
Err(error) => {
warn!("{}", error);
None
}
}
}
fn prompt(prompt: &str) -> Option<String> {
print!("{} ", prompt);
stdout().flush().unwrap();
match stdin().lines().next() {
Some(Ok(line)) => Some(line),
_ => None,
}
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
colog::init(); colog::init();

View File

@ -8,8 +8,8 @@ use itertools::Itertools;
use log::{debug, error, info, trace, warn}; use log::{debug, error, info, trace, warn};
use nostr_sdk::{Alphabet, Event, EventBuilder, EventId, Kind, Tag, TagStandard, Timestamp}; use nostr_sdk::{Alphabet, Event, EventBuilder, EventId, Kind, Tag, TagStandard, Timestamp};
use crate::helpers::some_non_empty;
use crate::kinds::is_hashtag; use crate::kinds::is_hashtag;
use crate::some_non_empty;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub(crate) struct Task { pub(crate) struct Task {