Merge pull request #2954 from TheBlueMatt/2024-03-test-ci-beta-fail
[rust-lightning] / lightning / src / util / string.rs
index 42eb96f5bf6076b471f0d72fc0ef66e4f4d0bafa..ab12486a0d8ebfbac985dd3289b8e6dbeb23f3f2 100644 (file)
 //! Utilities for strings.
 
 use core::fmt;
+use crate::io::{self, Read};
+use crate::ln::msgs;
+use crate::util::ser::{Writeable, Writer, Readable};
+
+#[allow(unused_imports)]
+use crate::prelude::*;
+
+/// Struct to `Display` fields in a safe way using `PrintableString`
+#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
+pub struct UntrustedString(pub String);
+
+impl Writeable for UntrustedString {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+               self.0.write(w)
+       }
+}
+
+impl Readable for UntrustedString {
+       fn read<R: Read>(r: &mut R) -> Result<Self, msgs::DecodeError> {
+               let s: String = Readable::read(r)?;
+               Ok(Self(s))
+       }
+}
+
+impl fmt::Display for UntrustedString {
+       fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+               PrintableString(&self.0).fmt(f)
+       }
+}
 
 /// A string that displays only printable characters, replacing control characters with
 /// [`core::char::REPLACEMENT_CHARACTER`].