feat: Add support for externally tagged enums
Also did a massive code refactor that helped simplify the implementation.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 },
|
||||
|
||||
9
tests/ui/tuple_enum_untagged.rs
Normal file
9
tests/ui/tuple_enum_untagged.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use lua_typed::Typed;
|
||||
|
||||
#[derive(Typed)]
|
||||
enum Test {
|
||||
A,
|
||||
D { test: f32 },
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
5
tests/ui/tuple_enum_untagged.stderr
Normal file
5
tests/ui/tuple_enum_untagged.stderr
Normal 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 },
|
||||
| ^^^^^^^^^^^^^
|
||||
Reference in New Issue
Block a user