Got code to compile and serialize correctly

This commit is contained in:
Nicholas Novak 2023-10-23 21:29:47 -07:00
parent 496a67cc7f
commit 63ff6e7c2c
4 changed files with 30 additions and 2 deletions

10
Cargo.lock generated
View File

@ -1111,6 +1111,15 @@ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]]
name = "serde_arrays"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38636132857f68ec3d5f3eb121166d2af33cb55174c4d5ff645db6165cbef0fd"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.189" version = "1.0.189"
@ -1191,6 +1200,7 @@ dependencies = [
"parquet", "parquet",
"rand", "rand",
"serde", "serde",
"serde_arrays",
"serde_json", "serde_json",
"tokio", "tokio",
] ]

View File

@ -12,5 +12,6 @@ clap = { version = "4.4.5", features = ["derive"] }
parquet = "47.0.0" parquet = "47.0.0"
rand = "0.8.5" rand = "0.8.5"
serde = { version = "1.0.189", features = ["derive"] } serde = { version = "1.0.189", features = ["derive"] }
serde_arrays = "0.1.0"
serde_json = "1.0.107" serde_json = "1.0.107"
tokio = { version = "1.32.0", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.32.0", features = ["macros", "rt-multi-thread"] }

View File

@ -61,6 +61,7 @@ impl ChunkStorageCache {
&self.cached_chunk_files[last_used_index] &self.cached_chunk_files[last_used_index]
} }
/// `fetch_chunk_by_pos` takes in the position of a chunk, and returns the /// `fetch_chunk_by_pos` takes in the position of a chunk, and returns the
/// data of the chunk from disk /// data of the chunk from disk
/// ///

View File

@ -1,5 +1,6 @@
use core::fmt; use core::fmt;
use serde::ser; use serde::ser;
use serde::ser::{SerializeSeq, SerializeStruct, Serializer};
use serde::Serialize; use serde::Serialize;
use std::{ use std::{
cmp::{max, min}, cmp::{max, min},
@ -31,12 +32,26 @@ impl ChunkPos {
} }
} }
#[derive(Debug, Serialize)] #[derive(Debug)]
pub struct ChunkData { pub struct ChunkData {
pub pos: ChunkPos, pub pos: ChunkPos,
pub sections: [ChunkSection; SECTIONS_PER_CHUNK], pub sections: [ChunkSection; SECTIONS_PER_CHUNK],
} }
impl Serialize for ChunkData {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.sections.len()))?;
for section in self.sections {
seq.serialize_element(&section)?;
}
seq.end()
}
}
impl ChunkData { impl ChunkData {
pub fn new(pos: &ChunkPos) -> Self { pub fn new(pos: &ChunkPos) -> Self {
ChunkData { ChunkData {
@ -59,7 +74,7 @@ impl ChunkData {
} }
// https://wiki.vg/Chunk_Format // https://wiki.vg/Chunk_Format
#[derive(Clone, Copy)] #[derive(Clone, Copy, Serialize)]
pub struct ChunkSection { pub struct ChunkSection {
/// The number of non-empty blocks in the section. If completely full, the /// The number of non-empty blocks in the section. If completely full, the
/// section contains a 16 x 16 x 16 cube of blocks = 4096 blocks /// section contains a 16 x 16 x 16 cube of blocks = 4096 blocks
@ -68,6 +83,7 @@ pub struct ChunkSection {
/// The data for all the blocks in the chunk /// The data for all the blocks in the chunk
/// The representation for this may be different based on the number of /// The representation for this may be different based on the number of
/// non-empty blocks /// non-empty blocks
#[serde(with = "serde_arrays")]
block_states: [BlockID; 16 * 16 * 16], block_states: [BlockID; 16 * 16 * 16],
} }