-
Notifications
You must be signed in to change notification settings - Fork 111
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
Serialize doc comments #376
Comments
Seeing as this was rejected by serde, there isn't room for this to exist in the |
While parsing toml with manual comments is supported, writing to a toml file with comments creates an unreadable corrupted file at worst, or wipes out the comments at best. |
In what way are corrupted files written?
As I said, |
Is it possible to add a comment type as another variant to the Mainly, I want to be able to produce partially customized files that the reader can read through and customize afterwards. They are partially customized in two ways:
Thus, the comments are more than just whitespace, they are a necessary feature of the file. |
The |
OK, I just checked it out. I think that will work for my use case. Thank you! |
Just mentioning it here because it took me a while to figure out - the way to add comments with |
Thanks this was very helpful! Here's an example for posterity: use documented::DocumentedFields;
use serde::Serialize;
use struct_field_names_as_array::FieldNamesAsArray;
use toml_edit::ser::to_document;
/// A test struct
#[derive(Default, DocumentedFields, FieldNamesAsArray, Serialize)]
struct TestMe {
/// First field
///
/// Second line
field1: u32,
/// Second field
field2: String,
}
let test = TestMe::default();
let mut doc = to_document(&test).unwrap();
for (mut key, _value) in doc.iter_mut() {
let key_name = key.get().to_owned();
let decor = key.decor_mut();
let docstring = TestMe::get_field_comment(key_name).unwrap();
let mut comment = String::new();
for line in docstring.lines() {
let line = if line.is_empty() {
String::from("#\n")
} else {
format!("# {line}\n")
};
comment.push_str(&line);
}
decor.set_prefix(comment);
}
let actual = doc.to_string();
let expected = "# First field\n#\n# Second line\nfield1 = 0\n# Second field\nfield2 = \"\"\n";
assert_eq!(actual, expected); |
@Notgnoshi Thanks for the example. I'm trying to generate a default config file that includes comments, so this is pretty much exactly what I need. An update on the APIs used here:
P.s. It's wild to see what was originally just my own toy crate ( |
And this is what I ended up with. Just thought I'd share it with anyone who needs a simple (well, somewhat simple) and practical example. |
I've spent two days trying to add doc comment support to my personal config derive macro crate. Getting the comments was easy, especially with the However, I made things harder on myself by trying to use I realize this isn't really what Because of the recursive nature of this, when you change one part of the prefix in one area it would break for recursive areas, and constantly running into deprecated decor that seemed to previously work but now now requires way more work to place the comments exactly where they should be. |
This issue existed before the move to
toml_edit
toml-rs/toml-rs#274The issue was also brought up to serde serde-rs/serde#1430
It would be a nice feature to write comments for config files, whether it uses serde or a macro
(I think the prior would be the cleanest) 👍
The text was updated successfully, but these errors were encountered: