darling_core/error/
util.rs1use std::fmt;
2
3pub(crate) struct Quoted<T> {
5 open: &'static str,
6 body: T,
7 close: &'static str,
8}
9
10impl<T> Quoted<T> {
11 pub fn new(quote: &'static str, body: T) -> Self {
13 Self {
14 open: quote,
15 body,
16 close: quote,
17 }
18 }
19
20 pub fn backticks(body: T) -> Self {
22 Self::new("`", body)
23 }
24}
25
26impl<T: fmt::Display> fmt::Display for Quoted<T> {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 write!(f, "{}{}{}", self.open, self.body, self.close)
29 }
30}
31
32pub(crate) fn write_delimited<T: fmt::Display>(
35 f: &mut impl fmt::Write,
36 items: impl IntoIterator<Item = T>,
37 delimiter: &str,
38) -> fmt::Result {
39 let mut first = true;
40 for item in items {
41 if !first {
42 write!(f, "{}", delimiter)?;
43 }
44 first = false;
45 write!(f, "{}", item)?;
46 }
47
48 Ok(())
49}