Skip to content

Commit

Permalink
Add colon_delimiter option and change default behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuiRuTian committed May 29, 2024
1 parent ecad660 commit 66d670f
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
use std::{
borrow::Cow,
char, error,
char,
error,
fmt::{self, Display},
fs::{File, OpenOptions},
io::{self, Read, Seek, SeekFrom, Write},
Expand Down Expand Up @@ -214,13 +215,23 @@ pub struct ParseOption {
///
/// If `enabled_escape` is true, then the value of `Key` will become `C:Windows` (`\W` equals to `W`).
pub enabled_escape: bool,

/// Allow ':' as delimiter as key value pair
/// For example
/// ```ini
/// //url:password=123
/// ```
/// If `colon_delimiter` is true, then the key is `//url` and the value is `password=123`
/// otherwise, the key is `//url:password` and the value is `123`
pub colon_delimiter: bool,
}

impl Default for ParseOption {
fn default() -> ParseOption {
ParseOption {
enabled_quote: true,
enabled_escape: true,
colon_delimiter: false,
}
}
}
Expand Down Expand Up @@ -1498,7 +1509,13 @@ impl<'a> Parser<'a> {
}

fn parse_key(&mut self) -> Result<String, ParseError> {
self.parse_str_until(&[Some('='), Some(':')], false)
let endpoint: &[Option<char>] = if self.opt.colon_delimiter {
&[Some('='), Some(':')]
} else {
&[Some('=')]
};

self.parse_str_until(endpoint, false)
}

fn parse_val(&mut self) -> Result<String, ParseError> {
Expand Down Expand Up @@ -1794,7 +1811,10 @@ gender = mail ; abdddd
name: hello
gender : mail
";
let ini = Ini::load_from_str(input).unwrap();
let mut parse_option = ParseOption::default();
parse_option.colon_delimiter = true;
let ini = Ini::load_from_str_opt(input, parse_option).unwrap();

assert_eq!(ini.get_from(Some("section name"), "name").unwrap(), "hello");
assert_eq!(ini.get_from(Some("section name"), "gender").unwrap(), "mail");
}
Expand Down Expand Up @@ -2151,6 +2171,38 @@ a3 = n3
assert_eq!(keys, vec!["x2", "xb", "a3"])
}

#[test]
fn parse_npmrc() {
let input = r"
@myorg:registry=https://somewhere-else.com/myorg
@another:registry=https://somewhere-else.com/another
//registry.npmjs.org/:_authToken=MYTOKEN
; would apply to both @myorg and @another
//somewhere-else.com/:_authToken=MYTOKEN
; would apply only to @myorg
//somewhere-else.com/myorg/:_authToken=MYTOKEN1
; would apply only to @another
//somewhere-else.com/another/:_authToken=MYTOKEN2
";
let mut parse_option = ParseOption::default();
parse_option.colon_delimiter = false;
let data = Ini::load_from_str_opt(input, parse_option).unwrap();
let (_, section) = data.into_iter().next().unwrap();
let props: Vec<_> = section.iter().collect();

assert_eq!(
props,
vec![
("@myorg:registry", "https://somewhere-else.com/myorg"),
("@another:registry", "https://somewhere-else.com/another"),
("//registry.npmjs.org/:_authToken", "MYTOKEN"),
("//somewhere-else.com/:_authToken", "MYTOKEN"),
("//somewhere-else.com/myorg/:_authToken", "MYTOKEN1"),
("//somewhere-else.com/another/:_authToken", "MYTOKEN2")
]
);
}

#[test]
fn preserve_order_write() {
let input = r"
Expand Down Expand Up @@ -2348,29 +2400,23 @@ bar = f
fn add_properties_api() {
// Test duplicate properties in a section
let mut ini = Ini::new();
ini.with_section(Some("foo"))
.add("a", "1")
.add("a", "2");
ini.with_section(Some("foo")).add("a", "1").add("a", "2");

let sec = ini.section(Some("foo")).unwrap();
assert_eq!(sec.get("a"), Some("1"));
assert_eq!(sec.get_all("a").collect::<Vec<&str>>(), vec!["1", "2"]);

// Test add with unique keys
let mut ini = Ini::new();
ini.with_section(Some("foo"))
.add("a", "1")
.add("b", "2");
ini.with_section(Some("foo")).add("a", "1").add("b", "2");

let sec = ini.section(Some("foo")).unwrap();
assert_eq!(sec.get("a"), Some("1"));
assert_eq!(sec.get("b"), Some("2"));

// Test string representation
let mut ini = Ini::new();
ini.with_section(Some("foo"))
.add("a", "1")
.add("a", "2");
ini.with_section(Some("foo")).add("a", "1").add("a", "2");
let mut buf = Vec::new();
ini.write_to(&mut buf).unwrap();
let ini_str = String::from_utf8(buf).unwrap();
Expand Down

0 comments on commit 66d670f

Please sign in to comment.