Skip to content

[Rust] Implement a to_string like method to generate a human-readable representation of decoder's fields #1051

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

Open
marciorasf opened this issue Feb 13, 2025 · 2 comments

Comments

@marciorasf
Copy link
Contributor

When using sbe-tools to generate java code, the encoders and decoders implement toString() that returns a human-readable representation of all the fields of the message. For example:

[Sequence](sbeTemplateId=9|sbeSchemaId=1|sbeSchemaVersion=3|sbeBlockLength=4):messageType=Sequence|nextSeqNo=629

Having a human-readable representation makes debugging much, much easier, as you can print the SBE messages and check what exactly you're sending/receiving.

When generating rust, currently there's nothing like the java's toString(). The encoders and decoders derive Debug, but it prints just the name of the struct and the buffer, which is not that useful.

Could we implement Display using the exactly same format as Java for both encoders and decoders?

@marciorasf
Copy link
Contributor Author

Update: I'm working on it and I expect to submit a proposal soon.

@marciorasf
Copy link
Contributor Author

My first try was to implement Display for all decoders. However, as some nested decoders need to receive self as value, it's not possible to print their fields with just &self as is the signature of Display.

Thus, I created another trait with a method that does the same thing, returns a human readable representation of the decoders and its fields, but which receives self as value. As the client will probably keep using self, the method also returns it.

The trait is defined as:

/// Returns a human-readable string representation of the SBE message.
/// This trait works like `ToString`, but it takes `self` as value
/// to be compatible with the generated decoders.
pub trait SbeToString: Sized {
    /// Returns a human-readable string along with the consumed `self`.
    fn sbe_to_string(self) -> SbeResult<(Self, String)>;
}

The returned String has the same syntax (as far as I compared) as generated by the Java decoders' toString(). For example:

[Car](sbeTemplateId=1|sbeSchemaId=1|sbeSchemaVersion=0|sbeBlockLength=45):serial_number=1234|model_year=2013|available=T|code=A|some_numbers=[1,2,3,4]|vehicle_code=abcdef|extras={sun_roof=false,sports_pack=true,cruise_control=true}|discounted_model=C|engine=(capacity=2000|num_cylinders=4|manufacturer_code=123|efficiency=35|booster_enabled=T|booster=(boost_type=NITROUS|horse_power=200))

I changed the rust generator code to implement SbeToString for all decoders, so decoders with fields which also requires decoders, like composite ones, may just call sbe_to_string for each of them.

Here is the PR with the changes. The PR is not ready, as it's missing tests and some docs. But I preferred to have some feedback earlier, before commiting more time to it.

Please let me know your thoughts about this proposal and/or suggestions.

@marciorasf marciorasf changed the title [Rust] Implement Display for encoders and decoders [Rust] Implement a to_string like method to generate a human-readable representation of decoder's fields Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant