Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums with custom types #25

Closed
RandyMarsh1 opened this issue Mar 29, 2023 · 3 comments · Fixed by #33
Closed

Enums with custom types #25

RandyMarsh1 opened this issue Mar 29, 2023 · 3 comments · Fixed by #33

Comments

@RandyMarsh1
Copy link

RandyMarsh1 commented Mar 29, 2023

Is it possible to have Enum variants that take a self named struct parameter and output as types?

#[tsync]
enum Message {
    MyStruct1(MyStruct1),
    MyStruct2(MyStruct2),
}

The ideal output: type Message = | MyStruct1 | MyStruct2; where the structs are types, not strings

.

This would allow easy structuring of JSON in Rust and Destructing in TypeScript, Example:

#[tsync]
#[derive(Serialize)]
struct MyStruct1 {
    data1: bool,
    data2: MyStruct2,
}

#[tsync]
#[derive(Serialize)]
struct MyStruct2 {
    data1: String,
    data2: i32,
}

#[tsync]
#[derive(Serialize)]
enum Message {
    MyStruct1(MyStruct1),
    MyStruct2(MyStruct2),
}

fn main() {
    let message = Message::MyStruct2(MyStruct2 {
        data1: "hello".to_string(),
        data2: 42,
    });

    let json = serde_json::to_string(&message).unwrap();
    println!("Send to frontend: {}", json);
    // Output: {"MyStruct2":{"data1":"hello","data2":42}}
}

@Wulf
Copy link
Owner

Wulf commented Mar 30, 2023

hey @RandyMarsh1, thanks for reporting this :)

Let me know if 1.7.0 doesn't work for you. I think eventually what we'll want to do is namespace a lot of this output. Closing this for now, feel free to re-open it

@Wulf Wulf closed this as completed Mar 30, 2023
@RandyMarsh1
Copy link
Author

RandyMarsh1 commented Mar 31, 2023

Thanks, nested structs within structs now works, this is a great improvement 👍

However the specific scenario I was hoping for was Rust Enums that take a struct of the same name as input parameter like this:

#[tsync]
#[derive(Serialize)]
enum Message {
    MyStruct1(MyStruct1), <-- enum variant name == struct name
    MyStruct2(MyStruct2),
}

This currently does not output anything and is skipped.

The following does output a type but the variants are Strings, not Interfaces.

#[tsync]
#[derive(Serialize)]
enum Message2 {
    MyStruct1,
    MyStruct2,
}

This admittable requires a somewhat opinionated structure but I think is a nice clean way of generating JSON with the Enum variant as key and Struct as Value. The Example code in original post should compile and demonstrates the use case.

@Wulf Wulf reopened this Apr 2, 2023
@Wulf
Copy link
Owner

Wulf commented Apr 2, 2023

Ah that should be possible too -- do you mind lending me a hand with this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants