rust-embedded / svd2rust

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

Use one trait for all cases; use constant case for structures #626

Closed luojia65 closed 2 years ago

luojia65 commented 2 years ago

This pull request is the first part of https://github.com/rust-embedded/svd2rust/pull/612. It merges all ToSanitizedXxxCase traits in one ToSanitizedCase trait. It also replace all use of upper cases into constant cases, in case the source file did not provide underline between words. It rename variables related to cases, for example name_sc to name_snake_case to be explict.

The changed code merges original case conversation traits into one trait:

/// Convert self string into specific case without overlapping to svd2rust internal names
pub trait ToSanitizedCase {
    /// Convert self into PascalCase.
    ///
    /// Use on name of enumeration values.
    fn to_sanitized_pascal_case(&self) -> Cow<str>;
    /// Convert self into CONSTANT_CASE.
    ///
    /// Use on name of reader structs, writer structs and enumerations.
    fn to_sanitized_constant_case(&self) -> Cow<str>;
    /// Convert self into snake_case, must use only if the target is used with extra prefix or suffix.
    fn to_sanitized_not_keyword_snake_case(&self) -> Cow<str>;
    /// Convert self into snake_case target and ensure target is not a Rust keyword.
    ///
    /// If the sanitized target is a Rust keyword, this function adds an underline `_`
    /// to it.
    ///
    /// Use on name of peripheral modules, register modules and field modules.
    fn to_sanitized_snake_case(&self) -> Cow<str> {
        let s = self.to_sanitized_not_keyword_snake_case();
        sanitize_keyword(s)
    }
}

Note on replacing upper case to constant case: if the source svd file provide enumeration name InterruptEnable, it converts to constant case INTERRUPT_ENABLE instead of less readable INTERRUPTENABLE in current code.

rust-highfive commented 2 years ago

r? @adamgreig

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

burrbull commented 2 years ago

bors r+

bors[bot] commented 2 years ago

Build succeeded: