Просмотр исходного кода

bin/tau: use State struct instead of hardcoded states

Dastan-glitch 4 лет назад
Родитель
Сommit
3f07a2bbc5
3 измененных файлов с 22 добавлено и 9 удалено
  1. 8 5
      bin/tau/tau-cli/src/filter.rs
  2. 9 0
      bin/tau/tau-cli/src/primitives.rs
  3. 5 4
      bin/tau/tau-cli/src/view.rs

+ 8 - 5
bin/tau/tau-cli/src/filter.rs

@@ -1,18 +1,21 @@
 use chrono::{Datelike, NaiveDateTime, Utc};
 use serde_json::Value;
 
-use crate::{primitives::TaskInfo, TaskEvent};
+use crate::{
+    primitives::{State, TaskInfo},
+    TaskEvent,
+};
 
 /// Helper function to check task's state
-fn check_task_state(task: &TaskInfo, state: &str) -> bool {
+fn check_task_state(task: &TaskInfo, state: State) -> bool {
     let last_state = task.events.last().unwrap_or(&TaskEvent::default()).action.clone();
-    state == last_state
+    state.to_string() == last_state
 }
 
 pub fn apply_filter(tasks: &mut Vec<TaskInfo>, filter: &str) {
     match filter {
-        "open" => tasks.retain(|task| check_task_state(task, "open")),
-        "pause" => tasks.retain(|task| check_task_state(task, "pause")),
+        "open" => tasks.retain(|task| check_task_state(task, State::Open)),
+        "pause" => tasks.retain(|task| check_task_state(task, State::Pause)),
 
         _ if filter.len() == 4 && filter.parse::<u32>().is_ok() => {
             let (month, year) =

+ 9 - 0
bin/tau/tau-cli/src/primitives.rs

@@ -11,6 +11,15 @@ pub enum State {
     Stop,
 }
 
+impl State {
+    pub const fn is_start(&self) -> bool {
+        matches!(*self, Self::Start)
+    }
+    pub const fn is_pause(&self) -> bool {
+        matches!(*self, Self::Pause)
+    }
+}
+
 impl fmt::Display for State {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self {

+ 5 - 4
bin/tau/tau-cli/src/view.rs

@@ -1,4 +1,4 @@
-use std::fmt::Write;
+use std::{fmt::Write, str::FromStr};
 
 use prettytable::{
     cell,
@@ -13,7 +13,7 @@ use darkfi::{
 
 use crate::{
     filter::apply_filter,
-    primitives::{Comment, TaskInfo},
+    primitives::{Comment, State, TaskInfo},
     TaskEvent,
 };
 
@@ -54,10 +54,11 @@ pub fn print_task_list(tasks: Vec<TaskInfo>, filters: Vec<String>) -> Result<()>
 
     for task in tasks {
         let state = task.events.last().unwrap_or(&TaskEvent::default()).action.clone();
+        let state = State::from_str(&state)?;
 
-        let (max_style, min_style, mid_style, gen_style) = if state == "start" {
+        let (max_style, min_style, mid_style, gen_style) = if state.is_start() {
             ("bFg", "Fc", "Fg", "Fg")
-        } else if state == "pause" {
+        } else if state.is_pause() {
             ("iFYBd", "iFYBd", "iFYBd", "iFYBd")
         } else {
             ("", "", "", "")