use std::collections::BTreeSet;
use rho_protocol::Device;
use super::tokens::DaemonTokenSet;
use crate::app::mcp_server::token_store;
pub(crate) fn list(connected: &BTreeSet<String>) -> anyhow::Result<Vec<Device>> {
let tokens: DaemonTokenSet =
token_store::load(&crate::credential_store::UntrackedCredentialStore)?;
Ok(devices(&tokens, connected))
}
pub(super) fn devices(tokens: &DaemonTokenSet, connected: &BTreeSet<String>) -> Vec<Device> {
let mut devices: Vec<Device> = tokens
.tokens
.iter()
.map(|token| {
let device = Device::new(&token.name)
.with_platform(token.platform.clone())
.with_paired_at(token.paired_at)
.with_last_seen(token.last_seen);
if connected.contains(&token.name) {
device.connected_now()
} else {
device
}
})
.collect();
devices.sort_by(|left, right| left.name.cmp(&right.name));
devices
}
pub(crate) fn revoke(name: &str) -> anyhow::Result<bool> {
token_store::update(|tokens: &mut DaemonTokenSet| Ok(tokens.revoke(name)))
}
#[cfg(test)]
#[path = "devices_tests.rs"]
mod tests;