use crate::{Bus, LAST, PointDecl, PointKind, Refused, offers};
pub fn declare_all(bus: &dyn Bus, table: &[PointDecl]) -> Vec<Refused> {
table
.iter()
.filter_map(|d| {
if d.journaled && d.kind != PointKind::Event {
return Some(Refused::NotEvent { point: d.id });
}
if let Some(codec) = d.relay {
if codec.point != d.id {
return Some(Refused::TypeMismatch { point: d.id });
}
let crosses = match d.kind {
PointKind::Event => true,
PointKind::Request => codec.answers(),
_ => false,
};
if !crosses {
return Some(Refused::NotCrossable { point: d.id });
}
}
if let Some(capability) = d.required().into_iter().find(|c| !offers(bus, *c)) {
return Some(Refused::Unsupported {
point: d.id,
capability,
});
}
bus.declare_point(d.key(), d.spec()).err()
})
.collect()
}
pub fn order_dump(bus: &dyn Bus, points: &[&str]) -> String {
let mut out = String::new();
for point in points {
out.push_str(point);
out.push('\n');
for (n, l) in bus.listeners(point).unwrap_or_default().iter().enumerate() {
let mut declares: Vec<String> = Vec::new();
if l.local {
declares.push("local".into());
}
if l.after.iter().any(|a| a == LAST) {
declares.push("last".into());
}
let after: Vec<&str> = l
.after
.iter()
.filter(|a| *a != LAST)
.map(String::as_str)
.collect();
if !after.is_empty() {
declares.push(format!("after {}", after.join(", ")));
}
if !l.before.is_empty() {
declares.push(format!("before {}", l.before.join(", ")));
}
if l.priority != 0 {
declares.push(format!("priority {}", l.priority));
}
if !l.missing.is_empty() {
declares.push(format!("MISSING {}", l.missing.join(", ")));
}
let declares = if declares.is_empty() {
"nothing".to_string()
} else {
declares.join("; ")
};
out.push_str(&format!(
" {}. {} (owner {}): {declares}\n",
n + 1,
l.id,
l.owner.as_deref().unwrap_or("-"),
));
}
}
out
}
pub fn missing_targets(bus: &dyn Bus, points: &[&str]) -> Vec<String> {
points
.iter()
.flat_map(|p| {
bus.listeners(p)
.unwrap_or_default()
.into_iter()
.flat_map(move |l| {
l.missing
.iter()
.map(|t| format!("{p}: {} -> {t}", l.id))
.collect::<Vec<_>>()
})
})
.collect()
}