qbrsh 0.2.0

A fast, keyboard-driven web browser
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
//! The single, owned application state.
//!
//! All mutable application state lives here as plain owned fields. There is no
//! `Rc<RefCell<_>>` sharing of state across subsystems; the dispatch loop holds
//! the sole `&mut State`. Subsystems are added to [`State`] as they are ported;
//! the skeleton establishes the ownership shape with the fields the core already
//! exercises (mode, tabs, input, command line, status, config).

use std::collections::{BTreeMap, HashMap, VecDeque};

use crate::core::bindings::default_bindings;
use crate::core::command::HintTarget;
use crate::core::completion::CompletionState;
use crate::core::key::Key;
use crate::core::msg::{JsPurpose, RequestId};
use crate::core::trie::BindingTrie;

/// Input modes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    Normal,
    Insert,
    Command,
    Hint,
    /// Answering an interactive permission prompt.
    Prompt,
    /// Browsing the permission management list.
    Permissions,
}

/// Tracks the current mode and the one to return to on leave.
#[derive(Debug, Clone, Copy)]
pub struct ModeState {
    pub current: Mode,
    pub previous: Mode,
}

impl Default for ModeState {
    fn default() -> Self {
        Self {
            current: Mode::Normal,
            previous: Mode::Normal,
        }
    }
}

impl ModeState {
    /// Enter a new mode, remembering the current one as previous.
    pub fn enter(&mut self, mode: Mode) {
        if mode != self.current {
            self.previous = self.current;
            self.current = mode;
        }
    }

    /// Leave the current mode, returning to Normal.
    pub fn leave(&mut self) {
        self.previous = self.current;
        self.current = Mode::Normal;
    }
}

/// Stable identifier for a tab, shared between the state model and the engine's
/// web views.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TabId(pub u64);

/// A single tab's model. The actual web view is owned by the engine layer and
/// correlated to this model by [`TabId`].
#[derive(Debug, Clone)]
pub struct Tab {
    pub id: TabId,
    pub url: String,
    pub title: String,
    pub loading: bool,
    pub progress: f64,
    pub crashed: bool,
    /// Page zoom level (1.0 = 100%).
    pub zoom: f64,
}

impl Tab {
    fn new(id: TabId, url: &str) -> Self {
        Self {
            id,
            url: url.to_string(),
            title: String::new(),
            loading: false,
            progress: 0.0,
            crashed: false,
            zoom: 1.0,
        }
    }
}

/// A recently-closed tab, retained so it can be reopened with `undo`.
#[derive(Debug, Clone)]
pub struct ClosedTab {
    pub url: String,
}

/// Maximum number of closed tabs retained for undo.
const UNDO_LIMIT: usize = 100;

/// The ordered set of open tabs plus the active selection.
#[derive(Debug, Default)]
pub struct Tabs {
    tabs: Vec<Tab>,
    active: usize,
    next_id: u64,
    undo_stack: Vec<ClosedTab>,
}

impl Tabs {
    /// Create a tab model for `url` and return its id. Does not change focus.
    pub fn open(&mut self, url: &str) -> TabId {
        let id = TabId(self.next_id);
        self.next_id += 1;
        self.tabs.push(Tab::new(id, url));
        id
    }

    /// Number of open tabs.
    pub fn len(&self) -> usize {
        self.tabs.len()
    }

    /// The active tab, if any.
    pub fn active(&self) -> Option<&Tab> {
        self.tabs.get(self.active)
    }

    /// The id of the active tab, if any.
    pub fn active_id(&self) -> Option<TabId> {
        self.active().map(|t| t.id)
    }

    /// The zero-based index of the active tab. Consumed by the tab-bar renderer.
    pub fn active_index(&self) -> usize {
        self.active
    }

    /// Mutable access to a tab by id.
    pub fn get_mut(&mut self, id: TabId) -> Option<&mut Tab> {
        self.tabs.iter_mut().find(|t| t.id == id)
    }

    /// Borrow the tab with the given id, if present.
    pub fn get(&self, id: TabId) -> Option<&Tab> {
        self.tabs.iter().find(|t| t.id == id)
    }

    /// Focus the last tab (used after opening a foreground tab).
    pub fn focus_last(&mut self) {
        if !self.tabs.is_empty() {
            self.active = self.tabs.len() - 1;
        }
    }

