librasn / compiler

An ASN1 compiler producing Rust bindings for the rasn framework
Other
10 stars 7 forks source link

Snake case conversion and text-based encodings #6

Closed v0-e closed 5 months ago

v0-e commented 5 months ago

Hi @6d7a, Just want to start by saying a big thank you for the awesome work on the compiler!

On the issue, the compiler is currently replacing uppercase letters of ASN.1 identifiers to an underscore and the respective lowercase. For example the ETSI ITS ItsPduHeader is currently compiled into (using https://librasn.github.io):

#[derive(AsnType, Debug, Clone, Decode, Encode, Default, PartialEq)]
#[rasn(automatic_tags)]
pub struct ItsPduHeader {
    #[rasn(value("0..=255"))]
    pub protocol_version: u8,
    #[rasn(value("0..=255"))]
    pub message_i_d: u8,
    pub station_i_d: StationID,
}
impl ItsPduHeader {
    pub fn new(protocol_version: u8, message_i_d: u8, station_i_d: StationID) -> Self {
        Self {
            protocol_version,
            message_i_d,
            station_i_d,
        }
    }
}

That is, protocolVersion is converted to protocol_version, messageID to message_i_d, and stationID to station_i_d. In the current JER implementation, rasn uses the struct field names to encode the JSON names of name/value pairs. Instead of,

{"protocolVersion":1,"messageID":5,"stationID":999}

we have,

{"protocol_version":1,"message_i_d":5,"station_i_d":999}

Which goes a bit against X.697 27.3.2 a) 2021.

Could we add an option, or disable the conversion altogether, to preserve the original ASN.1 identifiers format in the compiler?

6d7a commented 5 months ago

Thank you for your issue. I'll add a compilation option to suppress all identifier conversions beyond the necessary - -> _ replacement in the next release. Further along, rasn will probably move towards some kind of annotation in order to conserve the original identifier in the rust bindings (something along the lines of #[identifier="stationID"]).

6d7a commented 5 months ago

As of release 0.1.2, the compiler generates the appropriate identifier annotation. I agree that further along, the identifier conversion should become configurable, but let's leave that for another PR.