use std::fmt::Display;
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub enum TitleWhitespace {
Spaces,
Underscores,
}
impl TitleWhitespace {
pub(crate) const fn char(self) -> char {
match self {
TitleWhitespace::Spaces => ' ',
TitleWhitespace::Underscores => '_',
}
}
pub(crate) const fn other_char(self) -> char {
match self {
TitleWhitespace::Spaces => '_',
TitleWhitespace::Underscores => ' ',
}
}
}
struct WhitespaceDisplayer<'a>(pub(crate) &'a str, pub(crate) TitleWhitespace);
impl<'a> Display for WhitespaceDisplayer<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self(s, whitespace) = self;
use std::fmt::Write as _;
let mut last_pos = None;
for (pos, _) in s.match_indices(whitespace.other_char()) {
f.write_str(&s[last_pos.replace(pos + 1).unwrap_or(0)..pos])?;
f.write_char(whitespace.char())?;
}
if last_pos
.map(|last| last < s.len())
.unwrap_or(true)
{
f.write_str(&s[last_pos.unwrap_or(0)..s.len()])?;
}
Ok(())
}
}
#[test]
fn whitespace_displayer_displays_spaces_as_underscores() {
for (input, expected) in [
(" ", "_"),
(" ", "___"),
("a b", "a_b"),
(" a", "_a"),
("a ", "a_"),
(" a b ", "_a_b_"),
("a b", "a__b"),
(" ab", "__ab"),
("ab ", "ab__"),
(" a b", "__a__b"),
(" a b ", "__a__b__"),
] {
for input in [input, &input.replace(' ', "_")] {
assert_eq!(
&WhitespaceDisplayer(input, TitleWhitespace::Underscores)
.to_string(),
expected,
"\n{input:?}"
);
}
}
}
pub(crate) struct TitleDisplay<'a> {
pub(crate) interwiki: Option<&'a str>,
pub(crate) namespace: Option<&'a str>,
pub(crate) dbkey: &'a str,
pub(crate) fragment: Option<&'a str>,
pub(crate) whitespace: TitleWhitespace,
}
impl<'a> Display for TitleDisplay<'a> {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> std::result::Result<(), std::fmt::Error> {
let whitespace_displayer = |s| WhitespaceDisplayer(s, self.whitespace);
if let Some(interwiki) = self.interwiki {
whitespace_displayer(interwiki).fmt(f)?;
f.write_str(":")?;
}
if let Some(namespace) = self.namespace {
whitespace_displayer(namespace).fmt(f)?;
f.write_str(":")?;
}
whitespace_displayer(self.dbkey).fmt(f)?;
if let Some(fragment) = self.fragment {
f.write_str("#")?;
fragment.fmt(f)?;
}
Ok(())
}
}