    /// Focus a tab by 1-based index. Returns the focused tab id, if valid.
    pub fn focus_index_1based(&mut self, index: usize) -> Option<TabId> {
        let idx = index.checked_sub(1)?;
        let tab = self.tabs.get(idx)?;
        self.active = idx;
        Some(tab.id)
    }

    /// Move focus forward `count` tabs, wrapping. Returns the new active id.
    pub fn next(&mut self, count: u32) -> Option<TabId> {
        if self.tabs.is_empty() {
            return None;
        }
        self.active = (self.active + count as usize) % self.tabs.len();
        self.active_id()
    }

    /// Move focus backward `count` tabs, wrapping. Returns the new active id.
    pub fn prev(&mut self, count: u32) -> Option<TabId> {
        if self.tabs.is_empty() {
            return None;
        }
        let len = self.tabs.len();
        let back = (count as usize) % len;
        self.active = (self.active + len - back) % len;
        self.active_id()
    }

    /// Close the active tab, retaining it for undo. Returns its id and the id to
    /// focus next, if any.
    pub fn close_active(&mut self) -> Option<(TabId, Option<TabId>)> {
        if self.tabs.is_empty() {
            return None;
        }
        let closed = self.tabs.remove(self.active);
        self.push_undo(&closed);
        if self.active >= self.tabs.len() && !self.tabs.is_empty() {
            self.active = self.tabs.len() - 1;
        }
        let next = self.active_id();
        Some((closed.id, next))
    }

    /// Close all tabs except the active one, retaining them for undo. Returns the
    /// ids of the closed tabs.
    pub fn close_others(&mut self) -> Vec<TabId> {
        if self.tabs.len() < 2 {
            return Vec::new();
        }
        let kept = self.tabs.swap_remove(self.active);
        let removed = std::mem::take(&mut self.tabs);
        let closed_ids = removed.iter().map(|t| t.id).collect();
        for tab in &removed {
            self.push_undo(tab);
        }
        self.tabs = vec![kept];
        self.active = 0;
        closed_ids
    }

    /// Move the active tab by `delta` positions, clamped to the ends. Returns
    /// true if the order changed.
    pub fn move_active(&mut self, delta: i32) -> bool {
        let len = self.tabs.len();
        if len < 2 {
            return false;
        }
        let target = (self.active as i32 + delta).clamp(0, len as i32 - 1) as usize;
        if target == self.active {
            return false;
        }
        let tab = self.tabs.remove(self.active);
        self.tabs.insert(target, tab);
        self.active = target;
        true
    }

    /// Pop the most recently closed tab for reopening.
    pub fn undo(&mut self) -> Option<ClosedTab> {
        self.undo_stack.pop()
    }

    /// The URLs of all open tabs, in order.
    pub fn urls(&self) -> Vec<String> {
        self.tabs.iter().map(|t| t.url.clone()).collect()
    }

    fn push_undo(&mut self, tab: &Tab) {
        self.undo_stack.push(ClosedTab {
            url: tab.url.clone(),
        });
        if self.undo_stack.len() > UNDO_LIMIT {
            self.undo_stack.remove(0);
        }
    }
}

/// The command-line input state.
#[derive(Debug, Default)]
pub struct CommandLine {
    pub text: String,
    pub active: bool,
}

/// Pending key input: the partial key sequence and the count prefix.
#[derive(Debug, Default)]
pub struct InputState {
    pub pending: Vec<Key>,
    pub count: String,
}

/// Transient status-bar state not derived directly from the active tab.
#[derive(Debug, Default)]
pub struct StatusLine {
    pub scroll_percent: Option<u8>,
    /// Current in-page search position, shown until cleared.
    pub search: Option<SearchStatus>,
}

/// In-page search result for the status line: the total match count, or `None`
/// before the count arrives. WebKit's native find does not expose a reliable
/// current-match index, so only the total is shown.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchStatus {
    pub total: Option<usize>,
}

impl SearchStatus {
    /// Format as `N matches`, `searching` before the count, or `no matches`.
    pub fn label(&self) -> String {
        match self.total {
            None => "searching".to_string(),
            Some(0) => "no matches".to_string(),
            Some(1) => "1 match".to_string(),
            Some(n) => format!("{n} matches"),
        }
    }
}

/// Active hint-mode state: the follow action, the available labels, and the
/// label characters typed so far.
#[derive(Debug, Default)]
pub struct HintState {
    pub target: HintTarget,
    pub labels: Vec<String>,
    pub input: String,
}

impl HintState {
    /// Reset to an empty, inactive hint state.
    pub fn reset(&mut self) {
        self.labels.clear();
        self.input.clear();
    }
}

