use core::fmt::{self, Write};
const MAX_PIECES: usize = 16;
#[derive(Clone, Copy)]
enum Piece<'a> {
Lit(&'static str),
Arg(&'a dyn fmt::Display),
}
#[derive(Clone, Copy)]
pub struct Formatted<'a> {
pieces: [Option<Piece<'a>>; MAX_PIECES],
n: u8,
}
impl<'a> Formatted<'a> {
#[doc(hidden)]
#[inline]
#[must_use]
pub const fn empty() -> Self {
Self {
pieces: [None; MAX_PIECES],
n: 0,
}
}
#[doc(hidden)]
#[inline]
#[must_use]
pub fn lit(self, s: &'static str) -> Self {
self.push(Piece::Lit(s))
}
#[doc(hidden)]
#[inline]
#[must_use]
pub fn arg(self, value: &'a dyn fmt::Display) -> Self {
self.push(Piece::Arg(value))
}
fn push(mut self, piece: Piece<'a>) -> Self {
let i = self.n as usize;
debug_assert!(
i < MAX_PIECES,
"cognomen extra exceeded {MAX_PIECES} pieces"
);
self.pieces[i] = Some(piece);
self.n += 1;
self
}
}
impl fmt::Display for Formatted<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for piece in self.pieces.iter().take(self.n as usize) {
match piece {
Some(Piece::Lit(s)) => f.write_str(s)?,
Some(Piece::Arg(v)) => write!(f, "{v}")?,
None => {}
}
}
Ok(())
}
}
impl fmt::Debug for Formatted<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl PartialEq<str> for Formatted<'_> {
fn eq(&self, other: &str) -> bool {
let mut cmp = Cmp {
rest: other,
ok: true,
};
let _ = write!(&mut cmp, "{self}");
cmp.ok && cmp.rest.is_empty()
}
}
impl PartialEq<&str> for Formatted<'_> {
#[inline]
fn eq(&self, other: &&str) -> bool {
self == *other
}
}
impl PartialEq<Formatted<'_>> for str {
#[inline]
fn eq(&self, other: &Formatted<'_>) -> bool {
other == self
}
}
impl PartialEq<Formatted<'_>> for &str {
#[inline]
fn eq(&self, other: &Formatted<'_>) -> bool {
other == *self
}
}
struct Cmp<'a> {
rest: &'a str,
ok: bool,
}
impl Write for Cmp<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
if !self.ok {
return Ok(());
}
match self.rest.strip_prefix(s) {
Some(tail) => self.rest = tail,
None => self.ok = false,
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::Formatted;
#[test]
fn eq_and_display() {
let f = Formatted::empty().lit("host open failed ").arg(&"busy");
assert!(f == "host open failed busy");
assert!(f == *"host open failed busy");
assert!("host open failed busy" == f);
assert!(f != "host open failed");
assert!(f != "host open failed busy!");
assert!(f != "Host open failed busy");
assert!(Formatted::empty() == "");
assert!(Formatted::empty().arg(&"x") == "x");
assert!(Formatted::empty().lit("static") == "static");
}
}