forked from janek/mostr
1
0
Fork 0

refactor: refactor task structs into own file

This commit is contained in:
xeruf 2024-07-19 01:23:25 +03:00
parent 0b1c6fa45c
commit 2dbfecc4c5
3 changed files with 161 additions and 154 deletions

View File

@ -1,19 +1,20 @@
mod tasks;
use crate::tasks::Tasks;
use crate::State::*;
use nostr_sdk::async_utility::futures_util::TryFutureExt;
use nostr_sdk::prelude::*;
use once_cell::sync::Lazy;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::env::args; use std::env::args;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::io::{stdin, stdout, Write}; use std::io::{stdin, stdout, Write};
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::ops::Deref; use std::ops::Deref;
use std::time::Duration; use std::time::Duration;
use nostr_sdk::async_utility::futures_util::TryFutureExt;
use nostr_sdk::prelude::*;
use once_cell::sync::Lazy;
use crate::task::State;
use crate::tasks::Tasks;
mod task;
mod tasks;
/* /*
1: Task Description 1: Task Description
Issue Tracking: https://github.com/nostr-protocol/nips/blob/master/34.md Issue Tracking: https://github.com/nostr-protocol/nips/blob/master/34.md
@ -143,7 +144,11 @@ async fn repl() {
Some('>') | Some('<') => { Some('>') | Some('<') => {
tasks.update_state(&input[1..], |_| { tasks.update_state(&input[1..], |_| {
Some(if op.unwrap() == '<' { Closed } else { Done }) Some(if op.unwrap() == '<' {
State::Closed
} else {
State::Done
})
}); });
tasks.move_up() tasks.move_up()
} }
@ -198,144 +203,3 @@ async fn repl() {
) )
.await; .await;
} }
struct Task {
event: Event,
children: Vec<EventId>,
props: Vec<Event>,
}
impl Task {
fn new(event: Event) -> Task {
Task {
event,
children: Vec::new(),
props: Vec::new(),
}
}
fn parent_id(&self) -> Option<EventId> {
for tag in self.event.tags.iter() {
match tag {
Tag::Event { event_id, .. } => return Some(*event_id),
_ => {}
}
}
None
}
fn descriptions(&self) -> impl Iterator<Item = String> + '_ {
self.props.iter().filter_map(|event| {
if event.kind == Kind::TextNote {
Some(event.content.clone())
} else {
None
}
})
}
fn states(&self) -> impl Iterator<Item = TaskState> + '_ {
self.props.iter().filter_map(|event| {
match event.kind.as_u32() {
1630 => Some(Open),
1631 => Some(Done),
1632 => Some(Closed),
1633 => Some(Active),
_ => None,
}
.map(|s| TaskState {
name: if event.content.is_empty() {
None
} else {
Some(event.content.clone())
},
state: s,
time: event.created_at.clone(),
})
})
}
fn state(&self) -> Option<TaskState> {
self.states().max_by_key(|t| t.time)
}
fn pure_state(&self) -> State {
self.state().map_or(Open, |s| s.state)
}
fn default_state(&self) -> TaskState {
TaskState {
name: None,
state: Open,
time: self.event.created_at,
}
}
fn update_state(&mut self, state: State, comment: &str) {
self.props.push(make_event(
state.kind(),
comment,
&[Tag::event(self.event.id)],
))
}
fn get(&self, property: &str) -> Option<String> {
match property {
"id" => Some(self.event.id.to_string()),
"parentid" => self.parent_id().map(|i| i.to_string()),
"state" => self.state().map(|s| s.to_string()),
"name" => Some(self.event.content.clone()),
"desc" | "description" => self.descriptions().fold(None, |total, s| {
Some(match total {
None => s,
Some(i) => i + " " + &s,
})
}),
_ => {
eprintln!("Unknown column {}", property);
None
}
}
}
}
struct TaskState {
name: Option<String>,
state: State,
time: Timestamp,
}
impl Display for TaskState {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"{}{}",
self.state,
self.name
.as_ref()
.map_or(String::new(), |s| format!(": {}", s))
)
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
enum State {
Closed,
Open,
Active,
Done,
}
impl State {
fn kind(&self) -> Kind {
match self {
Open => Kind::from(1630),
Done => Kind::from(1631),
Closed => Kind::from(1632),
Active => Kind::from(1633),
}
}
}
static STATES: [State; 4] = [Closed, Open, Active, Done];
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}

143
src/task.rs Normal file
View File

@ -0,0 +1,143 @@
use std::fmt;
use nostr_sdk::{Event, EventId, Kind, Tag, Timestamp};
use crate::make_event;
pub(crate) struct Task {
pub(crate) event: Event,
pub(crate) children: Vec<EventId>,
pub(crate) props: Vec<Event>,
}
impl Task {
pub(crate) fn new(event: Event) -> Task {
Task {
event,
children: Vec::new(),
props: Vec::new(),
}
}
pub(crate) fn parent_id(&self) -> Option<EventId> {
for tag in self.event.tags.iter() {
match tag {
Tag::Event { event_id, .. } => return Some(*event_id),
_ => {}
}
}
None
}
fn descriptions(&self) -> impl Iterator<Item = String> + '_ {
self.props.iter().filter_map(|event| {
if event.kind == Kind::TextNote {
Some(event.content.clone())
} else {
None
}
})
}
fn states(&self) -> impl Iterator<Item = TaskState> + '_ {
self.props.iter().filter_map(|event| {
match event.kind.as_u32() {
1630 => Some(State::Open),
1631 => Some(State::Done),
1632 => Some(State::Closed),
1633 => Some(State::Active),
_ => None,
}
.map(|s| TaskState {
name: if event.content.is_empty() {
None
} else {
Some(event.content.clone())
},
state: s,
time: event.created_at.clone(),
})
})
}
fn state(&self) -> Option<TaskState> {
self.states().max_by_key(|t| t.time)
}
pub(crate) fn pure_state(&self) -> State {
self.state().map_or(State::Open, |s| s.state)
}
fn default_state(&self) -> TaskState {
TaskState {
name: None,
state: State::Open,
time: self.event.created_at,
}
}
pub(crate) fn update_state(&mut self, state: State, comment: &str) {
self.props.push(make_event(
state.kind(),
comment,
&[Tag::event(self.event.id)],
))
}
pub(crate) fn get(&self, property: &str) -> Option<String> {
match property {
"id" => Some(self.event.id.to_string()),
"parentid" => self.parent_id().map(|i| i.to_string()),
"state" => self.state().map(|s| s.to_string()),
"name" => Some(self.event.content.clone()),
"desc" | "description" => self.descriptions().fold(None, |total, s| {
Some(match total {
None => s,
Some(i) => i + " " + &s,
})
}),
_ => {
eprintln!("Unknown column {}", property);
None
}
}
}
}
struct TaskState {
name: Option<String>,
state: State,
time: Timestamp,
}
impl fmt::Display for TaskState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}{}",
self.state,
self.name
.as_ref()
.map_or(String::new(), |s| format!(": {}", s))
)
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum State {
Closed,
Open,
Active,
Done,
}
impl State {
fn kind(&self) -> Kind {
match self {
State::Open => Kind::from(1630),
State::Done => Kind::from(1631),
State::Closed => Kind::from(1632),
State::Active => Kind::from(1633),
}
}
}
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}

View File

@ -1,4 +1,5 @@
use crate::{make_event, make_task, State, Task}; use crate::{make_event, make_task};
use crate::task::{Task, State};
use nostr_sdk::{Event, EventId, Tag}; use nostr_sdk::{Event, EventId, Tag};
use std::collections::HashMap; use std::collections::HashMap;
@ -135,8 +136,7 @@ impl Tasks {
{ {
self.tasks.get_mut(id).map(|t| { self.tasks.get_mut(id).map(|t| {
f(t).map(|s| { f(t).map(|s| {
t.props t.update_state(s, comment);
.push(make_event(s.kind(), comment, &[Tag::event(id.clone())]))
}) })
}); });
} }