rip-settings 0.1.2

Core runtime for rip-settings - repository, backends, validation, backup, migration, undo/redo, reset
Documentation
//! Field metadata types used by the derive macro and runtime.

/// Describes the UI type of a setting field.
#[derive(Clone, Debug, PartialEq)]
pub enum FieldKind {
    Toggle,
    Slider { min: f64, max: f64, step: f64 },
    Dropdown { options: Vec<String> },
    TextInput,
    Button { action: String },
    Custom { type_name: String },
}

/// Validation rules for a field.
#[derive(Clone, Debug, Default)]
pub struct ValidationRules {
    pub range: Option<(f64, f64)>,
    pub length: Option<(usize, usize)>,
    pub pattern: Option<String>,
    pub required: bool,
    pub error_message: Option<String>,
}

/// Confirmation dialog configuration.
#[derive(Clone, Debug)]
pub struct ConfirmationConfig {
    pub title: String,
    pub message: String,
    pub confirm_text: String,
    pub cancel_text: String,
    pub is_dangerous: bool,
}

impl Default for ConfirmationConfig {
    fn default() -> Self {
        Self {
            title: "Confirm Change".into(),
            message: "Are you sure you want to change this setting?".into(),
            confirm_text: "Confirm".into(),
            cancel_text: "Cancel".into(),
            is_dangerous: false,
        }
    }
}

/// Runtime metadata for a single setting field.
///
/// Generated by the `#[derive(Settings)]` proc macro.
#[derive(Clone, Debug)]
pub struct FieldMeta {
    /// Property/field name in the struct.
    pub name: &'static str,

    /// Persistence key (defaults to snake_case of name).
    pub key: &'static str,

    /// Display title for UI.
    pub title: &'static str,

    /// Description text for UI.
    pub description: &'static str,

    /// Category identifier string.
    pub category: &'static str,

    /// Category display order.
    pub category_order: i32,

    /// UI widget type.
    pub kind: FieldKind,

    /// Field name this depends on (disabled when dependency is falsy).
    pub depends_on: Option<&'static str>,

    /// Whether this field is persisted-only (no UI).
    pub is_persisted_only: bool,

    /// Whether this field should be excluded from reset.
    pub no_reset: bool,

    /// Confirmation message before reset (if any).
    pub confirm_reset: Option<&'static str>,

    /// Validation rules.
    pub validation: Option<ValidationRules>,

    /// Confirmation config for changing this setting.
    pub confirmation: Option<ConfirmationConfig>,

    /// Platform restrictions (empty = all platforms).
    /// Values: "android", "ios", "windows", "macos", "linux", "desktop"
    pub platforms: Vec<&'static str>,
}

impl Default for FieldMeta {
    fn default() -> Self {
        Self {
            name: "",
            key: "",
            title: "",
            description: "",
            category: "",
            category_order: 0,
            kind: FieldKind::Toggle,
            depends_on: None,
            is_persisted_only: false,
            no_reset: false,
            confirm_reset: None,
            validation: None,
            confirmation: None,
            platforms: Vec::new(),
        }
    }
}

impl FieldMeta {
    /// Check if this field is visible on the current platform.
    pub fn is_visible_on_current_platform(&self) -> bool {
        if self.platforms.is_empty() {
            return true;
        }

        let current = current_platform_name();

        for &p in &self.platforms {
            if p == "all" {
                return true;
            }
            if p == current {
                return true;
            }
            // "desktop" matches windows, macos, linux
            if p == "desktop" && matches!(current, "windows" | "macos" | "linux") {
                return true;
            }
        }

        false
    }
}

fn current_platform_name() -> &'static str {
    #[cfg(target_os = "android")]
    {
        "android"
    }
    #[cfg(target_os = "ios")]
    {
        "ios"
    }
    #[cfg(target_os = "windows")]
    {
        "windows"
    }
    #[cfg(target_os = "macos")]
    {
        "macos"
    }
    #[cfg(target_os = "linux")]
    {
        "linux"
    }
    #[cfg(not(any(
        target_os = "android",
        target_os = "ios",
        target_os = "windows",
        target_os = "macos",
        target_os = "linux"
    )))]
    {
        "unknown"
    }
}