From e03a805bdbe4b89c2b357f8587aca927b5bd6c69 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 13 Apr 2023 23:16:12 +0200 Subject: [PATCH] Something Working :D --- schemsearch-files/Cargo.toml | 3 +-- schemsearch-files/src/lib.rs | 35 +++++++++++++++++++++++++++++++---- schemsearch-lib/Cargo.toml | 3 --- schemsearch-lib/src/lib.rs | 4 +--- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/schemsearch-files/Cargo.toml b/schemsearch-files/Cargo.toml index b954fe4..76d6ddf 100644 --- a/schemsearch-files/Cargo.toml +++ b/schemsearch-files/Cargo.toml @@ -9,6 +9,5 @@ edition = "2021" flate2 = "1.0.25" hematite-nbt = "0.5.2" serde = "1.0.152" -serde-this-or-that = "0.4.2" -serde_repr = "0.1.12" +serde_json = "1.0.84" diff --git a/schemsearch-files/src/lib.rs b/schemsearch-files/src/lib.rs index 3c18428..cd1efe3 100644 --- a/schemsearch-files/src/lib.rs +++ b/schemsearch-files/src/lib.rs @@ -18,8 +18,15 @@ use std::io::Read; use std::path::PathBuf; use nbt::{Error, from_gzip_reader, Map, Value}; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use serde_this_or_that::as_i64; +use serde::{Deserialize, Deserializer, Serialize}; + +#[derive(Clone, Serialize, Deserialize, Debug)] +#[serde(rename_all = "PascalCase")] +struct SchematicRaw { + version: i32, + #[serde(flatten)] + data: Map, +} #[derive(Clone, Serialize, Deserialize, Debug)] #[serde(untagged, rename_all = "PascalCase")] @@ -94,6 +101,26 @@ impl SchematicVersioned { } } +impl From for SchematicVersioned { + fn from(value: SchematicRaw) -> Self { + match value.version { + 1 => { + let schematic: SpongeV1Schematic = serde_json::from_value(serde_json::to_value(value.data).unwrap()).unwrap(); + return SchematicVersioned::V1(schematic); + }, + 2 => { + let schematic: SpongeV2Schematic = serde_json::from_value(serde_json::to_value(value.data).unwrap()).unwrap(); + return SchematicVersioned::V2(schematic); + }, + 3 => { + let schematic: SpongeV3Schematic = serde_json::from_value(serde_json::to_value(value.data).unwrap()).unwrap(); + return SchematicVersioned::V3(schematic); + } + _ => panic!("Unknown Schematic Version: {}", value.version), + } + } +} + #[derive(Clone, Serialize, Deserialize, Debug)] #[serde(rename_all = "PascalCase")] pub struct SpongeV1Schematic { @@ -180,8 +207,8 @@ pub struct Entity { impl SchematicVersioned { pub fn load_data(data: R) -> Result where R: Read { - let schematic: SchematicVersioned = from_gzip_reader(data)?; - Ok(schematic) + let raw: SchematicRaw = from_gzip_reader(data)?; + Ok(SchematicVersioned::from(raw)) } pub fn load(path: &PathBuf) -> Result { diff --git a/schemsearch-lib/Cargo.toml b/schemsearch-lib/Cargo.toml index 1e3a0d0..d8e737f 100644 --- a/schemsearch-lib/Cargo.toml +++ b/schemsearch-lib/Cargo.toml @@ -10,6 +10,3 @@ license = "AGPL-3.0-or-later" hematite-nbt = "0.5.2" serde = "1.0.152" schemsearch-files = { path = "../schemsearch-files" } - -[dev-dependencies] -serde_json = "1.0.95" diff --git a/schemsearch-lib/src/lib.rs b/schemsearch-lib/src/lib.rs index dae1716..1c6d1b4 100644 --- a/schemsearch-lib/src/lib.rs +++ b/schemsearch-lib/src/lib.rs @@ -18,10 +18,8 @@ pub mod pattern_mapper; use serde::{Deserialize, Serialize}; -use nbt::Map; use pattern_mapper::match_palette; -use schemsearch_files::{SchematicVersioned, SpongeV2Schematic}; -use schemsearch_files::SchematicVersioned::V2; +use schemsearch_files::SchematicVersioned; use crate::pattern_mapper::match_palette_adapt; #[derive(Debug, Clone, Copy, Deserialize, Serialize)]