ron icon indicating copy to clipboard operation
ron copied to clipboard

Make `#[derive(Debug)]` output compatible with ron

Open HKalbasi opened this issue 1 year ago • 3 comments

This makes ron useful in deserializing output of programs using default Debug trait of Rust.

HKalbasi avatar Oct 29 '24 21:10 HKalbasi

Do you mean making RON fully compatible with Rust syntax?

juntyr avatar Oct 30 '24 04:10 juntyr

I mean making the output of #[derive(Debug)] a subset of ron syntax, so I can use println!("{:?}", value) in addition to normal serializing.

For example, X { a: 1, b: 2 } should be accepted in addition to the current X ( a: 1, b: 2 ). There is no need to drop support for the current form to make it Rust syntax compatible.

HKalbasi avatar Oct 30 '24 08:10 HKalbasi

This may be possible to do. Ron has syntax for maps (since ron is a superset of JSON), so { a: 1, b: 2 } would be parsed as a map from an enum with variants a and b to ints. I mention this because Ron usually supports omitting struct names. But if we require including the struct name for this syntax, it may be possible.

Do you want to submit a PR for this feature? This syntax addition would likely be feature-gated. Enabling the feature during serialisation would use the new syntax for structs (and struct-like enum variants), and enabling it during deserialisation would allow using the new syntax.

My guess is that it may be as simple as modifying the current deserialize_struct impl (and its variant counterpart). If the struct name was found, it could peek for an opening curly brace and then expect a closing curly brace later, otherwise it would use the current parsing rules. The logic for guessing struct types in deserialize_any would also need to be amended (this case would actually be quite simple to handle).

juntyr avatar Oct 30 '24 08:10 juntyr