/// A saved bookmark.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Bookmark {
    pub url: String,
    pub title: String,
}

/// Chrome colors (CSS color strings).
#[derive(Debug, Clone, serde::Deserialize)]
#[serde(default)]
pub struct Colors {
    pub background: String,
    pub foreground: String,
    pub accent: String,
}

impl Default for Colors {
    fn default() -> Self {
        Self {
            background: "#1a1a2e".to_string(),
            foreground: "#e0e0e0".to_string(),
            accent: "#ffd76e".to_string(),
        }
    }
}

/// Chrome font.
#[derive(Debug, Clone, serde::Deserialize)]
#[serde(default)]
pub struct Font {
    pub family: String,
    pub size: u32,
}

impl Default for Font {
    fn default() -> Self {
        Self {
            family: "monospace".to_string(),
            size: 11,
        }
    }
}

/// Page zoom configuration.
#[derive(Debug, Clone, serde::Deserialize)]
#[serde(default)]
pub struct Zoom {
    /// Default zoom level applied to new tabs (1.0 = 100%).
    pub default: f64,
}

impl Default for Zoom {
    fn default() -> Self {
        Self { default: 1.0 }
    }
}

/// How a site permission request is answered.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PermissionPolicy {
    /// Prompt the user with the interactive permission prompt.
    #[default]
    Ask,
    Allow,
    Deny,
}

impl PermissionPolicy {
    /// Parse a policy from a `:set` value.
    pub fn parse(value: &str) -> Result<Self, String> {
        match value {
            "ask" => Ok(Self::Ask),
            "allow" => Ok(Self::Allow),
            "deny" => Ok(Self::Deny),
            other => Err(format!("invalid permission policy: {other}")),
        }
    }
}

/// A capability a page can request, decided independently per site.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Capability {
    Geolocation,
    Notifications,
    Camera,
    Microphone,
}

impl Capability {
    /// Every capability, in display order.
    pub const ALL: [Capability; 4] = [
        Capability::Geolocation,
        Capability::Notifications,
        Capability::Camera,
        Capability::Microphone,
    ];

    /// The lowercase name used in config keys and display.
    pub fn as_str(self) -> &'static str {
        match self {
            Capability::Geolocation => "geolocation",
            Capability::Notifications => "notifications",
            Capability::Camera => "camera",
            Capability::Microphone => "microphone",
        }
    }

    /// Parse a capability name from a config key.
    pub fn parse(value: &str) -> Option<Self> {
        Capability::ALL.into_iter().find(|c| c.as_str() == value)
    }
}

/// A site's permission rules: either one policy for all capabilities (the
/// backward-compatible bare form) or an explicit per-capability map.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum SiteRules {
    /// One policy applied to every capability (legacy bare-string form).
    All(PermissionPolicy),
    /// An explicit policy per capability.
    PerCapability(BTreeMap<Capability, PermissionPolicy>),
}

impl SiteRules {
    /// The policy this site defines for `cap`, if any.
    fn get(&self, cap: Capability) -> Option<PermissionPolicy> {
        match self {
            SiteRules::All(p) => Some(*p),
            SiteRules::PerCapability(m) => m.get(&cap).copied(),
        }
    }

    /// Set `cap` to `policy`, expanding a bare `All` rule into a per-capability
    /// map first so other capabilities keep their effective policy.
    fn set(&mut self, cap: Capability, policy: PermissionPolicy) {
        if let SiteRules::All(p) = *self {
            let mut m: BTreeMap<Capability, PermissionPolicy> =
                Capability::ALL.into_iter().map(|c| (c, p)).collect();
            m.insert(cap, policy);
            *self = SiteRules::PerCapability(m);
        } else if let SiteRules::PerCapability(m) = self {
            m.insert(cap, policy);
        }
    }
}

/// Per-site permission policy: a default plus host-suffix-keyed, per-capability
/// overrides.
#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct Permissions {
    pub default: PermissionPolicy,
    pub sites: BTreeMap<String, SiteRules>,
}

impl Permissions {
    /// Resolve the policy for `host` and `cap`, matching a site rule by exact
    /// host or subdomain suffix, else the default.
    pub fn policy_for(&self, host: &str, cap: Capability) -> PermissionPolicy {
        for (site, rules) in &self.sites {
            if (host == site.as_str() || host.ends_with(&format!(".{site}")))
                && let Some(p) = rules.get(cap)
            {
                return p;
            }
        }
        self.default
    }

