solunatus 0.6.0

High-precision astronomical calculation library and CLI for sun/moon positions, rise/set times, and lunar phases
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
//! Input draft types for the TUI settings and forms.
//!
//! These types handle user input state for various forms in the TUI,
//! including location input, calendar generation, and AI configuration.

use crate::calendar::CalendarFormat;
use crate::config;
use anyhow::{Context, Result, anyhow};
use chrono::{DateTime, Datelike, Local, NaiveDate};
use std::path::PathBuf;

#[cfg(feature = "ai-insights")]
use crate::ai;

// ============================================================================
// Location Input Draft
// ============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LocationInputField {
    Latitude,
    Longitude,
    Timezone,
}

#[derive(Debug, Clone)]
pub struct LocationInputDraft {
    pub latitude: String,
    pub longitude: String,
    pub timezone: String,
    pub field_index: usize,
    pub error: Option<String>,
}

impl Default for LocationInputDraft {
    fn default() -> Self {
        Self::new()
    }
}

impl LocationInputDraft {
    const FIELD_COUNT: usize = 3;

    pub fn new() -> Self {
        Self {
            latitude: String::new(),
            longitude: String::new(),
            timezone: "UTC".to_string(),
            field_index: 0,
            error: None,
        }
    }

    pub fn current_field(&self) -> LocationInputField {
        match self.field_index {
            0 => LocationInputField::Latitude,
            1 => LocationInputField::Longitude,
            _ => LocationInputField::Timezone,
        }
    }

    pub fn next_field(&mut self) {
        self.field_index = (self.field_index + 1) % Self::FIELD_COUNT;
        self.clear_error();
    }

    pub fn prev_field(&mut self) {
        self.field_index = (self.field_index + Self::FIELD_COUNT - 1) % Self::FIELD_COUNT;
        self.clear_error();
    }

    pub fn input_char(&mut self, c: char) {
        self.clear_error();
        match self.current_field() {
            LocationInputField::Latitude | LocationInputField::Longitude => {
                // Allow digits, minus sign, and decimal point
                if c.is_ascii_digit() || c == '-' || c == '.' {
                    let field = if self.current_field() == LocationInputField::Latitude {
                        &mut self.latitude
                    } else {
                        &mut self.longitude
                    };
                    field.push(c);
                }
            }
            LocationInputField::Timezone => {
                self.timezone.push(c);
            }
        }
    }

    pub fn backspace(&mut self) {
        self.clear_error();
        match self.current_field() {
            LocationInputField::Latitude => {
                self.latitude.pop();
            }
            LocationInputField::Longitude => {
                self.longitude.pop();
            }
            LocationInputField::Timezone => {
                self.timezone.pop();
            }
        }
    }

    pub fn clear_error(&mut self) {
        self.error = None;
    }

    pub fn set_error(&mut self, msg: String) {
        self.error = Some(msg);
    }

    pub fn validate(&self) -> Result<(f64, f64, String)> {
        // Parse latitude
        let lat = self
            .latitude
            .trim()
            .parse::<f64>()
            .map_err(|_| anyhow!("Invalid latitude"))?;

        if !(-90.0..=90.0).contains(&lat) {
            return Err(anyhow!("Latitude must be between -90 and 90"));
        }

        // Parse longitude
        let lon = self
            .longitude
            .trim()
            .parse::<f64>()
            .map_err(|_| anyhow!("Invalid longitude"))?;

        if !(-180.0..=180.0).contains(&lon) {
            return Err(anyhow!("Longitude must be between -180 and 180"));
        }

        // Validate timezone
        let tz = self.timezone.trim().to_string();
        if tz.is_empty() {
            return Err(anyhow!("Timezone cannot be empty"));
        }

        Ok((lat, lon, tz))
    }
}

// ============================================================================
// Calendar Draft
// ============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CalendarField {
    StartDate,
    EndDate,
    Format,
    OutputPath,
}

#[derive(Debug, Clone)]
pub struct CalendarDraft {
    pub start: String,
    pub end: String,
    pub output_path: String,
    pub field_index: usize,
    pub format_index: usize,
    pub error: Option<String>,
}

impl CalendarDraft {
    const FIELD_COUNT: usize = 4;
    const FORMATS: [CalendarFormat; 3] = [
        CalendarFormat::Html,
        CalendarFormat::Json,
        CalendarFormat::Ics,
    ];

    pub fn new(now: DateTime<Local>) -> Self {
        let today = now.date_naive();
        let start = NaiveDate::from_ymd_opt(today.year(), today.month(), 1).unwrap_or(today);
        let (next_year, next_month) = if today.month() == 12 {
            (today.year() + 1, 1)
        } else {
            (today.year(), today.month() + 1)
        };
        let next_month_start = NaiveDate::from_ymd_opt(next_year, next_month, 1).unwrap_or(today);
        let end = next_month_start.pred_opt().unwrap_or(next_month_start);

        Self {
            start: start.format("%Y-%m-%d").to_string(),
            end: end.format("%Y-%m-%d").to_string(),
            output_path: Self::default_output_filename(CalendarFormat::Html, start, end),
            field_index: 0,
            format_index: 0,
            error: None,
        }
    }

    pub fn reset(&mut self, now: DateTime<Local>) {
        *self = Self::new(now);
    }

    pub fn current_field(&self) -> CalendarField {
        match self.field_index {
            0 => CalendarField::StartDate,
            1 => CalendarField::EndDate,
            2 => CalendarField::Format,
            _ => CalendarField::OutputPath,
        }
    }

    pub fn next_field(&mut self) {
        self.field_index = (self.field_index + 1) % Self::FIELD_COUNT;
        self.clear_error();
    }

    pub fn prev_field(&mut self) {
        self.field_index = (self.field_index + Self::FIELD_COUNT - 1) % Self::FIELD_COUNT;
        self.clear_error();
    }

    pub fn current_format(&self) -> CalendarFormat {
        Self::FORMATS[self.format_index]
    }

    pub fn current_format_label(&self) -> &'static str {
        match self.current_format() {
            CalendarFormat::Html => "HTML",
            CalendarFormat::Json => "JSON",
            CalendarFormat::Ics => "ICS",
        }
    }

    pub fn cycle_format(&mut self, delta: isize) {
        let len = Self::FORMATS.len() as isize;
        let mut next = self.format_index as isize + delta;
        if next < 0 {
            next = (next % len + len) % len;
        } else {
            next %= len;
        }
        self.format_index = next as usize;
        self.sync_output_extension();
        self.clear_error();
    }

    pub fn set_format(&mut self, format: CalendarFormat) {
        if let Some(idx) = Self::FORMATS
            .iter()
            .position(|candidate| *candidate == format)
        {
            self.format_index = idx;
            self.sync_output_extension();
            self.clear_error();
        }
    }

    pub fn input_char(&mut self, c: char) {
        self.clear_error();
        match self.current_field() {
            CalendarField::StartDate => {
                if c.is_ascii_digit() || c == '-' {
                    self.start.push(c);
                }
            }
            CalendarField::EndDate => {
                if c.is_ascii_digit() || c == '-' {
                    self.end.push(c);
                }
            }
            CalendarField::Format => {}
            CalendarField::OutputPath => {
                self.output_path.push(c);
            }
        }
    }

    pub fn backspace(&mut self) {
        self.clear_error();
        match self.current_field() {
            CalendarField::StartDate => {
                self.start.pop();
            }
            CalendarField::EndDate => {
                self.end.pop();
            }
            CalendarField::Format => {}
            CalendarField::OutputPath => {
                self.output_path.pop();
            }
        }
    }

    pub fn clear_error(&mut self) {
        self.error = None;
    }

    pub fn set_error<S: Into<String>>(&mut self, msg: S) {
        self.error = Some(msg.into());
    }

    pub fn validate(&self) -> Result<(NaiveDate, NaiveDate, CalendarFormat, String)> {
        let start_str = self.start.trim();
        if start_str.is_empty() {
            return Err(anyhow!("Start date is required"));
        }
        let end_str = self.end.trim();
        if end_str.is_empty() {
            return Err(anyhow!("End date is required"));
        }

        let start = NaiveDate::parse_from_str(start_str, "%Y-%m-%d")
            .with_context(|| format!("Invalid start date '{}'", start_str))?;
        let end = NaiveDate::parse_from_str(end_str, "%Y-%m-%d")
            .with_context(|| format!("Invalid end date '{}'", end_str))?;

        if start > end {
            return Err(anyhow!("Start date must be on or before the end date"));
        }

        let format = self.current_format();

        let output_trim = self.output_path.trim();
        let output = if output_trim.is_empty() {
            Self::default_output_filename(format, start, end)
        } else {
            output_trim.to_string()
        };

        Ok((start, end, format, output))
    }

    fn sync_output_extension(&mut self) {
        let extension = Self::format_extension(self.current_format());
        if self.output_path.trim().is_empty() {
            if let (Ok(start), Ok(end)) = (
                NaiveDate::parse_from_str(self.start.trim(), "%Y-%m-%d"),
                NaiveDate::parse_from_str(self.end.trim(), "%Y-%m-%d"),
            ) {
                self.output_path = Self::default_output_filename(self.current_format(), start, end);
            }
            return;
        }

        let mut path = PathBuf::from(self.output_path.trim());
        path.set_extension(extension);
        self.output_path = path.to_string_lossy().to_string();
    }

    fn default_output_filename(format: CalendarFormat, start: NaiveDate, end: NaiveDate) -> String {
        format!(
            "solunatus-calendar-{}-{}.{}",
            start.format("%Y%m%d"),
            end.format("%Y%m%d"),
            Self::format_extension(format)
        )
    }

    fn format_extension(format: CalendarFormat) -> &'static str {
        match format {
            CalendarFormat::Html => "html",
            CalendarFormat::Json => "json",
            CalendarFormat::Ics => "ics",
        }
    }
}

// ============================================================================
// AI Config Draft (feature-gated)
// ============================================================================

#[cfg(feature = "ai-insights")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AiConfigField {
    Enabled,
    Server,
    Model,
    RefreshMinutes,
    RefreshMode,
}

#[cfg(feature = "ai-insights")]
#[derive(Debug, Clone)]
pub enum AiServerStatus {
    Unknown,
    Connected { server: String },
    Failed { server: String, message: String },
}

#[cfg(feature = "ai-insights")]
#[derive(Debug, Clone)]
pub struct AiConfigDraft {
    pub enabled: bool,
    pub server: String,
    pub model: String,
    pub refresh_minutes: String,
    pub refresh_mode: config::AiRefreshMode,
    pub field_index: usize,
    pub error: Option<String>,
    pub server_status: AiServerStatus,
    pub models: Vec<String>,
    pub model_index: Option<usize>,
}

#[cfg(feature = "ai-insights")]
impl AiConfigDraft {
    const FIELD_COUNT: usize = 5;

    pub fn from_config(config: &ai::AiConfig) -> Self {
        Self {
            enabled: config.enabled,
            server: config.server.clone(),
            model: config.model.clone(),
            refresh_minutes: config.refresh_minutes().to_string(),
            refresh_mode: config.refresh_mode,
            field_index: 0,
            error: None,
            server_status: AiServerStatus::Unknown,
            models: Vec::new(),
            model_index: None,
        }
    }

    pub fn sync_from(&mut self, config: &ai::AiConfig) {
        self.enabled = config.enabled;
        self.server = config.server.clone();
        self.model = config.model.clone();
        self.refresh_minutes = config.refresh_minutes().to_string();
        self.refresh_mode = config.refresh_mode;
        self.field_index = 0;
        self.error = None;
        self.reset_detection();
    }

    pub fn current_field(&self) -> AiConfigField {
        match self.field_index {
            0 => AiConfigField::Enabled,
            1 => AiConfigField::Server,
            2 => AiConfigField::Model,
            3 => AiConfigField::RefreshMinutes,
            _ => AiConfigField::RefreshMode,
        }
    }

    pub fn next_field(&mut self) {
        self.field_index = (self.field_index + 1) % Self::FIELD_COUNT;
        self.clear_error();
    }

    pub fn prev_field(&mut self) {
        self.field_index = (self.field_index + Self::FIELD_COUNT - 1) % Self::FIELD_COUNT;
        self.clear_error();
    }

    pub fn toggle_enabled(&mut self) {
        self.enabled = !self.enabled;
        self.clear_error();
    }

    pub fn input_char(&mut self, c: char) {
        self.clear_error();
        match self.current_field() {
            AiConfigField::Enabled => {}
            AiConfigField::Server => {
                self.server.push(c);
                self.mark_server_dirty();
            }
            AiConfigField::Model => {
                self.model.push(c);
                self.model_index = None;
            }
            AiConfigField::RefreshMinutes => {
                if c.is_ascii_digit() && self.refresh_minutes.len() < 2 {
                    self.refresh_minutes.push(c);
                }
            }
            AiConfigField::RefreshMode => {}
        }
    }

