From 89b2eb9721589eb087cd875d0b86e58b612573ac Mon Sep 17 00:00:00 2001 From: Malte Brandy Date: Mon, 23 Apr 2018 00:48:05 +0200 Subject: Add tw module with query and save functions for tasks --- src/lib.rs | 1 + src/tw.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/tw.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 3eeb8be..44859af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -70,3 +70,4 @@ pub mod status; pub mod tag; pub mod task; pub mod uda; +pub mod tw; diff --git a/src/tw.rs b/src/tw.rs new file mode 100644 index 0000000..b707ceb --- /dev/null +++ b/src/tw.rs @@ -0,0 +1,60 @@ +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// + + +//! This module offers functions to interact with taskwarrior. This will expect the `task` binary +//! in your path. This will always call task and never interact with your `.task` directory itself. +//! (This is in accordance with the taskwarrior api guide lines.) + +use error::{Result, ResultExt}; +use task::Task; +use std::process::{Command, Stdio}; +use std::io::Write; +use import::import; +use serde_json; + +/// This will give you all tasks which match the given query in the taskwarrior query syntax. +/// This is not sanitized. Never get the query string from an untrusted user. +pub fn query(query: &str) -> Result> { + let mut cmd = Command::new("task"); + for filter in query.split_whitespace() { + cmd.arg(filter); + } + let mut export = cmd.arg("export").stdout(Stdio::piped()).spawn().chain_err( + || "Failed to start task … export.", + )?; + export.wait().chain_err( + || "Failed to wait for task … export to finish", + )?; + import(export.stdout.chain_err( + || "Failed to capture stdout of task … export", + )?) +} + +/// This will save the given tasks to taskwarrior. Call with `Some(task)` if you just have one +/// task. +pub fn save(tasks: T) -> Result<()> +where + T: IntoIterator, +{ + let import = Command::new("task") + .arg("import") + .stdin(Stdio::piped()) + .spawn() + .chain_err(|| "Failed to spawn task … import")?; + import + .stdin + .chain_err(|| "Failed to grap stdin of task import")? + .write_all( + serde_json::to_string(&tasks.into_iter().collect::>()) + .chain_err(|| "Failed to Serialize")? + .as_bytes(), + ) + .chain_err( + || "Failed to write serialized tasks to stdin of task import", + )?; + Ok(()) +} -- cgit v1.2.3