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

Form derivation macros insists on string literals. #490

Closed
horned-sphere opened this issue Mar 15, 2022 · 3 comments
Closed

Form derivation macros insists on string literals. #490

horned-sphere opened this issue Mar 15, 2022 · 3 comments
Assignees
Labels
bug Something isn't working form Related to Recon forms. macros

Comments

@horned-sphere
Copy link
Collaborator

Describe the bug
When using the Form derivation macro and specifying field names/enumeration tags etc, for example:

pub enum Envelope {
    #[form(tag = "auth")]
    Auth {
        #[form(body)]
        body: Option<Value>,
    },
    ...
}

the macro will only work if them name is a literal. If a constant is specified:

const AUTH: &str = "auth";
pub enum Envelope {
    #[form(tag = AUTH)]
    Auth {
        #[form(body)]
        body: Option<Value>,
    },
    ...
}

a compiler error is produced.

Expected behavior
The macro should accept a path to a constant string in addition to a literal.

@horned-sphere horned-sphere added bug Something isn't working macros form Related to Recon forms. labels Mar 15, 2022
@horned-sphere horned-sphere changed the title Form derivation macros insist on string literals. Form derivation macros insists on string literals. Mar 15, 2022
@DobromirM
Copy link
Member

Unfortunately it looks like this is not supported by the Rust compiler:
rust-lang/rust#52393 (comment)

There is a workaround using proc macros:
rust-lang/rust#78835 (comment)

but apparently it is deliberately implemented to work only for the top level and not with nested attributes:
rust-lang/rust#78835 (comment)

@DobromirM
Copy link
Member

As a potential workaround we can treat any uppercase string as a constant, but I am not sure how intuitive it will be.

@DobromirM
Copy link
Member

I've tested more things and it is possible to add support for custom attributes by creating our own attribute parser instead of using the default syn::Meta parser. However that leads to a more complex structure and duplicates some of the code from the syn library. Furthermore, because constants are evaluated much later than macros there is no way to get their actual value and validation for duplicate tags will not be possible.

Example:

const TEST_TAG: &str = "test";

#[derive(Form)]
enum Foo{
    #[form(tag = "test")]
    A,
    /// There is no way to check what the actual value of TEST_TAG is.
      #[form(tag = TEST_TAG)]
    B,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working form Related to Recon forms. macros
Projects
None yet
Development

No branches or pull requests

3 participants