-
Notifications
You must be signed in to change notification settings - Fork 116
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
How do I round trip ron files preserving formatting and comments? #216
Comments
Oh interesting! I'm not aware of anything like that, no. |
I would also like to see a feature to modify ron files preserving the comments. (format would be nice-to-have).
|
Could you tell more? What RON would you modify programmatically, and how, while preserving comments? |
I contribute to a rust game called veloren (https://gitlab.com/veloren/veloren/) $cat server_settings.ron
(
gameserver_address: "0.0.0.0:14004",
metrics_address: "0.0.0.0:14005",
auth_server_address: Some("https://auth.veloren.net"),
max_players: 100,
world_seed: 59686,
server_name: "Veloren Alpha aa",
server_description: "dddd",
start_time: 32400,
admins: [
"Pfau",
"zesterer",
"xMAC94x",
"Timo",
"Songtronix",
"slipped",
"Sharp",
"Acrimon",
"imbris",
"YuriMomo",
"Vechro",
"AngelOnFira",
"Nancok",
"Qutrin",
"Mckol",
"Treeco", ],
map_file: None,
persistence_db_dir: "saves",
max_view_distance: Some(30),
banned_words_files: [],
max_player_group_size: 6,
player_timeout: (
secs: 2,
nanos: 0,
),
) now the user has the option to modify his settings during the game. e.g. by writing in the chat So we are reading and writing to our settings file. In this process, all comments get lost. However, it would be awsome, to allow comments. Or a user wants to temporarrily removes an admin from the list. but when he comments a person out, and restarts the game (and the file got saved again) the name is completly lost. |
Interesting, thank you for the info! |
Yes: https://github.com/ordian/toml_edit
|
I don't think it's using serde though. |
Maybe like with cbor rewrite, RON should have two crates: low-level serde-independent and high-level serde-based? |
A full rewrite on non-serde would also open up some possibilities, but that's a huge amount of work that we don't know how to allocate resources into. So this would unlikely happen until somebody motivated would do it. |
I'd also really like to have this feature, also for modifying user-written config files. Btw, is there something like serde that preserves whitespace & comments, that any format can piggyback on? If not, how hard would it be to write? |
One workaround I can think of, to get this feature with the current crates, (but with some overhead) is:
The source map would have to have a tree structure (matching the type nesting) so that it won't result in dumb string comparison when reapplying the source map. Actually I think the process can be simplified with a custom
This info can be stored in types that are isomorphic to the original types: Instead of the original types, each field has the type @xMAC94x Would you be interested in this workaround? :) |
Might be worth looking at Serde alternatives for this, such as https://github.com/dtolnay/miniserde and https://github.com/not-fl3/nanoserde |
@kvark It looks like those 2 crates also don't preserve whitespace/comment information. |
@Boscop sounds like a feasible workaround. it would be cool if there exist a procmacro/Serializer or at least an example coding for that :) |
@xMAC94x I implemented this approach once, for my transpiler from Rust to "Rust with pythonic syntax" because I needed to have the exact spans of all tokens, to be able to generate a sourcemap, so that I could backtranslate the lines/columns of compilation errors back to lines/columns in the pythonic source code (in my cargo proxy). |
Just to express interest in this old issue, I want comments in the config file that explain what the specific option does. Ideally, it would just take the doc comments of the field and insert those into the serialized format. pretty much all existing crates like toml/yaml/json/xml parsers have atleast one issue asking for this. but it seems this is not happening as serde doesn't provide doc comments to the backend crates :( having comments in the config file really helps users to be a little more daring and try out various options, so its a good thing to have imo. one of the core strengths of Rust is documentation of libraries. compared to older languages, documentation was part of the codebase for rust from early on and it makes sense for RON to consider it :) |
What if, to store data about comments and whitespace style, do one of:
|
Bump. Still an issue. Also, serde independence (or ability to use something smaller) would be very welcome. |
Given ron is designed to support all of serdes data model, and serde is a rust standard, I think serde independence is likely a bad idea, at least for the main parser (although an altnerate fork such as ron-noserde maybe, but as mentioned that is a lot of work). I think the work around suggested by xMAC94x could be good! Though I highly suspect that a non-destructive serialisation would come with performance downsides, so it should be a feature that swaps the standard functions for a non-destructive set. |
Maybe it should be something like
Serde is something like "greatest common divisor" of all parsing formats - it allows swapping formats for simple cases, but when you need something tricky (human-editable files, super performance, support of somewhat incorrect files), you often skip Serde and work directly with a low-level library. Each format being represented as a Serde library (if possible) is a good idea; formats being limited to what Serde can is not, even for formats which are directly inspired by Serde. |
I think that's a good way to think about it. @torkleyy in the past put some work into a rewrite of ron that has its own AST parsing with more functionality and then uses that to also provide serde features. As we are trying to make ron more robust, in particular to enhance its |
and
I did not realise that, I thought serde did some of the heavier lifting for parts. As long as there is a simple serde interface I would have no problems of it being independent now that I know that. (Even though I see myself working with lowlevel-ron directory a considerable amount for non-destructive serialisation, a serde interface is very nice to have for more plug-and-play use cases). And having serde independce would allow for non-destructive serialisation without workarounds, which is good imo! |
Are there Ron parsers for making automated edits to Ron files?
As far as I understand, whitespace-preserving parsing should be based on something other that Serde. Does Ron have some Ron-specific crate that goes beyond Serde's API?
The text was updated successfully, but these errors were encountered: