From c5b7d97bf130bffdc901ba7cc9165a4fb900429c Mon Sep 17 00:00:00 2001 From: ghassmo Date: Sun, 13 Mar 2022 08:43:47 +0400 Subject: [PATCH] bin/taud: add 'list' rpc function --- bin/taud/src/main.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/bin/taud/src/main.rs b/bin/taud/src/main.rs index 36aa787ab..bbbc75835 100644 --- a/bin/taud/src/main.rs +++ b/bin/taud/src/main.rs @@ -24,6 +24,7 @@ mod task_info; mod util; use crate::{ + month_tasks::MonthTasks, task_info::TaskInfo, util::{get_current_time, CliTaud, Settings, TauConfig, Timestamp, CONFIG_FILE_CONTENTS}, }; @@ -42,6 +43,7 @@ impl RequestHandler for JsonRpcInterface { match req.method.as_str() { Some("add") => return self.add(req.id, req.params).await, + Some("list") => return self.list(req.id, req.params).await, Some(_) | None => return JsonResult::Err(jsonerr(MethodNotFound, None, req.id)), } } @@ -112,11 +114,30 @@ impl JsonRpcInterface { } } - match task.save() { + let result = || -> Result<()> { + task.save()?; + task.activate()?; + Ok(()) + }; + + match result() { Ok(()) => JsonResult::Resp(jsonresp(json!(true), id)), Err(e) => JsonResult::Err(jsonerr(ServerError(-32603), Some(e.to_string()), id)), } } + + // RPCAPI: + // List tasks + // --> {"jsonrpc": "2.0", "method": "list", "params": [], "id": 1} + // <-- {"jsonrpc": "2.0", "result": [task, ...], "id": 1} + async fn list(&self, id: Value, _params: Value) -> JsonResult { + let tasks: Result> = MonthTasks::load_current_open_tasks(&self.settings); + + match tasks { + Ok(tks) => JsonResult::Resp(jsonresp(json!(tks), id)), + Err(e) => JsonResult::Err(jsonerr(ServerError(-32603), Some(e.to_string()), id)), + } + } } async fn start(config: TauConfig, executor: Arc>) -> Result<()> {