use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use super::body::BodyCache;
use crate::commands::target::BoardTarget;
pub(super) use super::shared::State;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Health {
pub fuse_blown: bool,
pub violations: usize,
}
impl Default for Health {
fn default() -> Self {
Self {
fuse_blown: true,
violations: 0,
}
}
}
#[derive(Default)]
struct Snapshot {
payload: String,
health: Health,
source: Option<Arc<str>>,
}
#[derive(Clone)]
pub(super) struct Slot {
pub name: Option<String>,
pub entry: PathBuf,
pub kicad: Option<PathBuf>,
pub body: BodyCache,
latest: Arc<Mutex<Snapshot>>,
}
impl Slot {
pub(super) fn new(board: &BoardTarget) -> Self {
Self {
name: board.name.clone(),
entry: board.entry.clone(),
kicad: board.kicad.clone(),
body: BodyCache::default(),
latest: Arc::default(),
}
}
pub(super) fn publish(&self, payload: String, health: Health, source: Option<Arc<str>>) {
if let Ok(mut latest) = self.latest.lock() {
*latest = Snapshot {
payload,
health,
source,
};
}
}
#[cfg(test)]
pub(super) fn set_source(&self, source: Option<&str>) {
if let Ok(mut latest) = self.latest.lock() {
latest.source = source.map(Arc::from);
}
}
pub(super) fn source(&self) -> Option<Arc<str>> {
self.latest.lock().ok().and_then(|l| l.source.clone())
}
pub(super) fn payload(&self) -> String {
self.latest
.lock()
.map(|l| l.payload.clone())
.unwrap_or_default()
}
pub(super) fn health(&self) -> Health {
self.latest.lock().map(|l| l.health).unwrap_or_default()
}
pub(super) fn is_ready(&self) -> bool {
self.latest.lock().is_ok_and(|l| !l.payload.is_empty())
}
}