    pub fn backspace(&mut self) {
        self.clear_error();
        match self.current_field() {
            AiConfigField::Enabled => {}
            AiConfigField::Server => {
                self.server.pop();
                self.mark_server_dirty();
            }
            AiConfigField::Model => {
                self.model.pop();
                self.model_index = None;
            }
            AiConfigField::RefreshMinutes => {
                self.refresh_minutes.pop();
            }
            AiConfigField::RefreshMode => {}
        }
    }

    pub fn bump_refresh(&mut self, delta: i64) {
        if self.current_field() != AiConfigField::RefreshMinutes {
            return;
        }

        let mut value = self.refresh_minutes.trim().parse::<i64>().unwrap_or(2);
        value += delta;
        value = value.clamp(1, 60);
        self.refresh_minutes = value.to_string();
        self.clear_error();
    }

    pub fn toggle_refresh_mode(&mut self) {
        if self.current_field() != AiConfigField::RefreshMode {
            return;
        }

        self.refresh_mode = match self.refresh_mode {
            config::AiRefreshMode::AutoAndManual => config::AiRefreshMode::ManualOnly,
            config::AiRefreshMode::ManualOnly => config::AiRefreshMode::AutoAndManual,
        };
        self.clear_error();
    }

    pub fn clear_error(&mut self) {
        self.error = None;
    }

    pub fn set_error<S: Into<String>>(&mut self, msg: S) {
        self.error = Some(msg.into());
    }

    pub fn reset_detection(&mut self) {
        self.server_status = AiServerStatus::Unknown;
        self.models.clear();
        self.model_index = None;
    }

    pub fn mark_server_dirty(&mut self) {
        self.reset_detection();
    }

    pub fn set_detection_success(&mut self, server: String, mut models: Vec<String>) {
        self.server_status = AiServerStatus::Connected {
            server: server.clone(),
        };
        self.server = server;
        models.sort();
        models.dedup();
        self.models = models;
        self.clear_error();

        if self.models.is_empty() {
            self.model_index = None;
            return;
        }

        if let Some(idx) = self.models.iter().position(|name| name == &self.model) {
            self.model_index = Some(idx);
            self.model = self.models[idx].clone();
        } else {
            let idx = 0;
            self.model_index = Some(idx);
            self.model = self.models[idx].clone();
        }
    }

    pub fn set_detection_failure(&mut self, server: String, message: String) {
        self.server_status = AiServerStatus::Failed {
            server: server.clone(),
            message,
        };
        self.server = server;
        self.model_index = None;
        self.models.clear();
    }

    pub fn cycle_model(&mut self, delta: isize) {
        if self.models.is_empty() {
            return;
        }

        let len = self.models.len() as isize;
        let current = self.model_index.unwrap_or(0) as isize;
        let mut next = current + delta;
        if next < 0 {
            next = (next % len + len) % len;
        } else {
            next %= len;
        }

        self.model_index = Some(next as usize);
        if let Some(model) = self.models.get(next as usize) {
            self.model = model.clone();
        }
        self.clear_error();
    }
}

// ============================================================================
// Settings Draft
// ============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SettingsField {
    LocationMode,
    TimeSyncEnabled,
    TimeSyncServer,
    ShowLocationDate,
    ShowEvents,
    ShowPositions,
    ShowMoon,
    ShowLunarPhases,
    ShowPlanets,
    NightMode,
    #[cfg(feature = "ai-insights")]
    AiEnabled,
    #[cfg(feature = "ai-insights")]
    AiServer,
    #[cfg(feature = "ai-insights")]
    AiModel,
    #[cfg(feature = "ai-insights")]
    AiRefreshMinutes,
}

#[derive(Debug, Clone)]
pub struct SettingsDraft {
    pub location_mode: config::LocationMode,
    pub time_sync_enabled: bool,
    pub time_sync_server: String,
    pub show_location_date: bool,
    pub show_events: bool,
    pub show_positions: bool,
    pub show_moon: bool,
    pub show_lunar_phases: bool,
    pub show_planets: bool,
    pub night_mode: bool,
    #[cfg(feature = "ai-insights")]
    pub ai_enabled: bool,
    #[cfg(feature = "ai-insights")]
    pub ai_server: String,
    #[cfg(feature = "ai-insights")]
    pub ai_model: String,
    #[cfg(feature = "ai-insights")]
    pub ai_refresh_minutes: String,
    pub field_index: usize,
    pub error: Option<String>,
    #[cfg(feature = "ai-insights")]
    pub ai_server_status: AiServerStatus,
    #[cfg(feature = "ai-insights")]
    pub ai_models: Vec<String>,
    #[cfg(feature = "ai-insights")]
    pub ai_model_index: Option<usize>,
}

impl SettingsDraft {
    #[cfg(feature = "ai-insights")]
    const FIELD_COUNT: usize = 14;
    #[cfg(not(feature = "ai-insights"))]
    const FIELD_COUNT: usize = 10;

    pub fn current_field(&self) -> SettingsField {
        match self.field_index {
            0 => SettingsField::LocationMode,
            1 => SettingsField::TimeSyncEnabled,
            2 => SettingsField::TimeSyncServer,
            3 => SettingsField::ShowLocationDate,
            4 => SettingsField::ShowEvents,
            5 => SettingsField::ShowPositions,
            6 => SettingsField::ShowMoon,
            7 => SettingsField::ShowLunarPhases,
            8 => SettingsField::ShowPlanets,
            9 => SettingsField::NightMode,
            #[cfg(feature = "ai-insights")]
            10 => SettingsField::AiEnabled,
            #[cfg(feature = "ai-insights")]
            11 => SettingsField::AiServer,
            #[cfg(feature = "ai-insights")]
            12 => SettingsField::AiModel,
            #[cfg(feature = "ai-insights")]
            _ => SettingsField::AiRefreshMinutes,
            #[cfg(not(feature = "ai-insights"))]
            _ => SettingsField::NightMode,
        }
    }

    pub fn next_field(&mut self) {
        self.field_index = (self.field_index + 1) % Self::FIELD_COUNT;
        self.clear_error();
    }

    pub fn prev_field(&mut self) {
        self.field_index = (self.field_index + Self::FIELD_COUNT - 1) % Self::FIELD_COUNT;
        self.clear_error();
    }

    pub fn toggle_current_bool(&mut self) {
        match self.current_field() {
            SettingsField::TimeSyncEnabled => self.time_sync_enabled = !self.time_sync_enabled,
            SettingsField::ShowLocationDate => self.show_location_date = !self.show_location_date,
            SettingsField::ShowEvents => self.show_events = !self.show_events,
            SettingsField::ShowPositions => self.show_positions = !self.show_positions,
            SettingsField::ShowMoon => self.show_moon = !self.show_moon,
            SettingsField::ShowLunarPhases => self.show_lunar_phases = !self.show_lunar_phases,
            SettingsField::ShowPlanets => self.show_planets = !self.show_planets,
            SettingsField::NightMode => self.night_mode = !self.night_mode,
            #[cfg(feature = "ai-insights")]
            SettingsField::AiEnabled => self.ai_enabled = !self.ai_enabled,
            _ => {}
        }
        self.clear_error();
    }

    pub fn cycle_location_mode(&mut self) {
        self.location_mode = match self.location_mode {
            config::LocationMode::City => config::LocationMode::Manual,
            config::LocationMode::Manual => config::LocationMode::City,
        };
        self.clear_error();
    }

    pub fn input_char(&mut self, c: char) {
        self.clear_error();
        match self.current_field() {
            SettingsField::TimeSyncServer => {
                self.time_sync_server.push(c);
            }
            #[cfg(feature = "ai-insights")]
            SettingsField::AiServer => {
                self.ai_server.push(c);
            }
            #[cfg(feature = "ai-insights")]
            SettingsField::AiModel => {
                self.ai_model.push(c);
            }
            #[cfg(feature = "ai-insights")]
            SettingsField::AiRefreshMinutes
                if c.is_ascii_digit() && self.ai_refresh_minutes.len() < 2 =>
            {
                self.ai_refresh_minutes.push(c);
            }
            _ => {}
        }
    }

    pub fn backspace(&mut self) {
        self.clear_error();
        match self.current_field() {
            SettingsField::TimeSyncServer => {
                self.time_sync_server.pop();
            }
            #[cfg(feature = "ai-insights")]
            SettingsField::AiServer => {
                self.ai_server.pop();
            }
            #[cfg(feature = "ai-insights")]
            SettingsField::AiModel => {
                self.ai_model.pop();
            }
            #[cfg(feature = "ai-insights")]
            SettingsField::AiRefreshMinutes => {
                self.ai_refresh_minutes.pop();
            }
            _ => {}
        }
    }

    pub fn clear_error(&mut self) {
        self.error = None;
    }

    pub fn set_error<S: Into<String>>(&mut self, msg: S) {
        self.error = Some(msg.into());
    }
}