cloudflare-dns 0.1.5

A TUI for managing Cloudflare DNS records programmatically
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
#![allow(dead_code)]

/// Status message types and rendering.
use crate::ui::state::AppView;

/// Represents the type of status message
#[derive(Debug, Clone, PartialEq)]
pub enum StatusType {
    /// Transient messages that should display briefly
    Transient,
    /// Persistent contextual help based on current view
    Contextual,
}

/// Context for form field help messages
#[derive(Debug, Clone)]
pub enum FormFieldContext {
    Type,
    Name,
    Content,
    Ttl,
    Proxied,
    Submit,
}

/// All possible status message variants
#[derive(Debug, Clone)]
pub enum StatusMessage {
    /// Initial loading state
    Initializing,
    /// Loading DNS records
    LoadingRecords,
    /// Error message
    Error(String),
    /// Operation results (created, updated, deleted, etc.)
    OperationResult(String),
    /// View-specific contextual help
    ViewHelp(ViewHelpContext),
    /// Form field help
    FormFieldHelp {
        context: FormFieldContext,
        form_type: String,
        form_proxied: String,
        is_editing: bool,
    },
    /// Record list navigation help
    RecordListHelp {
        position: usize,
        total: usize,
        record_name: String,
    },
    /// Empty list help
    EmptyListHelp,
}

/// View-specific help contexts
#[derive(Debug, Clone)]
pub enum ViewHelpContext {
    DeleteConfirmation,
    IpSelector,
}

impl StatusMessage {
    /// Determine if this status should be transient (auto-clearing)
    pub fn status_type(&self) -> StatusType {
        match self {
            StatusMessage::Error(_)
            | StatusMessage::OperationResult(_)
            | StatusMessage::Initializing
            | StatusMessage::LoadingRecords => StatusType::Transient,
            _ => StatusType::Contextual,
        }
    }

    /// Check if a status string represents a transient message
    pub fn is_transient(status_str: &str) -> bool {
        status_str.starts_with("Error:")
            || status_str.starts_with("Refreshing...")
            || status_str.starts_with("Created")
            || status_str.starts_with("Updated")
            || status_str.starts_with("Deleted")
            || status_str.starts_with("Failed")
            || status_str.starts_with("Selected")
            || status_str.starts_with("Cancelled")
    }

    /// Render the status message to a display string
    pub fn render(&self) -> String {
        match self {
            StatusMessage::Initializing => "Initializing...".to_string(),
            StatusMessage::LoadingRecords => "Loading DNS records...".to_string(),
            StatusMessage::Error(msg) => format!("Error: {}", msg),
            StatusMessage::OperationResult(msg) => msg.clone(),
            StatusMessage::ViewHelp(ViewHelpContext::DeleteConfirmation) => {
                "Enter: confirm deletion | Esc: cancel".to_string()
            }
            StatusMessage::ViewHelp(ViewHelpContext::IpSelector) => {
                "↑↓: navigate | Enter: select IP | Esc: back to form".to_string()
            }
            StatusMessage::FormFieldHelp {
                context,
                form_type,
                form_proxied,
                is_editing,
            } => match context {
                FormFieldContext::Type => {
                    format!(
                        "Field 1/6 — Type: {} | Press Space to cycle types",
                        form_type
                    )
                }
                FormFieldContext::Name => "Field 2/6 — Name: e.g. nginx".to_string(),
                FormFieldContext::Content => {
                    "Field 3/6 — IP Address | Press Space to use an existing address | Type: enter IP"
                        .to_string()
                }
                FormFieldContext::Ttl => "Field 4/6 — TTL: seconds (1 = auto)".to_string(),
                FormFieldContext::Proxied => {
                    let proxied_status = if form_proxied == "true" {
                        "Orange cloud ON"
                    } else {
                        "Grey cloud OFF"
                    };
                    format!(
                        "Field 5/6 — Proxied: {} | Press Space to toggle",
                        proxied_status
                    )
                }
                FormFieldContext::Submit => {
                    let action = if *is_editing { "Save" } else { "Create" };
                    format!("Field 6/6 — Press Enter to {} record", action)
                }
            },
            StatusMessage::RecordListHelp {
                position,
                total,
                record_name,
            } => {
                format!(
                    "{} of {} — {} | e: edit | d: delete | r: refresh | c: create | q: quit",
                    position, total, record_name
                )
            }
            StatusMessage::EmptyListHelp => {
                "No records | c: create your first DNS record | q: quit".to_string()
            }
        }
    }
}

