Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tiled: add flexible property value type #808

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tiled/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod error;
mod tiled;

pub use error::Error;
pub use tiled::layer::Property;
pub use tiled::{Property, PropertyVal};

#[derive(Debug, Clone)]
pub struct Object {
Expand Down
56 changes: 54 additions & 2 deletions tiled/src/tiled.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
use layer::Layer;
use nanoserde::DeJson;

pub mod layer;

use layer::Layer;
#[derive(Debug, Clone)]
pub enum PropertyVal {
String(String),
UInt(u64),
Integer(i64),
Float(f64),
Boolean(bool),
}

impl Default for PropertyVal {
fn default() -> Self {
PropertyVal::Boolean(false)
}
}

impl std::fmt::Display for PropertyVal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PropertyVal::String(x) => write!(f, "{}", x),
PropertyVal::UInt(x) => write!(f, "{}", x),
PropertyVal::Integer(x) => write!(f, "{}", x),
PropertyVal::Float(x) => write!(f, "{}", x),
PropertyVal::Boolean(x) => write!(f, "{}", x),
}
}
}

impl DeJson for PropertyVal {
fn de_json(
s: &mut nanoserde::DeJsonState,
i: &mut std::str::Chars,
) -> Result<Self, nanoserde::DeJsonErr> {
use nanoserde::DeJsonTok;

let v = match s.tok {
DeJsonTok::Bool(b) => PropertyVal::Boolean(b),
DeJsonTok::U64(x) => PropertyVal::UInt(x),
DeJsonTok::I64(x) => PropertyVal::Integer(x),
DeJsonTok::F64(x) => PropertyVal::Float(x),
DeJsonTok::Str => PropertyVal::String(core::mem::replace(&mut s.strbuf, String::new())),
_ => {
return Err(s.err_token(
"Incorrect property value. Must be either string, number or boolean",
))
}
};

s.next_tok(i)?;

Ok(v)
}
}

/// https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tmx-grid
#[derive(Clone, Debug, Default, DeJson)]
Expand All @@ -14,7 +66,7 @@ pub struct Grid {
#[derive(Clone, Debug, Default, DeJson)]
pub struct Property {
pub name: String,
pub value: String,
pub value: PropertyVal,
#[nserde(rename = "type")]
pub ty: String,
}
Expand Down
10 changes: 1 addition & 9 deletions tiled/src/tiled/layer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::Property;
use nanoserde::DeJson;

/// https://doc.mapeditor.org/en/stable/reference/json-map-format/#json-chunk
Expand Down Expand Up @@ -51,15 +52,6 @@ pub struct Layer {
pub image: Option<String>,
}

#[derive(Clone, Debug, Default, DeJson)]
#[nserde(default)]
pub struct Property {
pub name: String,
#[nserde(rename = "type")]
pub ty: String,
pub value: String,
}

#[derive(Clone, Debug, Default, DeJson)]
#[nserde(default)]
pub struct Object {
Expand Down
Loading