use serde_json::{Map, Value, json};
use crate::board::BoardRow;
use crate::spend::Figure;
use super::super::codec::join_token;
pub(super) fn board_row(row: &BoardRow) -> Value {
let mut map = Map::new();
map.insert("id".to_owned(), json!(row.id));
map.insert("column".to_owned(), json!(row.column.word()));
map.insert("state".to_owned(), json!(join_token(row.state)));
map.insert("title".to_owned(), json!(row.title));
map.insert("priority".to_owned(), json!(row.priority));
map.insert(
"project".to_owned(),
json!(row.project.to_string_lossy().into_owned()),
);
if let Some(ws) = &row.workspace {
map.insert(
"workspace".to_owned(),
json!(ws.to_string_lossy().into_owned()),
);
}
if let Some(claimant) = &row.claimant {
map.insert("claimant".to_owned(), json!(claimant));
}
if let Some(parent) = &row.parent {
map.insert("parent".to_owned(), json!(parent));
}
map.insert(
"gates".to_owned(),
json!(
row.gates
.iter()
.map(|g| json!({ "id": g.id, "title": g.title, "mints": "close" }))
.collect::<Vec<Value>>()
),
);
map.insert(
"drones".to_owned(),
json!(
row.drones
.iter()
.map(|d| json!({ "root_id": d.root_id, "name": d.name }))
.collect::<Vec<Value>>()
),
);
if let Some(figure) = &row.spend {
map.insert("spend".to_owned(), figure_value(figure));
}
if let Some(figure) = &row.rollup {
map.insert("rollup".to_owned(), figure_value(figure));
}
Value::Object(map)
}
pub(super) fn fleet_facts(facts: &crate::fleet::Facts) -> Value {
let mut map = Map::new();
map.insert(
"workspace".to_owned(),
json!(facts.workspace.to_string_lossy().into_owned()),
);
map.insert(
"project".to_owned(),
json!(facts.project.to_string_lossy().into_owned()),
);
map.insert("cap".to_owned(), json!(facts.cap));
map.insert("count".to_owned(), json!(facts.count));
map.insert("room".to_owned(), json!(facts.has_room()));
map.insert("tick_secs".to_owned(), json!(facts.tick.as_secs()));
if let Some(lease) = facts.lease {
map.insert("lease_secs".to_owned(), json!(lease.as_secs()));
}
if let Some(secs) = facts.since_act {
map.insert("last_act_secs_ago".to_owned(), json!(secs));
}
if let Some(ceiling) = &facts.ceiling {
map.insert("ceiling".to_owned(), json!(ceiling));
}
map.insert("label".to_owned(), json!(facts.label()));
Value::Object(map)
}
fn figure_value(figure: &Figure) -> Value {
let mut map = Map::new();
map.insert("tokens".to_owned(), json!(figure.tokens.total_tokens()));
if let Some(cost) = figure.cost {
map.insert("usd".to_owned(), json!(cost.usd()));
map.insert("micro_usd".to_owned(), json!(cost.micro_usd));
map.insert("unpriced_tokens".to_owned(), json!(cost.unpriced_tokens));
}
if let Some(note) = figure.attribution.note() {
map.insert("attribution".to_owned(), json!(note.label));
}
Value::Object(map)
}