use std::fmt;
pub(crate) struct Quoted<T> {
open: &'static str,
body: T,
close: &'static str,
}
impl<T> Quoted<T> {
pub fn new(quote: &'static str, body: T) -> Self {
Self {
open: quote,
body,
close: quote,
}
}
pub fn backticks(body: T) -> Self {
Self::new("`", body)
}
}
impl<T: fmt::Display> fmt::Display for Quoted<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}{}", self.open, self.body, self.close)
}
}
pub(crate) fn write_delimited<T: fmt::Display>(
f: &mut impl fmt::Write,
items: impl IntoIterator<Item = T>,
delimiter: &str,
) -> fmt::Result {
let mut first = true;
for item in items {
if !first {
write!(f, "{}", delimiter)?;
}
first = false;
write!(f, "{}", item)?;
}
Ok(())
}