Skip to content

Commit

Permalink
#164 updated glam & bevy_ecs types to match their serde implementat…
Browse files Browse the repository at this point in the history
…ion (#165)

* #164 updated glam & bevy_ecs types to match their `serde` implementation

* #164 fixed typo

* #164 improved error message and removed WorldId (no serde implemented)

* #164 fixed test

* #164 updated array to tuple

* #164 fixed to long tuple for Mat4 and added basic tests

* #164 bumped bevy version

* avoid string allocate when error not hit

* small cleanup

---------

Co-authored-by: Oscar Beaumont <[email protected]>
  • Loading branch information
bennobuilder and oscartbeaumont authored Nov 22, 2023
1 parent 7039fba commit 3a3f4bf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ glam = { version = "0.24", optional = true, default-features = false, features =
tokio = { version = "1.30", optional = true, default-features = false, features = ["sync"] }
url = { version = "2.4.0", optional = true, default-features = false }
either = { version = "1.9.0", optional = true, default-features = false }
bevy_ecs = { version = "0.11.3", optional = true, default-features = false }
bevy_ecs = { version = "0.12.0", optional = true, default-features = false }
thiserror = "1.0.44"
paste = "1.0.14"
ctor = { version = "0.2.4", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ pub(crate) fn is_valid_ty(dt: &DataType, type_map: &TypeMap) -> Result<(), Serde
let ty = type_map
.get(&ty.sid)
.as_ref()
.expect("Reference type not found")
.unwrap_or_else(|| panic!("Reference type not found for: {}", ty.sid.type_name))

Check warning on line 80 in src/serde.rs

View workflow job for this annotation

GitHub Actions / clippy

`panic` should not be present in production code

warning: `panic` should not be present in production code --> src/serde.rs:80:36 | 80 | .unwrap_or_else(|| panic!("Reference type not found for: {}", ty.sid.type_name)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#panic note: the lint level is defined here --> src/lib.rs:3:43 | 3 | #![warn(clippy::all, clippy::unwrap_used, clippy::panic)] // TODO: missing_docs | ^^^^^^^^^^^^^
.as_ref()
.expect("Type was never populated"); // TODO: Error properly
.unwrap_or_else(|| panic!("Type '{}' was never populated.", ty.sid.type_name)); // TODO: Error properly

Check warning on line 82 in src/serde.rs

View workflow job for this annotation

GitHub Actions / clippy

`panic` should not be present in production code

warning: `panic` should not be present in production code --> src/serde.rs:82:36 | 82 | .unwrap_or_else(|| panic!("Type '{}' was never populated.", ty.sid.type_name)); // TODO: Error properly | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#panic

is_valid_ty(&ty.inner, type_map)?;
}
Expand Down
97 changes: 17 additions & 80 deletions src/type/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,132 +587,77 @@ const _: () = {
#[derive(Type)]
#[specta(remote = glam::DVec2, crate = crate, export = false)]
#[allow(dead_code)]
struct DVec2 {
x: f64,
y: f64,
}
struct DVec2([f64; 2]);

#[derive(Type)]
#[specta(remote = glam::IVec2, crate = crate, export = false)]
#[allow(dead_code)]
struct IVec2 {
x: i32,
y: i32,
}
struct IVec2([i32; 2]);

#[derive(Type)]
#[specta(remote = glam::DMat2, crate = crate, export = false)]
#[allow(dead_code)]
struct DMat2 {
pub x_axis: glam::DVec2,
pub y_axis: glam::DVec2,
}
struct DMat2([f64; 4]);

#[derive(Type)]
#[specta(remote = glam::DAffine2, crate = crate, export = false)]
#[allow(dead_code)]
struct DAffine2 {
matrix2: glam::DMat2,
translation: glam::DVec2,
}
struct DAffine2([f64; 6]);

#[derive(Type)]
#[specta(remote = glam::Vec2, crate = crate, export = false)]
#[allow(dead_code)]
struct Vec2 {
pub x: f32,
pub y: f32,
}
struct Vec2([f32; 2]);

#[derive(Type)]
#[specta(remote = glam::Vec3, crate = crate, export = false)]
#[allow(dead_code)]
struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
struct Vec3([f32; 3]);

#[derive(Type)]
#[specta(remote = glam::Vec3A, crate = crate, export = false)]
#[allow(dead_code)]
struct Vec3A {
pub x: f32,
pub y: f32,
pub z: f32,
}
struct Vec3A([f32; 3]);

#[derive(Type)]
#[specta(remote = glam::Vec4, crate = crate, export = false)]
#[allow(dead_code)]
struct Vec4 {
x: f32,
y: f32,
z: f32,
w: f32,
}
struct Vec4([f32; 4]);

#[derive(Type)]
#[specta(remote = glam::Mat2, crate = crate, export = false)]
#[allow(dead_code)]
struct Mat2 {
pub x_axis: glam::Vec2,
pub y_axis: glam::Vec2,
}
struct Mat2([f32; 4]);

#[derive(Type)]
#[specta(remote = glam::Mat3, crate = crate, export = false)]
#[allow(dead_code)]
struct Mat3 {
pub x_axis: glam::Vec3,
pub y_axis: glam::Vec3,
pub z_axis: glam::Vec3,
}
struct Mat3([f32; 9]);

#[derive(Type)]
#[specta(remote = glam::Mat3A, crate = crate, export = false)]
#[allow(dead_code)]
struct Mat3A {
pub x_axis: glam::Vec3A,
pub y_axis: glam::Vec3A,
pub z_axis: glam::Vec3A,
}
struct Mat3A([f32; 9]);

#[derive(Type)]
#[specta(remote = glam::Mat4, crate = crate, export = false)]
#[allow(dead_code)]
struct Mat4 {
pub x_axis: glam::Vec4,
pub y_axis: glam::Vec4,
pub z_axis: glam::Vec4,
pub w_axis: glam::Vec4,
}
struct Mat4([f32; 16]);

#[derive(Type)]
#[specta(remote = glam::Quat, crate = crate, export = false)]
#[allow(dead_code)]
struct Quat {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}
struct Quat([f32; 4]);

#[derive(Type)]
#[specta(remote = glam::Affine2, crate = crate, export = false)]
#[allow(dead_code)]
struct Affine2 {
pub matrix2: glam::Mat2,
pub translation: glam::Vec2,
}
struct Affine2([f32; 6]);

#[derive(Type)]
#[specta(remote = glam::Affine3A, crate = crate, export = false)]
#[allow(dead_code)]
struct Affine3A {
matrix3: glam::Mat3A,
translation: glam::Vec3A,
}
struct Affine3A([f32; 12]);
};

#[cfg(feature = "url")]
Expand Down Expand Up @@ -780,15 +725,7 @@ impl<L: Type, R: Type> Type for either::Either<L, R> {
#[cfg(feature = "bevy_ecs")]
const _: () = {
#[derive(Type)]
#[specta(remote = bevy_ecs::entity::Entity, crate = crate, export = false)]
#[allow(dead_code)]
struct Entity {
generation: u32,
index: u32,
}

#[derive(Type)]
#[specta(remote = bevy_ecs::world::WorldId, crate = crate, export = false)]
#[specta(rename = "bevy_ecs::entity::Entity", remote = bevy_ecs::entity::Entity, crate = crate, export = false)]
#[allow(dead_code)]
struct WorldId(usize);
struct EntityDef(u64);
};
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod functions;
mod macro_decls;
mod map_keys;
mod optional;
mod remote_impls;
mod rename;
mod reserved_keywords;
mod selection;
Expand Down
43 changes: 43 additions & 0 deletions tests/remote_impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#[test]
#[cfg(feature = "glam")]
fn typescript_types_glam() {
use crate::ts::assert_ts;

assert_ts!(glam::DVec2, "[number, number]");
assert_ts!(glam::IVec2, "[number, number]");
assert_ts!(glam::DMat2, "[number, number, number, number]");
assert_ts!(
glam::DAffine2,
"[number, number, number, number, number, number]"
);
assert_ts!(glam::Vec2, "[number, number]");
assert_ts!(glam::Vec3, "[number, number, number]");
assert_ts!(glam::Vec3A, "[number, number, number]");
assert_ts!(glam::Vec4, "[number, number, number, number]");
assert_ts!(glam::Mat2, "[number, number, number, number]");
assert_ts!(
glam::Mat3,
"[number, number, number, number, number, number, number, number, number]"
);
assert_ts!(
glam::Mat3A,
"[number, number, number, number, number, number, number, number, number]"
);
assert_ts!(glam::Mat4, "[number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number]");
assert_ts!(glam::Quat, "[number, number, number, number]");
assert_ts!(
glam::Affine2,
"[number, number, number, number, number, number]"
);
assert_ts!(glam::Affine3A, "[number, number, number, number, number, number, number, number, number, number, number, number]");
}

#[test]
#[cfg(feature = "bevy_ecs")]
fn typescript_types_bevy_ecs() {
use specta::ts::ExportPath;

use crate::ts::assert_ts;

assert_ts!(error; bevy_ecs::entity::Entity, specta::ts::ExportError::BigIntForbidden(ExportPath::new_unsafe("bevy_ecs::entity::Entity -> u64")));
}
3 changes: 0 additions & 3 deletions tests/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,6 @@ fn typescript_types() {

// https://github.com/oscartbeaumont/specta/issues/156
assert_ts!(Vec<MyEnum>, r#"({ A: string } | { B: number })[]"#);

#[cfg(feature = "glam")]
assert_ts!(glam::DVec2, "{ x: number; y: number }");
}

#[derive(Type)]
Expand Down

0 comments on commit 3a3f4bf

Please sign in to comment.