use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use super::{ClaimGrant, is_swarm_id};
use crate::core::hive::HiveError;
use crate::core::hive::state::{lock_state, write_private};
const PARTICIPANT_FILE: &str = "participant.json";
const CLAIMS_FILE: &str = "claims.json";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Participant {
pub swarm: String,
pub worker_id: String,
pub cli: String,
pub joined_at: u64,
pub consent_id: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HeldClaim {
pub goal: String,
pub worker_id: String,
#[serde(flatten)]
pub grant: ClaimGrant,
}
pub fn dir(data_dir: &Path, swarm: &str) -> Result<PathBuf, HiveError> {
if !is_swarm_id(swarm) {
return Err(HiveError::Config(format!("not a swarm id: {swarm}")));
}
Ok(data_dir.join("swarm").join(swarm))
}
fn read<T: serde::de::DeserializeOwned>(path: &Path) -> Result<Option<T>, HiveError> {
match std::fs::read(path) {
Ok(bytes) => serde_json::from_slice(&bytes)
.map(Some)
.map_err(|e| HiveError::Local(format!("{}: {e}", path.display()))),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(HiveError::Local(format!("{}: {e}", path.display()))),
}
}
fn write<T: Serialize>(dir: &Path, name: &str, value: &T) -> Result<(), HiveError> {
let bytes = serde_json::to_vec_pretty(value).map_err(|e| HiveError::Local(e.to_string()))?;
write_private(dir, name, &bytes)
}
pub fn participant(data_dir: &Path, swarm: &str) -> Result<Option<Participant>, HiveError> {
read(&dir(data_dir, swarm)?.join(PARTICIPANT_FILE))
}
pub fn joined(data_dir: &Path) -> Vec<Participant> {
let Ok(entries) = std::fs::read_dir(data_dir.join("swarm")) else {
return Vec::new();
};
let mut out: Vec<Participant> = entries
.flatten()
.filter_map(|e| e.file_name().into_string().ok())
.filter(|name| is_swarm_id(name))
.filter_map(|name| participant(data_dir, &name).ok().flatten())
.collect();
out.sort_by(|a, b| a.swarm.cmp(&b.swarm));
out
}
pub fn save_participant(data_dir: &Path, p: &Participant) -> Result<(), HiveError> {
write(&dir(data_dir, &p.swarm)?, PARTICIPANT_FILE, p)
}
pub fn claims(data_dir: &Path, swarm: &str) -> Result<BTreeMap<String, HeldClaim>, HiveError> {
Ok(read(&dir(data_dir, swarm)?.join(CLAIMS_FILE))?.unwrap_or_default())
}
pub fn with_claims<T>(
data_dir: &Path,
swarm: &str,
change: impl FnOnce(&mut BTreeMap<String, HeldClaim>) -> T,
) -> Result<T, HiveError> {
let at = dir(data_dir, swarm)?;
let _lock = lock_state(&at, "claims")?;
let mut held: BTreeMap<String, HeldClaim> = read(&at.join(CLAIMS_FILE))?.unwrap_or_default();
let out = change(&mut held);
write(&at, CLAIMS_FILE, &held)?;
Ok(out)
}
pub fn forget(data_dir: &Path, swarm: &str) -> Result<(), HiveError> {
let at = dir(data_dir, swarm)?;
match std::fs::remove_dir_all(&at) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(HiveError::Local(format!("{}: {e}", at.display()))),
}
}