Skip to main content

rip_settings/
field.rs

1//! Field metadata types used by the derive macro and runtime.
2
3/// Describes the UI type of a setting field.
4#[derive(Clone, Debug, PartialEq)]
5pub enum FieldKind {
6    Toggle,
7    Slider { min: f64, max: f64, step: f64 },
8    Dropdown { options: Vec<String> },
9    TextInput,
10    Button { action: String },
11    Custom { type_name: String },
12}
13
14/// Validation rules for a field.
15#[derive(Clone, Debug, Default)]
16pub struct ValidationRules {
17    pub range: Option<(f64, f64)>,
18    pub length: Option<(usize, usize)>,
19    pub pattern: Option<String>,
20    pub required: bool,
21    pub error_message: Option<String>,
22}
23
24/// Confirmation dialog configuration.
25#[derive(Clone, Debug)]
26pub struct ConfirmationConfig {
27    pub title: String,
28    pub message: String,
29    pub confirm_text: String,
30    pub cancel_text: String,
31    pub is_dangerous: bool,
32}
33
34impl Default for ConfirmationConfig {
35    fn default() -> Self {
36        Self {
37            title: "Confirm Change".into(),
38            message: "Are you sure you want to change this setting?".into(),
39            confirm_text: "Confirm".into(),
40            cancel_text: "Cancel".into(),
41            is_dangerous: false,
42        }
43    }
44}
45
46/// Runtime metadata for a single setting field.
47///
48/// Generated by the `#[derive(Settings)]` proc macro.
49#[derive(Clone, Debug)]
50pub struct FieldMeta {
51    /// Property/field name in the struct.
52    pub name: &'static str,
53
54    /// Persistence key (defaults to snake_case of name).
55    pub key: &'static str,
56
57    /// Display title for UI.
58    pub title: &'static str,
59
60    /// Description text for UI.
61    pub description: &'static str,
62
63    /// Category identifier string.
64    pub category: &'static str,
65
66    /// Category display order.
67    pub category_order: i32,
68
69    /// UI widget type.
70    pub kind: FieldKind,
71
72    /// Field name this depends on (disabled when dependency is falsy).
73    pub depends_on: Option<&'static str>,
74
75    /// Whether this field is persisted-only (no UI).
76    pub is_persisted_only: bool,
77
78    /// Whether this field should be excluded from reset.
79    pub no_reset: bool,
80
81    /// Confirmation message before reset (if any).
82    pub confirm_reset: Option<&'static str>,
83
84    /// Validation rules.
85    pub validation: Option<ValidationRules>,
86
87    /// Confirmation config for changing this setting.
88    pub confirmation: Option<ConfirmationConfig>,
89
90    /// Platform restrictions (empty = all platforms).
91    /// Values: "android", "ios", "windows", "macos", "linux", "desktop"
92    pub platforms: Vec<&'static str>,
93}
94
95impl Default for FieldMeta {
96    fn default() -> Self {
97        Self {
98            name: "",
99            key: "",
100            title: "",
101            description: "",
102            category: "",
103            category_order: 0,
104            kind: FieldKind::Toggle,
105            depends_on: None,
106            is_persisted_only: false,
107            no_reset: false,
108            confirm_reset: None,
109            validation: None,
110            confirmation: None,
111            platforms: Vec::new(),
112        }
113    }
114}
115
116impl FieldMeta {
117    /// Check if this field is visible on the current platform.
118    pub fn is_visible_on_current_platform(&self) -> bool {
119        if self.platforms.is_empty() {
120            return true;
121        }
122
123        let current = current_platform_name();
124
125        for &p in &self.platforms {
126            if p == "all" {
127                return true;
128            }
129            if p == current {
130                return true;
131            }
132            // "desktop" matches windows, macos, linux
133            if p == "desktop" && matches!(current, "windows" | "macos" | "linux") {
134                return true;
135            }
136        }
137
138        false
139    }
140}
141
142fn current_platform_name() -> &'static str {
143    #[cfg(target_os = "android")]
144    {
145        "android"
146    }
147    #[cfg(target_os = "ios")]
148    {
149        "ios"
150    }
151    #[cfg(target_os = "windows")]
152    {
153        "windows"
154    }
155    #[cfg(target_os = "macos")]
156    {
157        "macos"
158    }
159    #[cfg(target_os = "linux")]
160    {
161        "linux"
162    }
163    #[cfg(not(any(
164        target_os = "android",
165        target_os = "ios",
166        target_os = "windows",
167        target_os = "macos",
168        target_os = "linux"
169    )))]
170    {
171        "unknown"
172    }
173}