rust-embedded / svd2rust

Generate Rust register maps (`struct`s) from SVD files
Apache License 2.0
687 stars 149 forks source link

Enum typename considering name in enumeratedValues #612

Closed luojia65 closed 2 years ago

luojia65 commented 2 years ago

svd2rust has a good mechanism to parse enumeratedValues into Rust enums. However, current code would pick name of the first field the enum resides, and use pub type to re-export it into other names.

SVD files provide name field in enumeratedValues, this commit make use of them to name the Rust enums it generated. After this commit, more information of SVD file is considered in output pac crate. Users may also discover a significant drop in amount of types, which will speed up rustdoc generation and crate compilation.

Before this pull request:

![图片](https://user-images.githubusercontent.com/40385009/169642280-9b95b8df-6088-402e-85e9-2918fa8b1896.png)

After this pull request:

Note that INTERRUPT_ENABLE_A comes from enumeration name InterruptEnable, but not register name interrupt_enable. ![图片](https://user-images.githubusercontent.com/40385009/169642143-6d298868-1141-49e5-8f4e-70d5b62bb4b3.png)

SVD file for this example:

```xml fifo_error_enable Transmit or receive FIFO error interrupt enable 29 29 true InterruptEnable enable Enable interrupt 1 disable Disable interrupt 0 arbitrate_lost_enable Arbitration lost interrupt enable 28 28 true not_acknowledged_enable Not-acknowledged response interrupt enable 27 27 true ```
rust-highfive commented 2 years ago

r? @Emilgardis

(rust-highfive has picked a reviewer for you, use r? to override)

burrbull commented 2 years ago

Some of enums can be exported from other registers/peripherals (read modules)

luojia65 commented 2 years ago

Looks like MSRV 1.51.0 does not support new style format macro: format!("{variable}")

Emilgardis commented 2 years ago

Looks like MSRV 1.51.0 does not support new style format macro: format!("{variable}")

it was added in 1.58.0

Emilgardis commented 2 years ago

im not sure I like this change as it currently stands, it is imo less clear what is happening in the docs

luojia65 commented 2 years ago

There are register spec type parameters in BitWriters and FieldWriters, resulting in that write proxies cannot be re-exported properly; I can't apply enum names directly for it would result in conflict (writes with different constant values). Any good opinion?

burrbull commented 2 years ago

cannot be re-exported properly

and should not be

I like idea to use enumaratedValues name for enums, but not for readers and writers.

burrbull commented 2 years ago

Could you move ConstantCase, docs and other code style changes in separate PR? Possibly it makes sense to collect all case related traits in 1 ToCase.

luojia65 commented 2 years ago

I'll try to split this pull request, thanks! ;)