impl Display for BlockState

This commit is contained in:
Ryan 2022-07-06 18:46:03 -07:00
parent 24cf864ed1
commit 54e0d5cb90

View file

@ -1,6 +1,6 @@
#![allow(clippy::all, missing_docs)] #![allow(clippy::all, missing_docs)]
use std::fmt; use std::fmt::{self, Display};
use std::io::{Read, Write}; use std::io::{Read, Write};
use anyhow::Context; use anyhow::Context;
@ -11,30 +11,40 @@ use crate::protocol::{Decode, Encode, VarInt};
include!(concat!(env!("OUT_DIR"), "/block.rs")); include!(concat!(env!("OUT_DIR"), "/block.rs"));
impl fmt::Debug for BlockState { impl fmt::Debug for BlockState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_block_state(*self, f)
}
}
impl Display for BlockState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = self.to_kind(); fmt_block_state(*self, f)
}
}
write!(f, "{}", kind.to_str())?; fn fmt_block_state(bs: BlockState, f: &mut fmt::Formatter) -> fmt::Result {
let kind = bs.to_kind();
let props = kind.props(); write!(f, "{}", kind.to_str())?;
if !props.is_empty() { let props = kind.props();
let mut list = f.debug_list();
for &p in kind.props() {
struct KeyVal<'a>(&'a str, &'a str);
impl<'a> fmt::Debug for KeyVal<'a> { if !props.is_empty() {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut list = f.debug_list();
write!(f, "{}={}", self.0, self.1) for &p in kind.props() {
} struct KeyVal<'a>(&'a str, &'a str);
impl<'a> fmt::Debug for KeyVal<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}={}", self.0, self.1)
} }
list.entry(&KeyVal(p.to_str(), self.get(p).unwrap().to_str()));
} }
list.finish()
} else { list.entry(&KeyVal(p.to_str(), bs.get(p).unwrap().to_str()));
Ok(())
} }
list.finish()
} else {
Ok(())
} }
} }