Skip to content

Commit cb2bda2

Browse files
bors[bot]Waridley
andauthored
Merge #743
743: serde support for core types & VariantDispatch r=Bromeon a=Waridley I got curious about how hard it would be to add serde support, and ended up basically fully implementing it so I figured I should start a PR 😂 There are no tests yet, and honestly I haven't really even tested it in my own project yet. I'll be doing so over the next few days probably, but for now I'll open this as a draft to allow for comments, feedback, and opinions. The implementations for `Object` types is a bit hacky, but I really like the idea of being able to replace Godot's resource files with something more Rust-y. Since I had to wrap the references in newtypes, I suppose they could be part of a separate crate anyway, unless anyone has a better idea of how to get around the orphan rules. Even if we can implement serde's traits directly on `gdnative_core` types, there should probably be some discussion about whether it's actually desirable to skip properties that are set to their defaults (this would end up serializing a **LOT** of properties otherwise). Would close #9 Co-authored-by: Waridley <Waridley64@gmail.com>
2 parents 490ae0f + 58fa3b3 commit cb2bda2

File tree

22 files changed

+822
-2
lines changed

22 files changed

+822
-2
lines changed

gdnative-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ glam = "0.18.0"
2424
indexmap = "1.7.0"
2525
ahash = "0.7.4"
2626
once_cell = "1.8.0"
27+
serde = { version = "1", features = ["derive"], optional = true }
2728

2829
gdnative-impl-proc-macros = { path = "../impl/proc_macros", version = "=0.9.3" }
2930

gdnative-core/src/core_types/color.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::core_types::GodotString;
66
/// RGBA color with 32 bits floating point components.
77
#[repr(C)]
88
#[derive(Copy, Clone, Debug, PartialEq)]
9+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
910
pub struct Color {
1011
pub r: f32,
1112
pub g: f32,

gdnative-core/src/core_types/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::sys;
22

33
/// Error codes used in various Godot APIs.
44
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
56
#[repr(u32)]
67
pub enum GodotError {
78
Failed = sys::godot_error_GODOT_FAILED as u32,

gdnative-core/src/core_types/geom/aabb.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::core_types::Vector3;
33
/// Axis-aligned bounding box.
44
#[repr(C)]
55
#[derive(Copy, Clone, Debug, PartialEq)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67
pub struct Aabb {
78
pub position: Vector3,
89
pub size: Vector3,

gdnative-core/src/core_types/geom/basis.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use glam::Mat3;
55
/// A 3x3 matrix.
66
#[repr(C)]
77
#[derive(Copy, Clone, Debug, PartialEq)]
8+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
89
pub struct Basis {
910
pub elements: [Vector3; 3],
1011
}

gdnative-core/src/core_types/geom/plane.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::core_types::{IsEqualApprox, Vector3};
33
/// Plane in hessian form.
44
#[repr(C)]
55
#[derive(Copy, Clone, Debug, PartialEq)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67
pub struct Plane {
78
pub normal: Vector3,
89
pub d: f32,

gdnative-core/src/core_types/geom/transform.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::core_types::{Basis, Vector3};
33
/// 3D Transformation (3x4 matrix) Using basis + origin representation.
44
#[repr(C)]
55
#[derive(Copy, Clone, Debug, PartialEq)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67
pub struct Transform {
78
/// The basis is a matrix containing 3 Vector3 as its columns: X axis, Y axis, and Z axis.
89
/// These vectors can be interpreted as the basis vectors of local coordinate system

gdnative-core/src/core_types/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub use rid::*;
4343
pub use string::*;
4444
pub use string_array::*;
4545
pub use transform2d::*;
46-
pub use typed_array::TypedArray;
46+
pub use typed_array::{Element, TypedArray};
4747
pub use variant::*;
4848
pub use variant_array::*;
4949
pub use vector2::*;

gdnative-core/src/core_types/node_path.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,60 @@ impl fmt::Debug for NodePath {
176176
write!(f, "NodePath({})", self.to_string())
177177
}
178178
}
179+
180+
#[cfg(feature = "serde")]
181+
mod serialize {
182+
use super::*;
183+
use serde::{
184+
de::{Error, Visitor},
185+
Deserialize, Deserializer, Serialize, Serializer,
186+
};
187+
use std::fmt::Formatter;
188+
189+
impl Serialize for NodePath {
190+
#[inline]
191+
fn serialize<S>(&self, ser: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
192+
where
193+
S: Serializer,
194+
{
195+
ser.serialize_newtype_struct("NodePath", &*self.to_string())
196+
}
197+
}
198+
199+
impl<'de> Deserialize<'de> for NodePath {
200+
#[inline]
201+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
202+
where
203+
D: Deserializer<'de>,
204+
{
205+
struct NodePathVisitor;
206+
207+
impl<'de> Visitor<'de> for NodePathVisitor {
208+
type Value = NodePath;
209+
210+
fn expecting(&self, formatter: &mut Formatter) -> fmt::Result {
211+
formatter.write_str("a NodePath")
212+
}
213+
214+
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
215+
where
216+
E: Error,
217+
{
218+
Ok(NodePath::from_str(s))
219+
}
220+
221+
fn visit_newtype_struct<D>(
222+
self,
223+
deserializer: D,
224+
) -> Result<Self::Value, <D as Deserializer<'de>>::Error>
225+
where
226+
D: Deserializer<'de>,
227+
{
228+
deserializer.deserialize_str(self)
229+
}
230+
}
231+
232+
deserializer.deserialize_newtype_struct("NodePath", NodePathVisitor)
233+
}
234+
}
235+
}

gdnative-core/src/core_types/quat.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use glam::EulerRot;
33
use std::ops::{Mul, Neg};
44

55
#[derive(Copy, Clone, Debug, PartialEq)]
6+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67
#[repr(C)]
78
pub struct Quat {
89
pub x: f32,

0 commit comments

Comments
 (0)