use super::{UiState, descend, string_array};
use serde_json::Value;
use std::collections::BTreeSet;
const COLLAPSED: &str = "collapsed";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SeenKind {
Notify,
Stopped,
Budget,
Conflicted,
}
impl SeenKind {
fn key(self) -> &'static str {
match self {
SeenKind::Notify => "notify",
SeenKind::Stopped => "stopped",
SeenKind::Budget => "budget",
SeenKind::Conflicted => "conflicted",
}
}
}
impl UiState {
pub fn record_seen(&mut self, ws: &str, agent: &str, marks: &[(SeenKind, String)]) {
for (kind, oid) in marks {
let by_ws = descend(&mut self.world.root, "seen".to_string());
let by_agent = descend(by_ws, ws.to_string());
let slot = descend(by_agent, agent.to_string());
slot.insert(kind.key().to_string(), Value::String(oid.clone()));
}
self.world.save();
}
pub fn is_seen(&self, kind: SeenKind, ws: &str, agent: &str, oid: &str) -> bool {
self.world
.root
.get("seen")
.and_then(Value::as_object)
.and_then(|m| m.get(ws))
.and_then(Value::as_object)
.and_then(|m| m.get(agent))
.and_then(Value::as_object)
.and_then(|m| m.get(kind.key()))
.and_then(Value::as_str)
== Some(oid)
}
pub fn pinned(&self) -> Vec<String> {
string_array(&self.world.root, "pinned")
}
pub fn set_pinned(&mut self, list: Vec<String>) {
let arr = list.into_iter().map(Value::String).collect();
self.world
.root
.insert("pinned".to_string(), Value::Array(arr));
self.world.save();
}
pub fn is_collapsed(&self, key: &str) -> bool {
string_array(&self.pane.root, COLLAPSED).contains(&key.to_string())
}
pub fn set_collapsed(&mut self, key: &str, collapsed: bool) {
let mut set: BTreeSet<String> = BTreeSet::new();
set.extend(string_array(&self.pane.root, COLLAPSED));
if collapsed {
set.insert(key.to_string());
} else {
set.remove(key);
}
let arr = set.into_iter().map(Value::String).collect();
self.pane
.root
.insert(COLLAPSED.to_string(), Value::Array(arr));
self.pane.save();
}
pub fn identity_last_used(&self) -> Option<String> {
self.world
.root
.get("identity_last_used")
.and_then(Value::as_str)
.map(String::from)
}
pub fn set_identity(&mut self, identity: &str) {
self.world.root.insert(
"identity_last_used".to_string(),
Value::String(identity.to_string()),
);
self.world.save();
}
}
#[cfg(test)]
mod tests;