lensbench_project/
detectors.rs1use lensbench_core::ext::{self, Ext};
9
10use serde_json::{Map, Value};
11
12#[derive(Clone, Copy)]
14pub struct DetectorKind {
15 pub name: &'static str,
17 pub doc: &'static str,
19 pub check: fn(&Map<String, Value>) -> lensbench_core::Result<()>,
21 pub schema: Option<fn() -> Value>,
23}
24
25#[derive(Clone, Default)]
27pub struct DetectorKinds(Vec<DetectorKind>);
28
29pub fn add(e: &mut Ext, k: DetectorKind) -> lensbench_core::Result<()> {
31 if crate::BUILTIN_BLOCKS.contains(&k.name) || k.name.is_empty() {
32 return Err(format!("detector kind name {:?} can't be used", k.name).into());
33 }
34 let v = &mut e.slot_mut::<DetectorKinds>().0;
35 v.retain(|x| x.name != k.name);
36 v.push(k);
37 Ok(())
38}
39
40pub fn register(k: DetectorKind) -> lensbench_core::Result<()> {
42 ext::update_default(|e| add(e, k))
43}
44
45pub fn get(name: &str) -> Option<DetectorKind> {
47 list().into_iter().find(|k| k.name == name)
48}
49
50pub fn list() -> Vec<DetectorKind> {
52 ext::current()
53 .slot::<DetectorKinds>()
54 .map(|k| k.0.clone())
55 .unwrap_or_default()
56}