-
Notifications
You must be signed in to change notification settings - Fork 79
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
feat(fmt): support fmt::Writers when writing ini to a Writer #131
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ | |
use std::{ | ||
borrow::Cow, | ||
char, error, | ||
fmt::{self, Display}, | ||
fmt::{self, Display, Write as FmtWrite}, | ||
fs::{File, OpenOptions}, | ||
io::{self, Read, Seek, SeekFrom, Write}, | ||
ops::{Index, IndexMut}, | ||
|
@@ -845,6 +845,16 @@ impl Default for Ini { | |
} | ||
} | ||
|
||
impl ToString for Ini { | ||
fn to_string(&self) -> String { | ||
let mut writer = String::new(); | ||
if let Err(err) = self.write_to_fmt(&mut writer) { | ||
panic!("{}", err.to_string()); | ||
} | ||
writer | ||
} | ||
} | ||
|
||
impl<S: Into<String>> Index<Option<S>> for Ini { | ||
type Output = Properties; | ||
|
||
|
@@ -959,6 +969,59 @@ impl Ini { | |
} | ||
} | ||
|
||
impl Ini { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this impl is a verbatim scrape of the |
||
/// Write to a writer | ||
pub fn write_to_fmt<W: FmtWrite>(&self, writer: &mut W) -> Result<(), std::fmt::Error> { | ||
self.write_to_opt_fmt(writer, Default::default()) | ||
} | ||
|
||
/// Write to a writer | ||
pub fn write_to_policy_fmt<W: FmtWrite>( | ||
&self, | ||
writer: &mut W, | ||
policy: EscapePolicy, | ||
) -> Result<(), std::fmt::Error> { | ||
self.write_to_opt_fmt( | ||
writer, | ||
WriteOption { | ||
escape_policy: policy, | ||
..Default::default() | ||
}, | ||
) | ||
} | ||
|
||
/// Write to a writer with options | ||
pub fn write_to_opt_fmt<W: FmtWrite>(&self, writer: &mut W, opt: WriteOption) -> Result<(), std::fmt::Error> { | ||
let mut firstline = true; | ||
|
||
for (section, props) in &self.sections { | ||
if !props.data.is_empty() { | ||
if firstline { | ||
firstline = false; | ||
} else { | ||
// Write an empty line between sections | ||
writer.write_str(opt.line_separator.as_str())?; | ||
} | ||
} | ||
|
||
if let Some(ref section) = *section { | ||
write!( | ||
writer, | ||
"[{}]{}", | ||
escape_str(§ion[..], opt.escape_policy), | ||
opt.line_separator | ||
)?; | ||
} | ||
for (k, v) in props.iter() { | ||
let k_str = escape_str(k, opt.escape_policy); | ||
let v_str = escape_str(v, opt.escape_policy); | ||
write!(writer, "{}{}{}{}", k_str, opt.kv_separator, v_str, opt.line_separator)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Ini { | ||
/// Load from a string | ||
pub fn load_from_str(buf: &str) -> Result<Ini, ParseError> { | ||
|
@@ -2348,29 +2411,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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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(); | ||
|
@@ -2636,4 +2693,19 @@ x3 = nb | |
] | ||
); | ||
} | ||
|
||
#[test] | ||
fn to_string_works() { | ||
let input = r" | ||
x2 = nc | ||
x1 = na | ||
x3 = nb | ||
"; | ||
let data = Ini::load_from_str(input).unwrap(); | ||
let exp = "x2=nc\nx1=na\nx3=nb\n"; | ||
assert_eq!(data.to_string(), exp); | ||
let mut my_str = String::new(); | ||
let _ = data.write_to_fmt(&mut my_str); | ||
assert_eq!(my_str, exp); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly this is negotiable, it could be nice to have but I'm perfectly fine cutting it out.