    /// Set the policy for a single capability on `host`.
    pub fn set_capability(&mut self, host: &str, cap: Capability, policy: PermissionPolicy) {
        self.sites
            .entry(host.to_string())
            .or_insert_with(|| SiteRules::PerCapability(BTreeMap::new()))
            .set(cap, policy);
    }

    /// Set one policy for every capability on `host`.
    pub fn set_all(&mut self, host: &str, policy: PermissionPolicy) {
        self.sites.insert(host.to_string(), SiteRules::All(policy));
    }

    /// Flatten the rules into display rows: one row per `All` site, one per
    /// capability for per-capability sites, sorted by host then capability.
    pub fn rows(&self) -> Vec<PermissionRow> {
        let mut rows = Vec::new();
        for (host, rules) in &self.sites {
            match rules {
                SiteRules::All(p) => rows.push(PermissionRow {
                    host: host.clone(),
                    capability: None,
                    policy: *p,
                }),
                SiteRules::PerCapability(m) => {
                    for (cap, p) in m {
                        rows.push(PermissionRow {
                            host: host.clone(),
                            capability: Some(*cap),
                            policy: *p,
                        });
                    }
                }
            }
        }
        rows
    }

    /// Apply a row's policy (used when cycling in the management view).
    pub fn set_row(&mut self, row: &PermissionRow, policy: PermissionPolicy) {
        match row.capability {
            Some(cap) => self.set_capability(&row.host, cap, policy),
            None => self.set_all(&row.host, policy),
        }
    }

    /// Remove a row's rule, reverting it to the default.
    pub fn revoke_row(&mut self, row: &PermissionRow) {
        match row.capability {
            None => {
                self.sites.remove(&row.host);
            }
            Some(cap) => {
                if let Some(SiteRules::PerCapability(m)) = self.sites.get_mut(&row.host) {
                    m.remove(&cap);
                    if m.is_empty() {
                        self.sites.remove(&row.host);
                    }
                }
            }
        }
    }
}

/// A single flattened permission rule for the management view.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PermissionRow {
    pub host: String,
    pub capability: Option<Capability>,
    pub policy: PermissionPolicy,
}

/// A deferred permission request awaiting the user's decision.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PermissionPrompt {
    pub id: u64,
    pub host: String,
    pub capability: Capability,
}

/// State of the permission management list view.
#[derive(Debug, Default)]
pub struct PermissionViewState {
    pub rows: Vec<PermissionRow>,
    pub selected: usize,
}

/// User configuration, deserialized from TOML and adjustable at runtime.
#[derive(Debug, Clone, serde::Deserialize)]
#[serde(default)]
pub struct Config {
    pub homepage: String,
    pub colors: Colors,
    pub font: Font,
    pub zoom: Zoom,
    pub permissions: Permissions,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            homepage: "https://duckduckgo.com".to_string(),
            colors: Colors::default(),
            font: Font::default(),
            zoom: Zoom::default(),
            permissions: Permissions::default(),
        }
    }
}

impl Config {
    /// Set a configuration value by dotted key at runtime. Returns an error for
    /// unknown keys or invalid values.
    pub fn set(&mut self, key: &str, value: &str) -> Result<(), String> {
        match key {
            "homepage" | "general.homepage" => self.homepage = value.to_string(),
            "colors.background" => self.colors.background = value.to_string(),
            "colors.foreground" => self.colors.foreground = value.to_string(),
            "colors.accent" => self.colors.accent = value.to_string(),
            "font.family" => self.font.family = value.to_string(),
            "font.size" => {
                self.font.size = value
                    .parse()
                    .map_err(|_| format!("invalid font.size: {value}"))?
            }
            "zoom.default" => {
                self.zoom.default = value
                    .parse()
                    .map_err(|_| format!("invalid zoom.default: {value}"))?
            }
            "permissions.default" => self.permissions.default = PermissionPolicy::parse(value)?,
            key if key.starts_with("permissions.") => {
                let rest = &key["permissions.".len()..];
                let policy = PermissionPolicy::parse(value)?;
                // A trailing capability segment sets that capability; otherwise
                // the key is a bare host and sets every capability.
                match rest.rsplit_once('.').and_then(|(host, last)| {
                    Capability::parse(last).map(|cap| (host, cap))
                }) {
                    Some((host, cap)) => self.permissions.set_capability(host, cap, policy),
                    None => self.permissions.set_all(rest, policy),
                }
            }
            _ => return Err(format!("unknown setting: {key}")),
        }
        Ok(())
    }
}

