Skip to content

Commit 415942b

Browse files
authored
feat: create Language Agnostic Declaration file format and ladfile crate (#274)
* feat: create `lad` file format * remove comment * move ladfile into it's own crate * fix test * disable bless * configure crates.io and release-plz
1 parent bf52605 commit 415942b

File tree

14 files changed

+1417
-58
lines changed

14 files changed

+1417
-58
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ members = [
8888
"crates/bevy_mod_scripting_functions",
8989
"crates/xtask",
9090
"crates/script_integration_test_harness",
91-
"crates/bevy_mod_scripting_derive",
91+
"crates/bevy_mod_scripting_derive", "crates/ladfile",
9292
]
9393
resolver = "2"
9494
exclude = ["crates/bevy_api_gen", "crates/macro_tests"]

crates/bevy_mod_scripting_core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ bevy = { workspace = true, default-features = false, features = [
3434
"bevy_asset",
3535
"reflect_functions",
3636
] }
37+
3738
thiserror = "1.0.31"
3839
parking_lot = "0.12.1"
3940
dashmap = "6"
@@ -43,6 +44,7 @@ derivative = "2.2"
4344
profiling = { workspace = true }
4445
bevy_mod_scripting_derive = { workspace = true }
4546

47+
4648
[dev-dependencies]
4749
test_utils = { workspace = true }
4850
tokio = { version = "1", features = ["rt", "macros"] }

crates/bevy_mod_scripting_core/src/bindings/function/into_ref.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ pub trait IntoScriptRef {
3030
/// a utility for matching types by their [`std::any::TypeId`]
3131
macro_rules! match_by_type {
3232
(match $on:ident {$($id:ident : $ty:ty => $conv:expr),*}) => {
33-
$(
34-
#[allow(unused_variables)]
35-
let $id = std::any::TypeId::of::<$ty>();
36-
)*
37-
3833
match $on {
3934
$(
4035
$id if $id == std::any::TypeId::of::<$ty>() => {$conv},

crates/bevy_mod_scripting_core/src/bindings/function/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ mod test {
6969
assert_eq!(test_fn.info.arg_info[1].name, Some("_arg1".into()));
7070

7171
assert_eq!(
72-
test_fn.info.return_info.as_ref().unwrap().type_id,
72+
test_fn.info.return_info.type_id,
7373
std::any::TypeId::of::<()>()
7474
);
7575
}

crates/bevy_mod_scripting_core/src/docgen/info.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct FunctionInfo {
2323
/// Information about the arguments of the function.
2424
pub arg_info: Vec<FunctionArgInfo>,
2525
/// Information about the return value of the function.
26-
pub return_info: Option<FunctionReturnInfo>,
26+
pub return_info: FunctionReturnInfo,
2727
/// Documentation for the function.
2828
pub docs: Option<Cow<'static, str>>,
2929
}
@@ -41,7 +41,7 @@ impl FunctionInfo {
4141
name: Cow::Borrowed(""),
4242
namespace: Namespace::Global,
4343
arg_info: Vec::new(),
44-
return_info: None,
44+
return_info: FunctionReturnInfo::default(),
4545
docs: None,
4646
}
4747
}
@@ -52,7 +52,7 @@ impl FunctionInfo {
5252
name,
5353
namespace,
5454
arg_info: Vec::new(),
55-
return_info: None,
55+
return_info: FunctionReturnInfo::default(),
5656
docs: None,
5757
}
5858
}
@@ -69,7 +69,7 @@ impl FunctionInfo {
6969

7070
/// Add a return value to the function info.
7171
pub fn add_return<T: TypedThrough + 'static>(mut self) -> Self {
72-
self.return_info = Some(FunctionReturnInfo::new_for::<T>());
72+
self.return_info = FunctionReturnInfo::new_for::<T>();
7373
self
7474
}
7575

@@ -136,6 +136,12 @@ pub struct FunctionReturnInfo {
136136
pub type_id: TypeId,
137137
}
138138

139+
impl Default for FunctionReturnInfo {
140+
fn default() -> Self {
141+
Self::new_for::<()>()
142+
}
143+
}
144+
139145
impl FunctionReturnInfo {
140146
/// Create a new function return info for a specific type.
141147
pub fn new_for<T: 'static>() -> Self {
@@ -169,7 +175,10 @@ bevy::utils::all_tuples!(impl_documentable, 0, 13, T);
169175

170176
#[cfg(test)]
171177
mod test {
172-
use crate::bindings::function::from::{Mut, Ref, Val};
178+
use crate::{
179+
bindings::function::from::{Mut, Ref, Val},
180+
docgen::typed_through::UntypedWrapperKind,
181+
};
173182

174183
use super::*;
175184

@@ -183,7 +192,7 @@ mod test {
183192
assert_eq!(info.name, "test_fn");
184193
assert_eq!(info.namespace, Namespace::Global);
185194
assert_eq!(info.arg_info.len(), 2);
186-
assert_eq!(info.return_info.unwrap().type_id, TypeId::of::<f64>());
195+
assert_eq!(info.return_info.type_id, TypeId::of::<f64>());
187196

188197
assert_eq!(info.arg_info[0].type_id, TypeId::of::<i32>());
189198
assert_eq!(info.arg_info[1].type_id, TypeId::of::<f32>());
@@ -211,33 +220,33 @@ mod test {
211220
assert_eq!(info.name, "test_fn");
212221
assert_eq!(info.namespace, Namespace::Global);
213222
assert_eq!(info.arg_info.len(), 2);
214-
assert_eq!(info.return_info.unwrap().type_id, TypeId::of::<Val<f64>>());
223+
assert_eq!(info.return_info.type_id, TypeId::of::<Val<f64>>());
215224

216225
assert_eq!(info.arg_info[0].type_id, TypeId::of::<Ref<'static, i32>>());
217226
assert_eq!(info.arg_info[1].type_id, TypeId::of::<Mut<'static, f32>>());
218227

219-
match info.arg_info[0].type_info.as_ref().unwrap() {
220-
ThroughTypeInfo::UntypedWrapper {
228+
match &info.arg_info[0].type_info {
229+
Some(ThroughTypeInfo::UntypedWrapper {
221230
through_type,
222231
wrapper_type_id,
223-
wrapper_name,
224-
} => {
232+
wrapper_kind,
233+
}) => {
225234
assert_eq!(through_type.type_id(), TypeId::of::<i32>());
226235
assert_eq!(*wrapper_type_id, TypeId::of::<Ref<'static, i32>>());
227-
assert_eq!(*wrapper_name, "Ref");
236+
assert_eq!(*wrapper_kind, UntypedWrapperKind::Ref);
228237
}
229238
_ => panic!("Expected UntypedWrapper"),
230239
}
231240

232-
match info.arg_info[1].type_info.as_ref().unwrap() {
233-
ThroughTypeInfo::UntypedWrapper {
241+
match &info.arg_info[1].type_info {
242+
Some(ThroughTypeInfo::UntypedWrapper {
234243
through_type,
235244
wrapper_type_id,
236-
wrapper_name,
237-
} => {
245+
wrapper_kind,
246+
}) => {
238247
assert_eq!(through_type.type_id(), TypeId::of::<f32>());
239248
assert_eq!(*wrapper_type_id, TypeId::of::<Mut<'static, f32>>());
240-
assert_eq!(*wrapper_name, "Mut");
249+
assert_eq!(*wrapper_kind, UntypedWrapperKind::Mut);
241250
}
242251
_ => panic!("Expected UntypedWrapper"),
243252
}

crates/bevy_mod_scripting_core/src/docgen/typed_through.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,25 @@ pub enum ThroughTypeInfo {
3737
/// The type id of the wrapper type.
3838
wrapper_type_id: TypeId,
3939
/// The name of the wrapper type.
40-
wrapper_name: &'static str,
40+
wrapper_kind: UntypedWrapperKind,
4141
},
4242
/// A wrapper around a through typed type, which itself is also a `Typed` type.
4343
TypedWrapper(TypedWrapperKind),
4444
/// an actual type info
4545
TypeInfo(&'static TypeInfo),
4646
}
4747

48+
#[derive(Debug, Clone, PartialEq, Eq)]
49+
/// The kind of untyped wrapper.
50+
pub enum UntypedWrapperKind {
51+
/// A reference wrapper.
52+
Ref,
53+
/// A mutable reference wrapper.
54+
Mut,
55+
/// A value wrapper.
56+
Val,
57+
}
58+
4859
/// The kind of typed wrapper.
4960
#[derive(Debug, Clone)]
5061
pub enum TypedWrapperKind {
@@ -73,7 +84,7 @@ impl<T: Typed> TypedThrough for Ref<'_, T> {
7384
ThroughTypeInfo::UntypedWrapper {
7485
through_type: T::type_info(),
7586
wrapper_type_id: TypeId::of::<Ref<T>>(),
76-
wrapper_name: "Ref",
87+
wrapper_kind: UntypedWrapperKind::Ref,
7788
}
7889
}
7990
}
@@ -83,7 +94,7 @@ impl<T: Typed> TypedThrough for Mut<'_, T> {
8394
ThroughTypeInfo::UntypedWrapper {
8495
through_type: T::type_info(),
8596
wrapper_type_id: TypeId::of::<Mut<T>>(),
86-
wrapper_name: "Mut",
97+
wrapper_kind: UntypedWrapperKind::Mut,
8798
}
8899
}
89100
}
@@ -93,7 +104,7 @@ impl<T: Typed> TypedThrough for Val<T> {
93104
ThroughTypeInfo::UntypedWrapper {
94105
through_type: T::type_info(),
95106
wrapper_type_id: TypeId::of::<Val<T>>(),
96-
wrapper_name: "Val",
107+
wrapper_kind: UntypedWrapperKind::Val,
97108
}
98109
}
99110
}

crates/ladfile/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

crates/ladfile/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "ladfile"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Maksymilian Mozolewski <makspl17@gmail.com>"]
6+
license = "MIT OR Apache-2.0"
7+
description = "Language Agnostic Declaration (LAD) file format for the bevy_mod_scripting crate"
8+
repository = "https://github.com/makspll/bevy_mod_scripting"
9+
homepage = "https://github.com/makspll/bevy_mod_scripting"
10+
keywords = ["bevy", "gamedev", "scripting", "format", "json"]
11+
categories = ["game-development", "parser-implementations"]
12+
readme = "readme.md"
13+
14+
[dependencies]
15+
bevy_mod_scripting_core = { workspace = true }
16+
# I don't think bevy has a top level feature for this :C
17+
bevy_reflect = { version = "0.15.1", features = ["documentation"] }
18+
serde = { version = "1.0", features = ["derive"] }
19+
serde_json = "1.0"
20+
indexmap = { version = "2.7", features = ["serde"] }
21+
22+
[lints]
23+
workspace = true

0 commit comments

Comments
 (0)