+/// A user-defined name for a node, which may be used when displaying the node in a graph.
+///
+/// Since node aliases are provided by third parties, they are a potential avenue for injection
+/// attacks. Care must be taken when processing.
+#[derive(Clone, Debug, PartialEq)]
+pub struct NodeAlias(pub [u8; 32]);
+
+impl fmt::Display for NodeAlias {
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ let control_symbol = core::char::REPLACEMENT_CHARACTER;
+ let first_null = self.0.iter().position(|b| *b == 0).unwrap_or(self.0.len());
+ let bytes = self.0.split_at(first_null).0;
+ match core::str::from_utf8(bytes) {
+ Ok(alias) => {
+ for c in alias.chars() {
+ let mut bytes = [0u8; 4];
+ let c = if !c.is_control() { c } else { control_symbol };
+ f.write_str(c.encode_utf8(&mut bytes))?;
+ }
+ },
+ Err(_) => {
+ for c in bytes.iter().map(|b| *b as char) {
+ // Display printable ASCII characters
+ let mut bytes = [0u8; 4];
+ let c = if c >= '\x20' && c <= '\x7e' { c } else { control_symbol };
+ f.write_str(c.encode_utf8(&mut bytes))?;
+ }
+ },
+ };
+ Ok(())
+ }
+}
+
+impl Writeable for NodeAlias {
+ fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+ self.0.write(w)
+ }
+}
+
+impl Readable for NodeAlias {
+ fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
+ Ok(NodeAlias(Readable::read(r)?))
+ }
+}
+