mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-07-04 06:39:07 +02:00
Add Serialize/Deserialize impls to various types
We want to be able to store this data to disk temporarily.
This commit is contained in:
parent
2d0f2fb865
commit
551056803d
3 changed files with 27 additions and 6 deletions
|
@ -6,6 +6,7 @@ use std::collections::HashMap;
|
|||
use enumflags2::{bitflags, BitFlags};
|
||||
|
||||
pub use legacy_block_types::LEGACY_BLOCK_TYPES;
|
||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
#[bitflags]
|
||||
#[repr(u8)]
|
||||
|
@ -19,11 +20,29 @@ pub enum BlockFlag {
|
|||
Water,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
fn serialize_block_flags<S>(flags: &BitFlags<BlockFlag>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
flags.bits().serialize(serializer)
|
||||
}
|
||||
fn deserialize_block_flags<'de, D>(deserializer: D) -> Result<BitFlags<BlockFlag>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let bits = u8::deserialize(deserializer)?;
|
||||
BitFlags::<BlockFlag>::from_bits(bits).map_err(de::Error::custom)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub struct BlockColor(pub u8, pub u8, pub u8);
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub struct BlockType {
|
||||
#[serde(
|
||||
serialize_with = "serialize_block_flags",
|
||||
deserialize_with = "deserialize_block_flags"
|
||||
)]
|
||||
pub flags: BitFlags<BlockFlag>,
|
||||
pub color: BlockColor,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue