feat: Add support for externally tagged enums

Also did a massive code refactor that helped simplify the
implementation.
This commit is contained in:
2025-09-16 23:21:36 +02:00
parent 6d5c2da030
commit 467a037a77
5 changed files with 277 additions and 105 deletions

View File

@@ -1,5 +1,25 @@
use std::collections::HashMap;
use lua_typed::Typed;
#[derive(Typed)]
#[serde(rename_all = "snake_case", tag = "action")]
pub enum ActionType {
Broadcast { extras: HashMap<String, String> },
// View,
// Http
}
#[test]
fn action_type() {
insta::assert_snapshot!(<ActionType as Typed>::generate_full().unwrap(), @r#"
---@class ActionType
---@field action
---| "broadcast"
---@field extras table<string, string>?
"#);
}
#[derive(Typed)]
#[repr(u8)]
#[serde(rename_all = "snake_case")]
@@ -25,22 +45,23 @@ fn priority() {
#[derive(Typed)]
pub struct Action {
// #[serde(flatten)]
// pub action: ActionType,
#[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?
// "#);
// }
#[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
---@field clear boolean?
"#);
}
#[derive(Typed)]
pub struct Notification {

View File

@@ -1,16 +1,16 @@
error: Only basic enums are supported by Typed
error: Typed supports enum variants with fields only when the enum is externally tagged
--> tests/ui/complex_struct.rs:6:6
|
6 | B(String),
| ^^^^^^^^
error: Only basic enums are supported by Typed
error: Typed supports enum variants with fields only when the enum is externally tagged
--> tests/ui/complex_struct.rs:7:6
|
7 | C(u8),
| ^^^^
error: Only basic enums are supported by Typed
error: Typed supports enum variants with fields only when the enum is externally tagged
--> tests/ui/complex_struct.rs:8:7
|
8 | D { test: f32 },

View File

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

View File

@@ -0,0 +1,5 @@
error: Typed supports enum variants with fields only when the enum is externally tagged
--> tests/ui/tuple_enum_untagged.rs:6:7
|
6 | D { test: f32 },
| ^^^^^^^^^^^^^