use cbf::data::ids::{BrowsingContextId, TransientBrowsingContextId};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TabId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PopupId(pub u64);
impl std::fmt::Display for TabId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.get())
}
}
impl TabId {
pub const fn new(raw: u64) -> Self {
Self(raw)
}
pub const fn get(self) -> u64 {
self.0
}
pub const fn from_browsing_context_id(id: BrowsingContextId) -> Self {
Self(id.get())
}
pub const fn to_browsing_context_id(self) -> BrowsingContextId {
BrowsingContextId::new(self.get())
}
}
impl PopupId {
pub const fn new(raw: u64) -> Self {
Self(raw)
}
pub const fn get(self) -> u64 {
self.0
}
pub const fn from_transient_browsing_context_id(id: TransientBrowsingContextId) -> Self {
Self(id.get())
}
pub const fn to_transient_browsing_context_id(self) -> TransientBrowsingContextId {
TransientBrowsingContextId::new(self.get())
}
}
impl From<BrowsingContextId> for TabId {
fn from(value: BrowsingContextId) -> Self {
Self::from_browsing_context_id(value)
}
}
impl From<TabId> for BrowsingContextId {
fn from(value: TabId) -> Self {
value.to_browsing_context_id()
}
}
impl From<TransientBrowsingContextId> for PopupId {
fn from(value: TransientBrowsingContextId) -> Self {
Self::from_transient_browsing_context_id(value)
}
}
impl From<PopupId> for TransientBrowsingContextId {
fn from(value: PopupId) -> Self {
value.to_transient_browsing_context_id()
}
}
#[cfg(test)]
mod tests {
use cbf::data::ids::{BrowsingContextId, TransientBrowsingContextId};
use super::{PopupId, TabId};
#[test]
fn tab_id_round_trip_preserves_raw_value() {
let original = BrowsingContextId::new(4242);
let tab_id = TabId::from(original);
let round_trip = tab_id.to_browsing_context_id();
assert_eq!(round_trip, BrowsingContextId::new(4242));
assert_eq!(tab_id, TabId::new(4242));
}
#[test]
fn popup_id_round_trip_preserves_raw_value() {
let original = TransientBrowsingContextId::new(77);
let popup_id = PopupId::from(original);
let round_trip = popup_id.to_transient_browsing_context_id();
assert_eq!(round_trip, TransientBrowsingContextId::new(77));
assert_eq!(popup_id, PopupId::new(77));
}
}