Skip to main content

FeatureFlags

Trait FeatureFlags 

Source
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§

Source

fn is_enabled(&self, name: &str) -> Option<bool>

Check if a feature is enabled by name

Returns None if the feature doesn’t exist.

Source

fn set(&mut self, name: &str, enabled: bool) -> bool

Set a feature’s enabled state

Returns false if the feature doesn’t exist.

Source

fn all_flags() -> &'static [&'static str]
where Self: Sized,

Get all available flag names

Provided Methods§

Source

fn enable(&mut self, name: &str) -> bool

Enable a feature by name

Returns false if the feature doesn’t exist.

Source

fn disable(&mut self, name: &str) -> bool

Disable a feature by name

Returns false if the feature doesn’t exist.

Source

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.

Source

fn to_map(&self) -> HashMap<String, bool>
where Self: Sized,

Get all flags as a map of name -> enabled

Source

fn load_from_map(&mut self, map: &HashMap<String, bool>) -> usize

Load flags from a map (e.g., from config file)

Unknown flags are ignored. Returns the number of flags that were set.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§