use std::fmt::{self, Debug, Formatter};
use std::num::NonZeroUsize;
use ecow::EcoString;
use crate::engine::Engine;
use crate::foundations::{Repr, func, scope, ty};
use crate::layout::Position;
use crate::model::Numbering;
#[ty(scope)]
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct Location(u128);
impl Location {
pub fn new(hash: u128) -> Self {
Self(hash)
}
pub fn hash(self) -> u128 {
self.0
}
pub fn variant(self, n: usize) -> Self {
Self(typst_utils::hash128(&(self.0, n)))
}
}
#[scope]
impl Location {
#[func]
pub fn page(self, engine: &mut Engine) -> NonZeroUsize {
engine.introspector.page(self)
}
#[func]
pub fn position(self, engine: &mut Engine) -> Position {
engine.introspector.position(self)
}
#[func]
pub fn page_numbering(self, engine: &mut Engine) -> Option<Numbering> {
engine.introspector.page_numbering(self).cloned()
}
}
impl Debug for Location {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
if f.alternate() {
write!(f, "Location({})", self.0)
} else {
let truncated = self.0 as u16;
write!(f, "Location({truncated})")
}
}
}
impl Repr for Location {
fn repr(&self) -> EcoString {
"..".into()
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct LocationKey(u128);
impl LocationKey {
pub fn new(location: Location) -> Self {
Self(location.0)
}
}
impl From<Location> for LocationKey {
fn from(location: Location) -> Self {
Self::new(location)
}
}
pub trait Locatable {}
pub trait Unqueriable: Locatable {}
pub trait Tagged {}