Skip to main content

steel_utils/types/
codec_glue.rs

1use std::io::{self, Cursor, Write};
2
3use simdnbt::owned::{NbtCompound, NbtTag};
4
5use crate::{
6    codec::VarInt,
7    hash::{ComponentHasher, HashComponent},
8    serial::{ReadFrom, WriteTo},
9};
10
11/// A placeholder type for unimplemented component values.
12/// Unlike `()`, this is a distinct type that can have its own trait implementations.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
14pub struct Todo;
15
16impl WriteTo for Todo {
17    fn write(&self, _writer: &mut impl Write) -> io::Result<()> {
18        // Placeholder components write nothing
19        Ok(())
20    }
21}
22
23impl ReadFrom for Todo {
24    fn read(_data: &mut Cursor<&[u8]>) -> io::Result<Self> {
25        // Placeholder components read nothing
26        Ok(Todo)
27    }
28}
29
30impl HashComponent for Todo {
31    fn hash_component(&self, hasher: &mut ComponentHasher) {
32        // Hash as empty value
33        hasher.put_empty();
34    }
35}
36
37impl simdnbt::ToNbtTag for Todo {
38    fn to_nbt_tag(self) -> NbtTag {
39        // Placeholder components serialize as empty compound
40        NbtTag::Compound(NbtCompound::new())
41    }
42}
43
44impl simdnbt::FromNbtTag for Todo {
45    fn from_nbt_tag(_tag: simdnbt::borrow::NbtTag) -> Option<Self> {
46        // Placeholder components always deserialize successfully
47        Some(Todo)
48    }
49}
50
51/// A raw block state id. Using the registry this id can be derived into a block and it's current properties.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
53pub struct BlockStateId(pub u16);
54
55impl WriteTo for BlockStateId {
56    fn write(&self, writer: &mut impl Write) -> io::Result<()> {
57        VarInt(i32::from(self.0)).write(writer)
58    }
59}
60
61impl ReadFrom for BlockStateId {
62    fn read(data: &mut Cursor<&[u8]>) -> io::Result<Self> {
63        let id = VarInt::read(data)?.0;
64        #[expect(
65            clippy::cast_sign_loss,
66            reason = "VarInt is validated upstream; block state IDs are non-negative"
67        )]
68        Ok(Self(id as u16))
69    }
70}