/// The complete application state.
#[derive(Debug, Default)]
pub struct State {
    pub mode: ModeState,
    pub tabs: Tabs,
    /// Filled once the binding trie is ported (input subsystem).
    pub input: InputState,
    pub command_line: CommandLine,
    pub status: StatusLine,
    pub hints: HintState,
    pub completion: CompletionState,
    /// Named shortcuts to URLs (name → url).
    pub quickmarks: BTreeMap<String, String>,
    /// Saved bookmarks.
    pub bookmarks: Vec<Bookmark>,
    pub config: Config,
    /// Normal-mode key bindings.
    pub bindings: BindingTrie,
    /// Purposes of in-flight JS evaluations, keyed by request id.
    pub pending_js: HashMap<RequestId, JsPurpose>,
    next_request_id: u64,
    /// Whether web-content dark mode is active.
    pub dark_mode: bool,
    /// Pending permission prompts; the front item is the one being shown.
    pub prompts: VecDeque<PermissionPrompt>,
    /// State of the permission management list view.
    pub perm_view: PermissionViewState,
    /// The last in-page search, for `n`/`N` repeat.
    pub last_search: Option<Search>,
    /// Active/recent downloads by id, for status reporting (id -> filename).
    pub downloads: BTreeMap<u64, String>,
    /// Cleared to false to request shutdown.
    pub running: bool,
}

/// A remembered in-page search.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Search {
    pub text: String,
}

impl State {
    /// Create the initial state from configuration.
    pub fn new(config: Config) -> Self {
        Self {
            config,
            bindings: default_bindings(),
            running: true,
            ..Self::default()
        }
    }

    /// Allocate a fresh request id for correlating an async result.
    pub fn alloc_request_id(&mut self) -> RequestId {
        let id = RequestId(self.next_request_id);
        self.next_request_id += 1;
        id
    }
}

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

    #[test]
    fn capabilities_are_independent() {
        let mut p = Permissions::default();
        p.set_capability("example.com", Capability::Notifications, PermissionPolicy::Allow);
        assert_eq!(
            p.policy_for("example.com", Capability::Notifications),
            PermissionPolicy::Allow
        );
        // Granting notifications must not grant the camera; it stays at default.
        assert_eq!(
            p.policy_for("example.com", Capability::Camera),
            PermissionPolicy::default()
        );
    }

    #[test]
    fn set_all_applies_to_every_capability() {
        let mut p = Permissions::default();
        p.set_all("example.com", PermissionPolicy::Allow);
        for cap in Capability::ALL {
            assert_eq!(p.policy_for("example.com", cap), PermissionPolicy::Allow);
        }
    }

    #[test]
    fn revoke_reverts_to_default() {
        let mut p = Permissions::default();
        p.set_capability("example.com", Capability::Camera, PermissionPolicy::Allow);
        let row = p.rows().into_iter().next().unwrap();
        p.revoke_row(&row);
        assert_eq!(
            p.policy_for("example.com", Capability::Camera),
            PermissionPolicy::default()
        );
        assert!(p.rows().is_empty());
    }

    #[test]
    fn old_bare_string_config_parses_as_all() {
        let toml = "default = \"ask\"\n[sites]\n\"example.com\" = \"allow\"\n";
        let p: Permissions = toml::from_str(toml).unwrap();
        assert_eq!(p.default, PermissionPolicy::Ask);
        assert_eq!(
            p.policy_for("example.com", Capability::Camera),
            PermissionPolicy::Allow
        );
    }

    #[test]
    fn per_capability_config_parses() {
        let toml =
            "[sites]\n\"example.com\" = { geolocation = \"allow\", camera = \"deny\" }\n";
        let p: Permissions = toml::from_str(toml).unwrap();
        assert_eq!(
            p.policy_for("example.com", Capability::Geolocation),
            PermissionPolicy::Allow
        );
        assert_eq!(
            p.policy_for("example.com", Capability::Camera),
            PermissionPolicy::Deny
        );
    }

    #[test]
    fn permissions_round_trip_through_toml() {
        let mut p = Permissions::default();
        p.set_all("a.test", PermissionPolicy::Allow);
        p.set_capability("b.test", Capability::Geolocation, PermissionPolicy::Deny);
        let text = toml::to_string_pretty(&p).unwrap();
        let back: Permissions = toml::from_str(&text).unwrap();
        assert_eq!(p, back);
    }
}