1use cbf::data::ids::{BrowsingContextId, TransientBrowsingContextId};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
7pub struct TabId(pub u64);
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
14pub struct PopupId(pub u64);
15
16impl std::fmt::Display for TabId {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 write!(f, "{}", self.get())
19 }
20}
21
22impl TabId {
23 pub const fn new(raw: u64) -> Self {
25 Self(raw)
26 }
27
28 pub const fn get(self) -> u64 {
30 self.0
31 }
32
33 pub const fn from_browsing_context_id(id: BrowsingContextId) -> Self {
35 Self(id.get())
36 }
37
38 pub const fn to_browsing_context_id(self) -> BrowsingContextId {
40 BrowsingContextId::new(self.get())
41 }
42}
43
44impl PopupId {
45 pub const fn new(raw: u64) -> Self {
47 Self(raw)
48 }
49
50 pub const fn get(self) -> u64 {
52 self.0
53 }
54
55 pub const fn from_transient_browsing_context_id(id: TransientBrowsingContextId) -> Self {
57 Self(id.get())
58 }
59
60 pub const fn to_transient_browsing_context_id(self) -> TransientBrowsingContextId {
62 TransientBrowsingContextId::new(self.get())
63 }
64}
65
66impl From<BrowsingContextId> for TabId {
67 fn from(value: BrowsingContextId) -> Self {
68 Self::from_browsing_context_id(value)
69 }
70}
71
72impl From<TabId> for BrowsingContextId {
73 fn from(value: TabId) -> Self {
74 value.to_browsing_context_id()
75 }
76}
77
78impl From<TransientBrowsingContextId> for PopupId {
79 fn from(value: TransientBrowsingContextId) -> Self {
80 Self::from_transient_browsing_context_id(value)
81 }
82}
83
84impl From<PopupId> for TransientBrowsingContextId {
85 fn from(value: PopupId) -> Self {
86 value.to_transient_browsing_context_id()
87 }
88}
89
90#[cfg(test)]
91mod tests {
92 use cbf::data::ids::{BrowsingContextId, TransientBrowsingContextId};
93
94 use super::{PopupId, TabId};
95
96 #[test]
97 fn tab_id_round_trip_preserves_raw_value() {
98 let original = BrowsingContextId::new(4242);
99
100 let tab_id = TabId::from(original);
101 let round_trip = tab_id.to_browsing_context_id();
102
103 assert_eq!(round_trip, BrowsingContextId::new(4242));
104 assert_eq!(tab_id, TabId::new(4242));
105 }
106
107 #[test]
108 fn popup_id_round_trip_preserves_raw_value() {
109 let original = TransientBrowsingContextId::new(77);
110
111 let popup_id = PopupId::from(original);
112 let round_trip = popup_id.to_transient_browsing_context_id();
113
114 assert_eq!(round_trip, TransientBrowsingContextId::new(77));
115 assert_eq!(popup_id, PopupId::new(77));
116 }
117}