#![doc(html_root_url = "https://docs.rs/phf_codegen/0.13.1")]
#![allow(clippy::new_without_default)]
use phf_shared::{FmtConst, PhfHash};
use std::borrow::Cow;
use std::collections::HashSet;
use std::fmt;
use std::hash::Hash;
use phf_generator::HashState;
struct Delegate<T>(T);
impl<T: FmtConst> fmt::Display for Delegate<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt_const(f)
}
}
pub struct Map<'a, K> {
keys: Vec<K>,
values: Vec<Cow<'a, str>>,
path: Cow<'a, str>,
}
impl<'a, K: Hash + PhfHash + Eq + FmtConst> Map<'a, K> {
pub fn new() -> Self {
fn noop_fix_for_27438() {}
noop_fix_for_27438();
Map {
keys: vec![],
values: vec![],
path: Cow::Borrowed("::phf"),
}
}
pub fn phf_path(&mut self, path: impl Into<Cow<'a, str>>) -> &mut Self {
self.path = path.into();
self
}
pub fn entry(&mut self, key: K, value: impl Into<Cow<'a, str>>) -> &mut Self {
self.keys.push(key);
self.values.push(value.into());
self
}
pub fn build(&self) -> DisplayMap<'_, K> {
let mut set = HashSet::new();
for key in &self.keys {
if !set.insert(key) {
panic!("duplicate key `{}`", Delegate(key));
}
}
let state = phf_generator::generate_hash(&self.keys);
DisplayMap {
state,
path: &self.path,
keys: &self.keys,
values: &self.values,
}
}
}
pub struct DisplayMap<'a, K> {
path: &'a str,
state: HashState,
keys: &'a [K],
values: &'a [Cow<'a, str>],
}
impl<'a, K: FmtConst + 'a> fmt::Display for DisplayMap<'a, K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}::Map {{
key: {:?},
disps: &[",
self.path, self.state.key
)?;
for &(d1, d2) in &self.state.disps {
write!(
f,
"
({}, {}),",
d1, d2
)?;
}
write!(
f,
"
],
entries: &[",
)?;
for &idx in &self.state.map {
write!(
f,
"
({}, {}),",
Delegate(&self.keys[idx]),
&self.values[idx]
)?;
}
write!(
f,
"
],
}}"
)
}
}
impl<'a, K, V> FromIterator<(K, V)> for Map<'a, K>
where
K: Hash + PhfHash + Eq + FmtConst,
V: Into<Cow<'a, str>>,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let mut map = Map::new();
for (key, value) in iter {
map.entry(key, value);
}
map
}
}
pub struct Set<'a, T> {
map: Map<'a, T>,
}
impl<'a, T: Hash + PhfHash + Eq + FmtConst> Set<'a, T> {
pub fn new() -> Self {
Set { map: Map::new() }
}
pub fn phf_path(&mut self, path: impl Into<Cow<'a, str>>) -> &mut Self {
self.map.phf_path(path);
self
}
pub fn entry(&mut self, entry: T) -> &mut Self {
self.map.entry(entry, "()");
self
}
pub fn build(&self) -> DisplaySet<'_, T> {
DisplaySet {
inner: self.map.build(),
}
}
}
pub struct DisplaySet<'a, T> {
inner: DisplayMap<'a, T>,
}
impl<'a, T: FmtConst + 'a> fmt::Display for DisplaySet<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}::Set {{ map: {} }}", self.inner.path, self.inner)
}
}
pub struct OrderedMap<'a, K> {
keys: Vec<K>,
values: Vec<Cow<'a, str>>,
path: Cow<'a, str>,
}
impl<'a, K: Hash + PhfHash + Eq + FmtConst> OrderedMap<'a, K> {
pub fn new() -> Self {
OrderedMap {
keys: vec![],
values: vec![],
path: Cow::Borrowed("::phf"),
}
}
pub fn phf_path(&mut self, path: impl Into<Cow<'a, str>>) -> &mut Self {
self.path = path.into();
self
}
pub fn entry(&mut self, key: K, value: impl Into<Cow<'a, str>>) -> &mut Self {
self.keys.push(key);
self.values.push(value.into());
self
}
pub fn build(&self) -> DisplayOrderedMap<'_, K> {
let mut set = HashSet::new();
for key in &self.keys {
if !set.insert(key) {
panic!("duplicate key `{}`", Delegate(key));
}
}
let state = phf_generator::generate_hash(&self.keys);
DisplayOrderedMap {
state,
path: &self.path,
keys: &self.keys,
values: &self.values,
}
}
}
pub struct DisplayOrderedMap<'a, K> {
path: &'a str,
state: HashState,
keys: &'a [K],
values: &'a [Cow<'a, str>],
}
impl<'a, K: FmtConst + 'a> fmt::Display for DisplayOrderedMap<'a, K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}::OrderedMap {{
key: {:?},
disps: &[",
self.path, self.state.key
)?;
for &(d1, d2) in &self.state.disps {
write!(
f,
"
({}, {}),",
d1, d2
)?;
}
write!(
f,
"
],
idxs: &[",
)?;
for &idx in &self.state.map {
write!(
f,
"
{},",
idx
)?;
}
write!(
f,
"
],
entries: &[",
)?;
for (key, value) in self.keys.iter().zip(self.values.iter()) {
write!(
f,
"
({}, {}),",
Delegate(key),
value
)?;
}
write!(
f,
"
],
}}"
)
}
}
pub struct OrderedSet<'a, T> {
map: OrderedMap<'a, T>,
}
impl<'a, T: Hash + PhfHash + Eq + FmtConst> OrderedSet<'a, T> {
pub fn new() -> Self {
OrderedSet {
map: OrderedMap::new(),
}
}
pub fn phf_path(&mut self, path: impl Into<Cow<'a, str>>) -> &mut Self {
self.map.phf_path(path);
self
}
pub fn entry(&mut self, entry: T) -> &mut Self {
self.map.entry(entry, "()");
self
}
pub fn build(&self) -> DisplayOrderedSet<'_, T> {
DisplayOrderedSet {
inner: self.map.build(),
}
}
}
pub struct DisplayOrderedSet<'a, T> {
inner: DisplayOrderedMap<'a, T>,
}
impl<'a, T: FmtConst + 'a> fmt::Display for DisplayOrderedSet<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}::OrderedSet {{ map: {} }}",
self.inner.path, self.inner
)
}
}