Skip to content
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

Feature Request: Add an option to preserve leading whitespace in keys #126

Open
prestomation opened this issue Mar 17, 2024 · 1 comment

Comments

@prestomation
Copy link

I have what I expect to be a common case where I'm reading a ini file with rust-ini, modifying it, and then writing it back.

This particular application uses some significant white space. See documentation here

There are some particular keys that are expected to be indented. When reading using rust-ini, this indentation is lost.

Here is a unit test snippet that shows the behavior:

#[test]
fn test_aws_sdk_services_whitespace() {
  let config = r#"[profile dev]
services=my-services

[services my-services]
dynamodb= 
  endpoint_url=http://localhost:8000
"#;

  let aws_config: Ini = Ini::load_from_str(config).unwrap();
  let mut w = Vec::new();
  aws_config.write_to(&mut w).ok();
  let output = String::from_utf8(w).ok().unwrap();
  assert_eq!(config, output);
}

This fails like this, notice the missing whitespace before endpoint_url

assertion `left == right` failed
  left: "[profile dev]\nservices=my-services\n\n[services my-services]\ndynamodb= \n  endpoint_url=http://localhost:8000\n"
 right: "[profile dev]\nservices=my-services\n\n[services my-services]\ndynamodb=\nendpoint_url=http://localhost:8000\n"
@prestomation prestomation changed the title Feature Request: Add an option to preserve whitespace Feature Request: Add an option to preserve leading whitespace in keys Mar 18, 2024
@zonyitoo
Copy link
Owner

zonyitoo commented Mar 18, 2024

Emm.. This is not an easy request.

The key issue is to preserve surrounding whitespaces right here:

rust-ini/src/lib.rs

Lines 1339 to 1345 in becdb26

_ => match self.parse_key() {
Ok(mut mkey) => {
mkey.trim_in_place();
curkey = mkey;
}
Err(e) => return Err(e),
},

But leading white spaces are removed from self.parse_whitespace() before and after each loop. So, Emm...

Those self.parse_whitespace() calls are necessary because the parse need to see what is the next element to be parsed. So it right now requires to see whether the next valid character is either [, =, ;, ...

    # This is a line with only comments
   key=value  # This is a key-value pair, which key has leading whitespaces

   [Section]  # This is a section, the leading whitespaces should be ignored.

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

2 participants