valence_generated/
block.rs

1#![allow(clippy::all)] // TODO: block build script creates many warnings.
2
3use std::fmt;
4use std::fmt::Display;
5use std::iter::FusedIterator;
6
7use valence_ident::{ident, Ident};
8
9use crate::item::ItemKind;
10
11include!(concat!(env!("OUT_DIR"), "/block.rs"));
12
13impl fmt::Debug for BlockState {
14    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15        fmt_block_state(*self, f)
16    }
17}
18
19impl Display for BlockState {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        fmt_block_state(*self, f)
22    }
23}
24
25fn fmt_block_state(bs: BlockState, f: &mut fmt::Formatter) -> fmt::Result {
26    let kind = bs.to_kind();
27
28    write!(f, "{}", kind.to_str())?;
29
30    let props = kind.props();
31
32    if !props.is_empty() {
33        let mut list = f.debug_list();
34        for &p in kind.props() {
35            struct KeyVal<'a>(&'a str, &'a str);
36
37            impl<'a> fmt::Debug for KeyVal<'a> {
38                fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39                    write!(f, "{}={}", self.0, self.1)
40                }
41            }
42
43            list.entry(&KeyVal(p.to_str(), bs.get(p).unwrap().to_str()));
44        }
45        list.finish()
46    } else {
47        Ok(())
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn get_set_consistency() {
57        for kind in BlockKind::ALL {
58            let block = kind.to_state();
59
60            for &prop in kind.props() {
61                let new_block = block.set(prop, block.get(prop).unwrap());
62                assert_eq!(new_block, block);
63            }
64        }
65    }
66
67    #[test]
68    fn blockstate_to_wall() {
69        assert_eq!(BlockState::STONE.wall_block_id(), None);
70        assert_eq!(
71            BlockState::OAK_SIGN.wall_block_id(),
72            Some(BlockState::OAK_WALL_SIGN)
73        );
74        assert_eq!(
75            BlockState::GREEN_BANNER.wall_block_id(),
76            Some(BlockState::GREEN_WALL_BANNER)
77        );
78        assert_ne!(
79            BlockState::GREEN_BANNER.wall_block_id(),
80            Some(BlockState::GREEN_BANNER)
81        );
82    }
83}