use crate::control::{Control, ControlId};
pub struct ControlRegistry {
controls: Vec<Box<dyn Control>>,
}
impl ControlRegistry {
pub fn new() -> Self {
Self {
controls: Vec::new(),
}
}
pub fn builtin() -> Self {
let mut registry = Self::new();
registry.register_builtins();
registry
}
pub fn register(&mut self, control: Box<dyn Control>) {
self.controls.push(control);
}
fn register_builtins(&mut self) {
use crate::controls;
self.controls.extend(controls::all_controls());
}
pub fn controls(&self) -> &[Box<dyn Control>] {
&self.controls
}
pub fn control_ids(&self) -> Vec<ControlId> {
self.controls.iter().map(|c| c.id()).collect()
}
pub fn len(&self) -> usize {
self.controls.len()
}
pub fn is_empty(&self) -> bool {
self.controls.is_empty()
}
}
impl Default for ControlRegistry {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builtin_registry_matches_all_count() {
use crate::control::builtin;
let registry = ControlRegistry::builtin();
assert_eq!(
registry.len(),
builtin::ALL.len(),
"registry must contain exactly one control per builtin::ALL entry"
);
}
#[test]
fn empty_registry() {
let registry = ControlRegistry::new();
assert!(registry.is_empty());
}
}