refactor: reformat and inline hints

This commit is contained in:
xeruf 2024-07-30 17:13:29 +03:00
parent c848a4797c
commit 320575e9c3
2 changed files with 16 additions and 10 deletions

View File

@ -1,3 +1,4 @@
use fmt::Display;
use std::collections::{BTreeSet, HashSet}; use std::collections::{BTreeSet, HashSet};
use std::fmt; use std::fmt;
use std::ops::Div; use std::ops::Div;
@ -142,7 +143,9 @@ impl Task {
"parentid" => self.parent_id().map(|i| i.to_string()), "parentid" => self.parent_id().map(|i| i.to_string()),
"state" => self.state().map(|s| s.to_string()), "state" => self.state().map(|s| s.to_string()),
"name" => Some(self.event.content.clone()), "name" => Some(self.event.content.clone()),
"time" => Some(self.time_tracked().div(60)).filter(|t| t>&0).map(|t| format!("{}m", t)), "time" => Some(self.time_tracked().div(60))
.filter(|t| t > &0)
.map(|t| format!("{}m", t)),
"desc" => self.descriptions().last().cloned(), "desc" => self.descriptions().last().cloned(),
"description" => Some(self.descriptions().join(" ")), "description" => Some(self.descriptions().join(" ")),
"hashtags" => self.filter_tags(|tag| { "hashtags" => self.filter_tags(|tag| {
@ -188,7 +191,7 @@ impl TaskState {
|| self.state.to_string().eq_ignore_ascii_case(label) || self.state.to_string().eq_ignore_ascii_case(label)
} }
} }
impl fmt::Display for TaskState { impl Display for TaskState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let state_str = self.state.to_string(); let state_str = self.state.to_string();
write!( write!(
@ -240,7 +243,7 @@ impl State {
} }
} }
} }
impl fmt::Display for State { impl Display for State {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f) fmt::Debug::fmt(self, f)
} }

View File

@ -64,17 +64,19 @@ impl Tasks {
impl Tasks { impl Tasks {
// Accessors // Accessors
#[inline]
pub(crate) fn get_by_id(&self, id: &EventId) -> Option<&Task> { pub(crate) fn get_by_id(&self, id: &EventId) -> Option<&Task> {
self.tasks.get(id) self.tasks.get(id)
} }
#[inline]
pub(crate) fn get_position(&self) -> Option<EventId> { pub(crate) fn get_position(&self) -> Option<EventId> {
self.position self.position
} }
/// Total time this task and its subtasks have been active /// Total time this task and its subtasks have been active
fn total_time_tracked(&self, task: &EventId) -> u64 { fn total_time_tracked(&self, id: &EventId) -> u64 {
self.tasks.get(task).map_or(0, |t| { self.get_by_id(id).map_or(0, |t| {
t.time_tracked() t.time_tracked()
+ t.children + t.children
.iter() .iter()
@ -84,7 +86,7 @@ impl Tasks {
} }
fn total_progress(&self, id: &EventId) -> Option<f32> { fn total_progress(&self, id: &EventId) -> Option<f32> {
self.tasks.get(id).and_then(|t| match t.pure_state() { self.get_by_id(id).and_then(|t| match t.pure_state() {
State::Closed => None, State::Closed => None,
State::Done => Some(1.0), State::Done => Some(1.0),
_ => { _ => {
@ -102,7 +104,7 @@ impl Tasks {
// Parents // Parents
pub(crate) fn get_parent(&self, id: Option<EventId>) -> Option<EventId> { pub(crate) fn get_parent(&self, id: Option<EventId>) -> Option<EventId> {
id.and_then(|id| self.tasks.get(&id)) id.and_then(|id| self.get_by_id(&id))
.and_then(|t| t.parent_id()) .and_then(|t| t.parent_id())
} }
@ -151,7 +153,7 @@ impl Tasks {
depth: i8, depth: i8,
) -> Vec<&Task> { ) -> Vec<&Task> {
iter.into_iter() iter.into_iter()
.filter_map(|id| self.tasks.get(&id)) .filter_map(|id| self.get_by_id(&id))
.flat_map(|task| { .flat_map(|task| {
let new_depth = depth - 1; let new_depth = depth - 1;
if new_depth < 0 { if new_depth < 0 {
@ -184,8 +186,9 @@ impl Tasks {
} }
} }
#[inline]
fn current_task(&self) -> Option<&Task> { fn current_task(&self) -> Option<&Task> {
self.position.and_then(|id| self.tasks.get(&id)) self.position.and_then(|id| self.get_by_id(&id))
} }
pub(crate) fn current_tasks(&self) -> Vec<&Task> { pub(crate) fn current_tasks(&self) -> Vec<&Task> {
@ -279,7 +282,7 @@ impl Tasks {
let time = self.total_time_tracked(&task.event.id); let time = self.total_time_tracked(&task.event.id);
if time > 60 { if time > 60 {
format!("{:02}:{:02}", time / 3600, time / 60 % 60) format!("{:02}:{:02}", time / 3600, time / 60 % 60)
} else { } else {
String::new() String::new()
} }
} }