feat: Initial limited implementation

This initial implementation only supports structs with named field and
basic enum. It also does not support all the available primitives,
although when needed these are very easy to implement.
This commit is contained in:
2025-09-14 04:01:11 +02:00
commit 619d1d7ec7
16 changed files with 890 additions and 0 deletions

66
tests/notification.rs Normal file
View File

@@ -0,0 +1,66 @@
use lua_typed::Typed;
#[derive(Typed)]
#[repr(u8)]
#[serde(rename_all = "snake_case")]
pub enum Priority {
Min = 1,
Low,
Default,
High,
Max,
}
#[test]
fn priority() {
insta::assert_snapshot!(<Priority as Typed>::generate_full().unwrap(), @r#"
---@alias Priority
---| "min"
---| "low"
---| "default"
---| "high"
---| "max"
"#);
}
#[derive(Typed)]
pub struct Action {
// #[serde(flatten)]
// pub action: ActionType,
pub label: String,
pub clear: Option<bool>,
}
// #[test]
// fn action() {
// insta::assert_snapshot!(<Action as Typed>::generate_full().unwrap(), @r#"
// ---@class Action
// ---@field action "broadcast"
// ---@field extras table<string, string>?
// ---@field label string?
// ---@clear clear bool?
// "#);
// }
#[derive(Typed)]
pub struct Notification {
pub title: String,
pub message: Option<String>,
#[serde(default)]
pub tags: Vec<String>,
pub priority: Option<Priority>,
#[serde(default)]
pub actions: Vec<Action>,
}
#[test]
fn notification() {
insta::assert_snapshot!(<Notification as Typed>::generate_full().unwrap(), @r"
---@class Notification
---@field title string
---@field message string?
---@field tags string[]?
---@field priority Priority?
---@field actions Action[]?
");
}

5
tests/ui.rs Normal file
View File

@@ -0,0 +1,5 @@
#[test]
fn ui() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/*.rs");
}

View File

@@ -0,0 +1,11 @@
use lua_typed::Typed;
#[derive(Typed)]
enum Test {
A,
B(String),
C(u8),
D { test: f32 },
}
fn main() {}

View File

@@ -0,0 +1,17 @@
error: Only basic enums are supported by Typed
--> tests/ui/complex_struct.rs:6:6
|
6 | B(String),
| ^^^^^^^^
error: Only basic enums are supported by Typed
--> tests/ui/complex_struct.rs:7:6
|
7 | C(u8),
| ^^^^
error: Only basic enums are supported by Typed
--> tests/ui/complex_struct.rs:8:7
|
8 | D { test: f32 },
| ^^^^^^^^^^^^^

View File

@@ -0,0 +1,9 @@
use lua_typed::Typed;
#[derive(Typed)]
#[serde(rename_all = "invalid/case")]
enum Test {
HelloWorld,
}
fn main() {}

View File

@@ -0,0 +1,5 @@
error: Typed does not support this type of rename
--> tests/ui/invalid_rename.rs:4:22
|
4 | #[serde(rename_all = "invalid/case")]
| ^^^^^^^^^^^^^^

6
tests/ui/tuple_struct.rs Normal file
View File

@@ -0,0 +1,6 @@
use lua_typed::Typed;
#[derive(Typed)]
pub struct Test(u8);
fn main() {}

View File

@@ -0,0 +1,5 @@
error: Tuple structs are not supported by Typed
--> tests/ui/tuple_struct.rs:4:16
|
4 | pub struct Test(u8);
| ^^^^

9
tests/ui/union.rs Normal file
View File

@@ -0,0 +1,9 @@
use lua_typed::Typed;
#[derive(Typed)]
union MyUnion {
f1: u32,
f2: f32,
}
fn main() {}

5
tests/ui/union.stderr Normal file
View File

@@ -0,0 +1,5 @@
error: Unions are not supported by Typed
--> tests/ui/union.rs:4:1
|
4 | union MyUnion {
| ^^^^^