use serde_json::{json, Value};
use crate::task::{On, Status, Task};
pub(crate) fn status_word(s: Status) -> &'static str {
match s {
Status::Ready => "ready",
Status::Claimed => "claimed",
Status::Blocked => "blocked",
}
}
pub(crate) fn on_word(on: On) -> &'static str {
on.token()
}
pub(crate) fn task_json(id: &str, task: &Task) -> Value {
let blockers: Vec<Value> = task
.blockers
.iter()
.map(|b| json!({ "id": b.id, "on": on_word(b.on) }))
.collect();
let mut record = serde_json::to_value(&task.extra).expect("a toml table serializes to a json object");
let map = record.as_object_mut().expect("a toml table is a json object");
for (key, value) in [
("id", json!(id)),
("title", json!(task.title)),
("claimant", json!(task.claimant)),
("priority", json!(task.priority)),
("parent", json!(task.parent)),
("tags", json!(task.tags)),
("blockers", json!(blockers)),
("created", json!(task.created)),
("updated", json!(task.updated)),
("body", json!(task.body)),
] {
map.insert(key.to_string(), value);
}
record
}
pub(crate) fn json_line(v: &Value) -> String {
format!("{}\n", serde_json::to_string_pretty(v).expect("serde_json::Value serializes"))
}