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

codegen: enum #16

Closed
joleeee opened this issue Oct 18, 2022 · 3 comments
Closed

codegen: enum #16

joleeee opened this issue Oct 18, 2022 · 3 comments

Comments

@joleeee
Copy link

joleeee commented Oct 18, 2022

I don't know the best way to do this, but it'd be really great if the dsdl definitions could compile into rust enums, for instance if you have

uint8 REQ_A = 0
uint8 REQ_B = 1

it would be compiled into

enum REQ {
    A = 0,
    B = 1,
}

Of course this would be kinda hard to do automatically as well as this with regards to naming in a predictable way, but anything like this would be really great. Could do it automatically if the file just contains a bunch of defines like this, maybe look for a specific comment, or take a config file / cli args.

Ways I can think of

Note: we can use rust-like #[] because # declares a comment in dsdl

Start & end

#[canadensis(enum_start)]
uint8 REQ_A = 0
uint8 REQ_B = 1
#[canadensis(enum_end)]

Explicit member

Here we can specify the name of the enum, and it would remove this prefix from the enum members, like in the rust example above.

#[canadensis_enum(REQ)]
uint8 REQ_A = 0
#[canadensis_enum(REQ)]
uint8 REQ_B = 1

Smart compiler

If the "block" consists of constants of the same type, just turn them into an enum

@pavel-kirienko
Copy link

Related: OpenCyphal/specification#52

@samcrow
Copy link
Owner

samcrow commented Mar 31, 2023

I made some changes a while ago, but now I've released the new version. It uses a special #[canadensis(enum)] comment in a DSDL type to trigger enum code generation.

Documentation: https://github.com/samcrow/canadensis/blob/85cb16bc7c06f5d42438d4e34afd42d002e6ca14/canadensis_codegen_rust/src/struct_as_enum.rs
Example DSDL files: https://github.com/samcrow/canadensis/tree/master/canadensis_codegen_rust/tests/compile_pass/canadensis

@joleeee
Copy link
Author

joleeee commented Mar 31, 2023

amazing!

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

No branches or pull requests

3 participants