/// Generate the appropriate contextual status message based on current state
#[allow(clippy::too_many_arguments)]
pub fn generate_contextual_status(
    view: &AppView,
    form_focus: usize,
    form_type: &str,
    form_proxied: &str,
    is_editing: bool,
    record_count: usize,
    selected_record_idx: usize,
    selected_record_name: Option<&str>,
) -> StatusMessage {
    match view {
        AppView::Delete => StatusMessage::ViewHelp(ViewHelpContext::DeleteConfirmation),
        AppView::IpSelect => StatusMessage::ViewHelp(ViewHelpContext::IpSelector),
        AppView::Create | AppView::Edit => {
            let context = match form_focus {
                0 => FormFieldContext::Type,
                1 => FormFieldContext::Name,
                2 => FormFieldContext::Content,
                3 => FormFieldContext::Ttl,
                4 => FormFieldContext::Proxied,
                5 => FormFieldContext::Submit,
                _ => FormFieldContext::Name, // fallback
            };
            StatusMessage::FormFieldHelp {
                context,
                form_type: form_type.to_string(),
                form_proxied: form_proxied.to_string(),
                is_editing,
            }
        }
        AppView::List => {
            if record_count > 0 && selected_record_idx < record_count {
                StatusMessage::RecordListHelp {
                    position: selected_record_idx + 1,
                    total: record_count,
                    record_name: selected_record_name.unwrap_or("Unknown").to_string(),
                }
            } else {
                StatusMessage::EmptyListHelp
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_status_type_transient_error() {
        let status = StatusMessage::Error("test error".to_string());
        assert_eq!(status.status_type(), StatusType::Transient);
    }

    #[test]
    fn test_status_type_transient_operation_result() {
        let status = StatusMessage::OperationResult("Created A for example".to_string());
        assert_eq!(status.status_type(), StatusType::Transient);
    }

    #[test]
    fn test_status_type_transient_initializing() {
        let status = StatusMessage::Initializing;
        assert_eq!(status.status_type(), StatusType::Transient);
    }

    #[test]
    fn test_status_type_transient_loading_records() {
        let status = StatusMessage::LoadingRecords;
        assert_eq!(status.status_type(), StatusType::Transient);
    }

    #[test]
    fn test_status_type_contextual_view_help() {
        let status = StatusMessage::ViewHelp(ViewHelpContext::DeleteConfirmation);
        assert_eq!(status.status_type(), StatusType::Contextual);
    }

    #[test]
    fn test_status_type_contextual_form_field_help() {
        let status = StatusMessage::FormFieldHelp {
            context: FormFieldContext::Name,
            form_type: "A".to_string(),
            form_proxied: "false".to_string(),
            is_editing: false,
        };
        assert_eq!(status.status_type(), StatusType::Contextual);
    }

    #[test]
    fn test_status_type_contextual_record_list_help() {
        let status = StatusMessage::RecordListHelp {
            position: 1,
            total: 5,
            record_name: "example.com".to_string(),
        };
        assert_eq!(status.status_type(), StatusType::Contextual);
    }

    #[test]
    fn test_status_type_contextual_empty_list_help() {
        let status = StatusMessage::EmptyListHelp;
        assert_eq!(status.status_type(), StatusType::Contextual);
    }

    #[test]
    fn test_is_transient_error() {
        assert!(StatusMessage::is_transient("Error: something went wrong"));
    }

    #[test]
    fn test_is_transient_created() {
        assert!(StatusMessage::is_transient("Created A for example"));
    }

    #[test]
    fn test_is_transient_updated() {
        assert!(StatusMessage::is_transient("Updated A for example"));
    }

    #[test]
    fn test_is_transient_deleted() {
        assert!(StatusMessage::is_transient("Deleted A for example"));
    }

    #[test]
    fn test_is_transient_failed() {
        assert!(StatusMessage::is_transient("Failed: API error"));
    }

    #[test]
    fn test_is_transient_selected() {
        assert!(StatusMessage::is_transient("Selected IP address"));
    }

    #[test]
    fn test_is_transient_cancelled() {
        assert!(StatusMessage::is_transient("Cancelled operation"));
    }

    #[test]
    fn test_is_not_transient_contextual() {
        assert!(!StatusMessage::is_transient(
            "Enter: confirm deletion | Esc: cancel"
        ));
    }

    #[test]
    fn test_is_not_transient_record_list() {
        assert!(!StatusMessage::is_transient(
            "1 of 5 — example.com | E: edit | D: delete | R: refresh | C: create | Q: quit"
        ));
    }

    #[test]
    fn test_render_initializing() {
        let status = StatusMessage::Initializing;
        assert_eq!(status.render(), "Initializing...");
    }

    #[test]
    fn test_render_loading_records() {
        let status = StatusMessage::LoadingRecords;
        assert_eq!(status.render(), "Loading DNS records...");
    }

    #[test]
    fn test_render_error() {
        let status = StatusMessage::Error("connection failed".to_string());
        assert_eq!(status.render(), "Error: connection failed");
    }

    #[test]
    fn test_render_operation_result() {
        let status = StatusMessage::OperationResult("Created A record".to_string());
        assert_eq!(status.render(), "Created A record");
    }

    #[test]
    fn test_render_delete_confirmation_help() {
        let status = StatusMessage::ViewHelp(ViewHelpContext::DeleteConfirmation);
        assert_eq!(status.render(), "Enter: confirm deletion | Esc: cancel");
    }

    #[test]
    fn test_render_ip_selector_help() {
        let status = StatusMessage::ViewHelp(ViewHelpContext::IpSelector);
        assert_eq!(
            status.render(),
            "↑↓: navigate | Enter: select IP | Esc: back to form"
        );
    }

    #[test]
    fn test_render_form_field_type() {
        let status = StatusMessage::FormFieldHelp {
            context: FormFieldContext::Type,
            form_type: "A".to_string(),
            form_proxied: "false".to_string(),
            is_editing: false,
        };
        assert_eq!(
            status.render(),
            "Field 1/6 — Type: A | Press Space to cycle types"
        );
    }

    #[test]
    fn test_render_form_field_name() {
        let status = StatusMessage::FormFieldHelp {
            context: FormFieldContext::Name,
            form_type: "A".to_string(),
            form_proxied: "false".to_string(),
            is_editing: false,
        };
        assert_eq!(status.render(), "Field 2/6 — Name: e.g. nginx");
    }

    #[test]
    fn test_render_form_field_content() {
        let status = StatusMessage::FormFieldHelp {
            context: FormFieldContext::Content,
            form_type: "A".to_string(),
            form_proxied: "false".to_string(),
            is_editing: false,
        };
        assert_eq!(
            status.render(),
            "Field 3/6 — IP Address | Press Space to use an existing address | Type: enter IP"
        );
    }

    #[test]
    fn test_render_form_field_ttl() {
        let status = StatusMessage::FormFieldHelp {
            context: FormFieldContext::Ttl,
            form_type: "A".to_string(),
            form_proxied: "false".to_string(),
            is_editing: false,
        };
        assert_eq!(status.render(), "Field 4/6 — TTL: seconds (1 = auto)");
    }

    #[test]
    fn test_render_form_field_proxied_true() {
        let status = StatusMessage::FormFieldHelp {
            context: FormFieldContext::Proxied,
            form_type: "A".to_string(),
            form_proxied: "true".to_string(),
            is_editing: false,
        };
        assert_eq!(
            status.render(),
            "Field 5/6 — Proxied: Orange cloud ON | Press Space to toggle"
        );
    }

    #[test]
    fn test_render_form_field_proxied_false() {
        let status = StatusMessage::FormFieldHelp {
            context: FormFieldContext::Proxied,
            form_type: "A".to_string(),
            form_proxied: "false".to_string(),
            is_editing: false,
        };
        assert_eq!(
            status.render(),
            "Field 5/6 — Proxied: Grey cloud OFF | Press Space to toggle"
        );
    }

    #[test]
    fn test_render_form_field_submit_create() {
        let status = StatusMessage::FormFieldHelp {
            context: FormFieldContext::Submit,
            form_type: "A".to_string(),
            form_proxied: "false".to_string(),
            is_editing: false,
        };
        assert_eq!(status.render(), "Field 6/6 — Press Enter to Create record");
    }

    #[test]
    fn test_render_form_field_submit_edit() {
        let status = StatusMessage::FormFieldHelp {
            context: FormFieldContext::Submit,
            form_type: "A".to_string(),
            form_proxied: "false".to_string(),
            is_editing: true,
        };
        assert_eq!(status.render(), "Field 6/6 — Press Enter to Save record");
    }

    #[test]
    fn test_render_record_list_help() {
        let status = StatusMessage::RecordListHelp {
            position: 3,
            total: 10,
            record_name: "test.example.com".to_string(),
        };
        assert_eq!(
            status.render(),
            "3 of 10 — test.example.com | e: edit | d: delete | r: refresh | c: create | q: quit"
        );
    }

    #[test]
    fn test_render_empty_list_help() {
        let status = StatusMessage::EmptyListHelp;
        assert_eq!(
            status.render(),
            "No records | c: create your first DNS record | q: quit"
        );
    }

    #[test]
    fn test_generate_contextual_status_delete_view() {
        let result = generate_contextual_status(
            &AppView::Delete,
            0,
            "A",
            "false",
            false,
            5,
            0,
            Some("example.com"),
        );
        assert!(matches!(
            result,
            StatusMessage::ViewHelp(ViewHelpContext::DeleteConfirmation)
        ));
    }

    #[test]
    fn test_generate_contextual_status_ip_selector_view() {
        let result = generate_contextual_status(
            &AppView::IpSelect,
            0,
            "A",
            "false",
            false,
            5,
            0,
            Some("example.com"),
        );
        assert!(matches!(
            result,
            StatusMessage::ViewHelp(ViewHelpContext::IpSelector)
        ));
    }

    #[test]
    fn test_generate_contextual_status_create_view_first_field() {
        let result = generate_contextual_status(
            &AppView::Create,
            0,
            "A",
            "false",
            false,
            5,
            0,
            Some("example.com"),
        );
        match result {
            StatusMessage::FormFieldHelp {
                context: FormFieldContext::Type,
                form_type,
                ..
            } => {
                assert_eq!(form_type, "A");
            }
            _ => panic!("Expected FormFieldHelp with Type context"),
        }
    }

    #[test]
    fn test_generate_contextual_status_create_view_name_field() {
        let result = generate_contextual_status(
            &AppView::Create,
            1,
            "AAAA",
            "true",
            false,
            5,
            0,
            Some("example.com"),
        );
        match result {
            StatusMessage::FormFieldHelp {
                context: FormFieldContext::Name,
                form_type,
                form_proxied,
                ..
            } => {
                assert_eq!(form_type, "AAAA");
                assert_eq!(form_proxied, "true");
            }
            _ => panic!("Expected FormFieldHelp with Name context"),
        }
    }

    #[test]
    fn test_generate_contextual_status_edit_view_submit_field() {
        let result = generate_contextual_status(
            &AppView::Edit,
            5,
            "CNAME",
            "false",
            true,
            5,
            0,
            Some("example.com"),
        );
        match result {
            StatusMessage::FormFieldHelp {
                context: FormFieldContext::Submit,
                is_editing,
                ..
            } => {
                assert!(is_editing);
            }
            _ => panic!("Expected FormFieldHelp with Submit context"),
        }
    }

    #[test]
    fn test_generate_contextual_status_list_view_with_records() {
        let result = generate_contextual_status(
            &AppView::List,
            0,
            "A",
            "false",
            false,
            5,
            2,
            Some("test.example.com"),
        );
        match result {
            StatusMessage::RecordListHelp {
                position,
                total,
                record_name,
            } => {
                assert_eq!(position, 3); // idx + 1
                assert_eq!(total, 5);
                assert_eq!(record_name, "test.example.com");
            }
            _ => panic!("Expected RecordListHelp"),
        }
    }

    #[test]
    fn test_generate_contextual_status_list_view_empty() {
        let result = generate_contextual_status(&AppView::List, 0, "A", "false", false, 0, 0, None);
        assert!(matches!(result, StatusMessage::EmptyListHelp));
    }

    #[test]
    fn test_generate_contextual_status_list_view_invalid_selection() {
        // Selection index beyond record count
        let result = generate_contextual_status(
            &AppView::List,
            0,
            "A",
            "false",
            false,
            3,
            5, // idx >= record_count
            Some("example.com"),
        );
        assert!(matches!(result, StatusMessage::EmptyListHelp));
    }

    #[test]
    fn test_generate_contextual_status_list_view_unknown_record_name() {
        let result = generate_contextual_status(
            &AppView::List,
            0,
            "A",
            "false",
            false,
            1,
            0,
            None, // No record name
        );
        match result {
            StatusMessage::RecordListHelp { record_name, .. } => {
                assert_eq!(record_name, "Unknown");
            }
            _ => panic!("Expected RecordListHelp"),
        }
    }

    #[test]
    fn test_generate_contextual_status_form_fallback() {
        // form_focus beyond 0-5 range should fallback to Name context
        let result = generate_contextual_status(
            &AppView::Create,
            10, // out of range
            "A",
            "false",
            false,
            5,
            0,
            Some("example.com"),
        );
        match result {
            StatusMessage::FormFieldHelp {
                context: FormFieldContext::Name,
                ..
            } => {
                // Expected fallback behavior
            }
            _ => panic!("Expected FormFieldHelp with Name context as fallback"),
        }
    }
}