From 8c2c2792386c6003f0e05da6c61593ad302a95f1 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Fri, 2 Aug 2024 14:43:39 +0300 Subject: [PATCH] refactor: put kinds and helpers in own module --- src/kinds.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 7 +++---- src/task.rs | 7 +------ src/tasks.rs | 36 +++++------------------------------- 4 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 src/kinds.rs diff --git a/src/kinds.rs b/src/kinds.rs new file mode 100644 index 0000000..165f7d9 --- /dev/null +++ b/src/kinds.rs @@ -0,0 +1,42 @@ +use itertools::Itertools; +use log::info; +use nostr_sdk::{Alphabet, EventBuilder, EventId, GenericTagValue, Kind, Tag}; + +pub const TASK_KIND: u64 = 1621; +pub const TRACKING_KIND: u64 = 1650; + +pub(crate) fn build_tracking(id: I) -> EventBuilder +where I: IntoIterator { + EventBuilder::new( + Kind::from(TRACKING_KIND), + "", + id.into_iter().map(|id| Tag::event(id)), + ) +} + +pub(crate) fn build_task(name: &str, tags: Vec) -> EventBuilder { + info!("Created task \"{name}\" with tags [{}]", tags.iter().map(|tag| format_tag(tag)).join(", ")); + EventBuilder::new(Kind::from(TASK_KIND), name, tags) +} + +fn format_tag(tag: &Tag) -> String { + tag.content().map(|c| { + match c { + GenericTagValue::PublicKey(key) => format!("Key: {}", key.to_string()[..8].to_string()), + GenericTagValue::EventId(id) => format!("Parent: {}", id.to_string()[..8].to_string()), + GenericTagValue::String(str) => { + if is_hashtag(tag) { + format!("#{str}") + } else { + str + } + } + } + }).unwrap_or_else(|| format!("Kind {}", tag.kind())) +} + +pub(crate) fn is_hashtag(tag: &Tag) -> bool { + tag.single_letter_tag() + .is_some_and(|sltag| sltag.character == Alphabet::T) +} + diff --git a/src/main.rs b/src/main.rs index 1e835f1..59a7cbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,14 +15,13 @@ use log::{debug, error, info, trace, warn}; use nostr_sdk::prelude::*; use xdg::BaseDirectories; +use crate::kinds::{build_tracking, TRACKING_KIND}; use crate::task::State; use crate::tasks::Tasks; mod task; mod tasks; - -const TASK_KIND: u64 = 1621; -const TRACKING_KIND: u64 = 1650; +mod kinds; type Events = Vec; @@ -315,7 +314,7 @@ async fn main() { } }, - Some(',') => tasks.add_note(arg), + Some(',') => tasks.make_note(arg), Some('>') => { tasks.update_state(arg, State::Done); diff --git a/src/task.rs b/src/task.rs index 666e4f3..fd4b4db 100644 --- a/src/task.rs +++ b/src/task.rs @@ -8,7 +8,7 @@ use itertools::Itertools; use log::{debug, error, info, trace, warn}; use nostr_sdk::{Alphabet, Event, EventBuilder, EventId, Kind, Tag, Timestamp}; -use crate::EventSender; +use crate::kinds::is_hashtag; #[derive(Debug, Clone, PartialEq)] pub(crate) struct Task { @@ -132,11 +132,6 @@ impl Task { } } -pub(crate) fn is_hashtag(tag: &Tag) -> bool { - tag.single_letter_tag() - .is_some_and(|sltag| sltag.character == Alphabet::T) -} - pub(crate) struct TaskState { state: State, name: Option, diff --git a/src/tasks.rs b/src/tasks.rs index 849ff89..346e3de 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -11,8 +11,9 @@ use log::{debug, error, info, trace, warn}; use nostr_sdk::{Event, EventBuilder, EventId, GenericTagValue, Kind, PublicKey, Tag, Timestamp}; use nostr_sdk::Tag::Hashtag; -use crate::{EventSender, TASK_KIND, TRACKING_KIND}; -use crate::task::{is_hashtag, State, Task}; +use crate::EventSender; +use crate::kinds::*; +use crate::task::{State, Task}; type TaskMap = HashMap; #[derive(Debug, Clone)] @@ -447,13 +448,7 @@ impl Tasks { self.flush(); return; } - self.submit( - EventBuilder::new( - Kind::from(TRACKING_KIND), - "", - id.iter().map(|id| Tag::event(id.clone())), - ) - ); + self.submit(build_tracking(id)); if !id.and_then(|id| self.tasks.get(&id)).is_some_and(|t| t.parent_id() == self.position.as_ref()) { debug!("Flushing Tasks because of move"); self.flush(); @@ -567,7 +562,7 @@ impl Tasks { .map(|id| self.set_state_for(id, comment, state)); } - pub(crate) fn add_note(&mut self, note: &str) { + pub(crate) fn make_note(&mut self, note: &str) { match self.position { None => warn!("Cannot add note '{}' without active task", note), Some(id) => { @@ -593,27 +588,6 @@ fn display_time(format: &str, secs: u64) -> String { ) } -fn build_task(name: &str, tags: Vec) -> EventBuilder { - info!("Created task \"{name}\" with tags [{}]", tags.iter().map(|tag| format_tag(tag)).join(", ")); - EventBuilder::new(Kind::from(TASK_KIND), name, tags) -} - -fn format_tag(tag: &Tag) -> String { - tag.content().map(|c| { - match c { - GenericTagValue::PublicKey(key) => format!("Key: {}", key.to_string()[..8].to_string()), - GenericTagValue::EventId(id) => format!("Parent: {}", id.to_string()[..8].to_string()), - GenericTagValue::String(str) => { - if is_hashtag(tag) { - format!("#{str}") - } else { - str - } - } - } - }).unwrap_or_else(|| format!("Kind {}", tag.kind())) -} - pub(crate) fn join_tasks<'a>( iter: impl Iterator, include_last_id: bool,