rgb_lightning/util/
string.rs1use core::fmt;
13
14#[derive(Debug, PartialEq)]
17pub struct PrintableString<'a>(pub &'a str);
18
19impl<'a> fmt::Display for PrintableString<'a> {
20 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
21 use core::fmt::Write;
22 for c in self.0.chars() {
23 let c = if c.is_control() { core::char::REPLACEMENT_CHARACTER } else { c };
24 f.write_char(c)?;
25 }
26
27 Ok(())
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use super::PrintableString;
34
35 #[test]
36 fn displays_printable_string() {
37 assert_eq!(
38 format!("{}", PrintableString("I \u{1F496} LDK!\t\u{26A1}")),
39 "I \u{1F496} LDK!\u{FFFD}\u{26A1}",
40 );
41 }
42}