pub trait FeatureFlags {
// Required methods
fn is_enabled(&self, name: &str) -> Option<bool>;
fn set(&mut self, name: &str, enabled: bool) -> bool;
fn all_flags() -> &'static [&'static str]
where Self: Sized;
// Provided methods
fn enable(&mut self, name: &str) -> bool { ... }
fn disable(&mut self, name: &str) -> bool { ... }
fn toggle(&mut self, name: &str) -> Option<bool> { ... }
fn to_map(&self) -> HashMap<String, bool>
where Self: Sized { ... }
fn load_from_map(&mut self, map: &HashMap<String, bool>) -> usize { ... }
}Expand description
Trait for feature flag containers
Implement this trait to create a type-safe feature flag system.
Use #[derive(FeatureFlags)] for automatic implementation.
§Example
use tui_dispatch_core::FeatureFlags;
struct MyFeatures {
dark_mode: bool,
experimental: bool,
}
impl FeatureFlags for MyFeatures {
fn is_enabled(&self, name: &str) -> Option<bool> {
match name {
"dark_mode" => Some(self.dark_mode),
"experimental" => Some(self.experimental),
_ => None,
}
}
fn set(&mut self, name: &str, enabled: bool) -> bool {
match name {
"dark_mode" => { self.dark_mode = enabled; true }
"experimental" => { self.experimental = enabled; true }
_ => false,
}
}
fn all_flags() -> &'static [&'static str] {
&["dark_mode", "experimental"]
}
}Required Methods§
Sourcefn is_enabled(&self, name: &str) -> Option<bool>
fn is_enabled(&self, name: &str) -> Option<bool>
Check if a feature is enabled by name
Returns None if the feature doesn’t exist.
Provided Methods§
Sourcefn enable(&mut self, name: &str) -> bool
fn enable(&mut self, name: &str) -> bool
Enable a feature by name
Returns false if the feature doesn’t exist.
Sourcefn disable(&mut self, name: &str) -> bool
fn disable(&mut self, name: &str) -> bool
Disable a feature by name
Returns false if the feature doesn’t exist.
Sourcefn toggle(&mut self, name: &str) -> Option<bool>
fn toggle(&mut self, name: &str) -> Option<bool>
Toggle a feature by name
Returns the new state, or None if the feature doesn’t exist.