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

feat: rust plugin rename rust keywords with trail underscore #96

Merged

Conversation

driftluo
Copy link
Collaborator

@driftluo driftluo commented Jun 28, 2024

This PR Fix two bugs on molecule:

  1. rust keywords on table/struct can generate code, but cannot compile with rustc, for example:
array struct [byte; 2];

struct StructP {
    type: struct
}

It is OK to name a type as a rust keyword; the rust plugin will automatically camelCase the name. However, when the field of a table/struct is named as a rust keyword, the current version does not automatically handle this problem, causing the generated code to fail to compile. This PR will automatically check the field name and add an underscore to the end of the keyword to avoid keyword conflicts.

Just like the type above, in the generated code, it will become type_(Or prefix it with r#? I'm not sure)

I think the solution to keyword conflicts must be solved in each language's plugins.

  1. Since type names support uppercase and lowercase letters, they will be uniformly camel-cased. When two types have inconsistent names but consistent camel case, uncompilable code will be generated, as in the following schema:
array struct [byte; 2];

struct StructP {
    a: struct
}

union structP {
    struct,
}

This PR will detect this situation and report an error: "the name structP is used more than once, It seems that only the capitalization is inconsistent"

other change: upgrade serde_yaml/clap/pest deps

@driftluo driftluo force-pushed the rust-plugin-add-keyword-rename-feature branch 2 times, most recently from ed0dd42 to ed8a5fd Compare June 28, 2024 11:56
eval-exec
eval-exec previously approved these changes Jun 28, 2024
tools/codegen/src/generator/languages/rust/utilities.rs Outdated Show resolved Hide resolved
@driftluo driftluo force-pushed the rust-plugin-add-keyword-rename-feature branch from ed8a5fd to 7bae730 Compare June 29, 2024 04:19
@driftluo driftluo requested a review from eval-exec July 1, 2024 03:19
eval-exec
eval-exec previously approved these changes Jul 1, 2024
@zhangsoledad
Copy link
Member

MSRV was bumped because of the introduction of https://doc.rust-lang.org/std/sync/struct.OnceLock.html, this should have been stated in the README

eval-exec
eval-exec previously approved these changes Jul 24, 2024
doitian
doitian previously approved these changes Dec 13, 2024
@driftluo driftluo dismissed stale reviews from doitian and eval-exec via 990e97b December 13, 2024 05:42
@driftluo driftluo force-pushed the rust-plugin-add-keyword-rename-feature branch from 7fc7bc5 to 990e97b Compare December 13, 2024 05:42
@driftluo driftluo force-pushed the rust-plugin-add-keyword-rename-feature branch from 990e97b to 1c16835 Compare December 13, 2024 05:46
@driftluo driftluo merged commit 1d16770 into nervosnetwork:master Dec 13, 2024
6 checks passed
@driftluo driftluo deleted the rust-plugin-add-keyword-rename-feature branch December 13, 2024 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t:enhancement Type: Feature, refactoring.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants