use crate::strings::typst::ToTypst;
#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap;
use alloc::vec::Vec;
use core::fmt::{Formatter, Result};
use core::hash::Hash;
#[cfg(not(feature = "std"))]
use hashbrown::HashMap;
#[cfg(feature = "std")]
use std::collections::{BTreeMap, HashMap};
fn fmt_typst_entries<'a, K: ToTypst + 'a, V: ToTypst + 'a>(
entries: impl Iterator<Item = (&'a K, &'a V)>,
f: &mut Formatter,
) -> Result {
f.write_str("{")?;
for (i, (k, v)) in entries.enumerate() {
if i != 0 {
f.write_str(", ")?;
}
k.fmt_typst(f)?;
f.write_str(" |-> ")?;
v.fmt_typst(f)?;
}
f.write_str("}")
}
impl<K: ToTypst, V: ToTypst> ToTypst for BTreeMap<K, V> {
#[inline]
fn fmt_typst(&self, f: &mut Formatter) -> Result {
fmt_typst_entries(self.iter(), f)
}
}
impl<K: Eq + Hash + Ord + ToTypst, V: ToTypst> ToTypst for HashMap<K, V> {
fn fmt_typst(&self, f: &mut Formatter) -> Result {
let mut entries = self.iter().collect::<Vec<_>>();
entries.sort_unstable_by_key(|&(k, _)| k);
fmt_typst_entries(entries.into_iter(), f)
}
}