| 1 |
// The MIT License (MIT) |
| 2 |
|
| 3 |
// Copyright (c) 2014 Y. T. CHUNG |
| 4 |
|
| 5 |
// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 6 |
// this software and associated documentation files (the "Software"), to deal in |
| 7 |
// the Software without restriction, including without limitation the rights to |
| 8 |
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 9 |
// the Software, and to permit persons to whom the Software is furnished to do so, |
| 10 |
// subject to the following conditions: |
| 11 |
|
| 12 |
// The above copyright notice and this permission notice shall be included in all |
| 13 |
// copies or substantial portions of the Software. |
| 14 |
|
| 15 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 17 |
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 |
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 19 |
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 |
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
|
| 22 |
//! Ini |
| 23 |
|
| 24 |
use std::collections::HashMap; |
| 25 |
use std::collections::hash_map::{IntoIter, Iter, IterMut, Keys}; |
| 26 |
use std::collections::hash_map::Entry; |
| 27 |
use std::fs::{File, OpenOptions}; |
| 28 |
use std::ops::{Index, IndexMut}; |
| 29 |
use std::char; |
| 30 |
use std::io::{self, Read, Write}; |
| 31 |
use std::fmt::{self, Display}; |
| 32 |
use std::path::Path; |
| 33 |
use std::str::Chars; |
| 34 |
use std::borrow::Borrow; |
| 35 |
use std::hash::Hash; |
| 36 |
use std::cmp::Eq; |
| 37 |
use std::error; |
| 38 |
|
| 39 |
#[derive(Debug, PartialEq, Copy, Clone)] |
| 40 |
pub enum EscapePolicy { |
| 41 |
/// escape absolutely nothing (dangerous) |
| 42 |
Nothing, |
| 43 |
/// only escape the most necessary things |
| 44 |
Basics, |
| 45 |
/// escape basics and non-ascii characters |
| 46 |
BasicsUnicode, |
| 47 |
/// Escape reserved symbols. |
| 48 |
Reserved, |
| 49 |
/// Escape reserved symbols and non-ascii characters |
| 50 |
ReservedUnicode, |
| 51 |
/// Escape everything that some INI implementations assume |
| 52 |
Everything, |
| 53 |
} |
| 54 |
|
| 55 |
impl EscapePolicy { |
| 56 |
fn escape_basics(&self) -> bool { |
| 57 |
match *self { |
| 58 |
EscapePolicy::Nothing => false, |
| 59 |
_ => true, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
fn escape_reserved(&self) -> bool { |
| 64 |
match *self { |
| 65 |
EscapePolicy::Reserved => true, |
| 66 |
EscapePolicy::ReservedUnicode => true, |
| 67 |
EscapePolicy::Everything => true, |
| 68 |
_ => false, |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
fn escape_unicode(&self) -> bool { |
| 73 |
match *self { |
| 74 |
EscapePolicy::BasicsUnicode => true, |
| 75 |
EscapePolicy::ReservedUnicode => true, |
| 76 |
EscapePolicy::Everything => true, |
| 77 |
_ => false, |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/// Given a character this returns true if it should be escaped as |
| 82 |
/// per this policy or false if not. |
| 83 |
pub fn should_escape(&self, c: char) -> bool { |
| 84 |
match c { |
| 85 |
'\\' | '\x00'...'\x1f' | '\x7f'...'\u{00ff}' => self.escape_basics(), |
| 86 |
';' | '#' | '=' | ':' => self.escape_reserved(), |
| 87 |
'\u{0080}'...'\u{FFFF}' => self.escape_unicode(), |
| 88 |
_ => false, |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Escape non-INI characters |
| 94 |
// |
| 95 |
// Common escape sequences: https://en.wikipedia.org/wiki/INI_file#Escape_characters |
| 96 |
// |
| 97 |
// * `\\` \ (a single backslash, escaping the escape character) |
| 98 |
// * `\0` Null character |
| 99 |
// * `\a` Bell/Alert/Audible |
| 100 |
// * `\b` Backspace, Bell character for some applications |
| 101 |
// * `\t` Tab character |
| 102 |
// * `\r` Carriage return |
| 103 |
// * `\n` Line feed |
| 104 |
// * `\;` Semicolon |
| 105 |
// * `\#` Number sign |
| 106 |
// * `\=` Equals sign |
| 107 |
// * `\:` Colon |
| 108 |
// * `\x????` Unicode character with hexadecimal code point corresponding to ???? |
| 109 |
fn escape_str(s: &str, policy: EscapePolicy) -> String { |
| 110 |
let mut escaped: String = String::with_capacity(s.len()); |
| 111 |
for c in s.chars() { |
| 112 |
// if we know this is not something to escape as per policy, we just |
| 113 |
// write it and continue. |
| 114 |
if !policy.should_escape(c) { |
| 115 |
escaped.push(c); |
| 116 |
continue; |
| 117 |
} |
| 118 |
|
| 119 |
match c { |
| 120 |
'\\' => escaped.push_str("\\\\"), |
| 121 |
'\0' => escaped.push_str("\\0"), |
| 122 |
'\x01'...'\x06' | '\x0e'...'\x1f' | '\x7f'...'\u{00ff}' => { |
| 123 |
escaped.push_str(&format!("\\x{:04x}", c as isize)[..]) |
| 124 |
} |
| 125 |
'\x07' => escaped.push_str("\\a"), |
| 126 |
'\x08' => escaped.push_str("\\b"), |
| 127 |
'\x0c' => escaped.push_str("\\f"), |
| 128 |
'\x0b' => escaped.push_str("\\v"), |
| 129 |
'\n' => escaped.push_str("\\n"), |
| 130 |
'\t' => escaped.push_str("\\t"), |
| 131 |
'\r' => escaped.push_str("\\r"), |
| 132 |
'\u{0080}'...'\u{FFFF}' => escaped.push_str(&format!("\\x{:04x}", c as isize)[..]), |
| 133 |
_ => { |
| 134 |
escaped.push('\\'); |
| 135 |
escaped.push(c); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
escaped |
| 140 |
} |
| 141 |
|
| 142 |
/// A setter which could be used to set key-value pair in a specified section |
| 143 |
pub struct SectionSetter<'a> { |
| 144 |
ini: &'a mut Ini, |
| 145 |
section_name: Option<String>, |
| 146 |
} |
| 147 |
|
| 148 |
impl<'a> SectionSetter<'a> { |
| 149 |
fn new(ini: &'a mut Ini, section_name: Option<String>) -> SectionSetter<'a> { |
| 150 |
SectionSetter { |
| 151 |
ini: ini, |
| 152 |
section_name: section_name, |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/// Set key-value pair in this section |
| 157 |
pub fn set<K, V>(&'a mut self, key: K, value: V) -> &'a mut SectionSetter<'a> |
| 158 |
where |
| 159 |
K: Into<String>, |
| 160 |
V: Into<String>, |
| 161 |
{ |
| 162 |
{ |
| 163 |
let prop = match self.ini.sections.entry(self.section_name.clone()) { |
| 164 |
Entry::Vacant(entry) => entry.insert(HashMap::new()), |
| 165 |
Entry::Occupied(entry) => entry.into_mut(), |
| 166 |
}; |
| 167 |
prop.insert(key.into(), value.into()); |
| 168 |
} |
| 169 |
self |
| 170 |
} |
| 171 |
|
| 172 |
/// Delete the entry in this section with `key` |
| 173 |
pub fn delete<K>(&'a mut self, key: &K) -> &'a mut SectionSetter<'a> |
| 174 |
where |
| 175 |
String: Borrow<K>, |
| 176 |
K: Hash + Eq + ?Sized, |
| 177 |
{ |
| 178 |
if let Some(prop) = self.ini.sections.get_mut(&self.section_name) { |
| 179 |
prop.remove(key); |
| 180 |
} |
| 181 |
self |
| 182 |
} |
| 183 |
|
| 184 |
/// Get the entry in this section with `key` |
| 185 |
pub fn get<K>(&'a mut self, key: &K) -> Option<&'a str> |
| 186 |
where |
| 187 |
String: Borrow<K>, |
| 188 |
K: Hash + Eq + ?Sized, |
| 189 |
{ |
| 190 |
self.ini |
| 191 |
.sections |
| 192 |
.get(&self.section_name) |
| 193 |
.and_then(|prop| prop.get(key).map(|s| &s[..])) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/// Properties type (key-value pairs) |
| 198 |
pub type Properties = HashMap<String, String>; // Key-value pairs |
| 199 |
|
| 200 |
/// Ini struct |
| 201 |
#[derive(Clone)] |
| 202 |
pub struct Ini { |
| 203 |
sections: HashMap<Option<String>, Properties>, |
| 204 |
} |
| 205 |
|
| 206 |
impl Ini { |
| 207 |
/// Create an instance |
| 208 |
pub fn new() -> Ini { |
| 209 |
Ini { |
| 210 |
sections: HashMap::new(), |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/// Set with a specified section, `None` is for the general section |
| 215 |
pub fn with_section<'b, S>(&'b mut self, section: Option<S>) -> SectionSetter<'b> |
| 216 |
where |
| 217 |
S: Into<String>, |
| 218 |
{ |
| 219 |
SectionSetter::new(self, section.map(|s| s.into())) |
| 220 |
} |
| 221 |
|
| 222 |
/// Get the immmutable general section |
| 223 |
pub fn general_section(&self) -> &Properties { |
| 224 |
self.section(None::<String>) |
| 225 |
.expect("There is no general section in this Ini") |
| 226 |
} |
| 227 |
|
| 228 |
/// Get the mutable general section |
| 229 |
pub fn general_section_mut(&mut self) -> &mut Properties { |
| 230 |
self.section_mut(None::<String>) |
| 231 |
.expect("There is no general section in this Ini") |
| 232 |
} |
| 233 |
|
| 234 |
/// Get a immutable section |
| 235 |
pub fn section<'a, S>(&'a self, name: Option<S>) -> Option<&'a Properties> |
| 236 |
where |
| 237 |
S: Into<String>, |
| 238 |
{ |
| 239 |
self.sections.get(&name.map(|s| s.into())) |
| 240 |
} |
| 241 |
|
| 242 |
/// Get a mutable section |
| 243 |
pub fn section_mut<'a, S>(&'a mut self, name: Option<S>) -> Option<&'a mut Properties> |
| 244 |
where |
| 245 |
S: Into<String>, |
| 246 |
{ |
| 247 |
self.sections.get_mut(&name.map(|s| s.into())) |
| 248 |
} |
| 249 |
|
| 250 |
/// Get the entry |
| 251 |
pub fn entry<'a>(&'a mut self, name: Option<String>) -> Entry<Option<String>, Properties> { |
| 252 |
self.sections.entry(name.map(|s| s.into())) |
| 253 |
} |
| 254 |
|
| 255 |
/// Clear all entries |
| 256 |
pub fn clear<'a>(&mut self) { |
| 257 |
self.sections.clear() |
| 258 |
} |
| 259 |
|
| 260 |
/// Iterate with sections |
| 261 |
pub fn sections<'a>(&'a self) -> Keys<'a, Option<String>, Properties> { |
| 262 |
self.sections.keys() |
| 263 |
} |
| 264 |
|
| 265 |
/// Set key-value to a section |
| 266 |
pub fn set_to<S>(&mut self, section: Option<S>, key: String, value: String) |
| 267 |
where |
| 268 |
S: Into<String>, |
| 269 |
{ |
| 270 |
self.with_section(section).set(key, value); |
| 271 |
} |
| 272 |
|
| 273 |
/// Get the value from a section with key |
| 274 |
/// |
| 275 |
/// Example: |
| 276 |
/// |
| 277 |
/// ``` |
| 278 |
/// use ini::Ini; |
| 279 |
/// let input = "[sec]\nabc = def\n"; |
| 280 |
/// let ini = Ini::load_from_str(input).unwrap(); |
| 281 |
/// assert_eq!(ini.get_from(Some("sec"), "abc"), Some("def")); |
| 282 |
/// ``` |
| 283 |
pub fn get_from<'a, S>(&'a self, section: Option<S>, key: &str) -> Option<&'a str> |
| 284 |
where |
| 285 |
S: Into<String>, |
| 286 |
{ |
| 287 |
match self.sections.get(§ion.map(|s| s.into())) { |
| 288 |
None => None, |
| 289 |
Some(ref prop) => match prop.get(key) { |
| 290 |
Some(p) => Some(&p[..]), |
| 291 |
None => None, |
| 292 |
}, |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/// Get the value from a section with key, return the default value if it does not exist |
| 297 |
/// |
| 298 |
/// Example: |
| 299 |
/// |
| 300 |
/// ``` |
| 301 |
/// use ini::Ini; |
| 302 |
/// let input = "[sec]\n"; |
| 303 |
/// let ini = Ini::load_from_str(input).unwrap(); |
| 304 |
/// assert_eq!(ini.get_from_or(Some("sec"), "key", "default"), "default"); |
| 305 |
/// ``` |
| 306 |
pub fn get_from_or<'a, S>(&'a self, section: Option<S>, key: &str, default: &'a str) -> &'a str |
| 307 |
where |
| 308 |
S: Into<String>, |
| 309 |
{ |
| 310 |
match self.sections.get(§ion.map(|s| s.into())) { |
| 311 |
None => default, |
| 312 |
Some(ref prop) => match prop.get(key) { |
| 313 |
Some(p) => &p[..], |
| 314 |
None => default, |
| 315 |
}, |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/// Get the mutable from a section with key |
| 320 |
pub fn get_from_mut<'a, S>(&'a mut self, section: Option<S>, key: &str) -> Option<&'a str> |
| 321 |
where |
| 322 |
S: Into<String>, |
| 323 |
{ |
| 324 |
match self.sections.get_mut(§ion.map(|s| s.into())) { |
| 325 |
None => None, |
| 326 |
Some(prop) => prop.get_mut(key).map(|s| &s[..]), |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/// Delete a section, return the properties if it exists |
| 331 |
pub fn delete<S>(&mut self, section: Option<S>) -> Option<Properties> |
| 332 |
where |
| 333 |
S: Into<String>, |
| 334 |
{ |
| 335 |
self.sections.remove(§ion.map(|s| s.into())) |
| 336 |
} |
| 337 |
|
| 338 |
pub fn delete_from<S>(&mut self, section: Option<S>, key: &str) -> Option<String> |
| 339 |
where |
| 340 |
S: Into<String>, |
| 341 |
{ |
| 342 |
match self.section_mut(section) { |
| 343 |
None => return None, |
| 344 |
Some(prop) => prop.remove(key), |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
impl<'q> Index<&'q Option<String>> for Ini { |
| 350 |
type Output = Properties; |
| 351 |
|
| 352 |
fn index<'a>(&'a self, index: &'q Option<String>) -> &'a Properties { |
| 353 |
match self.sections.get(index) { |
| 354 |
Some(p) => p, |
| 355 |
None => panic!("Section `{:?}` does not exist", index), |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
impl<'i> IndexMut<&'i Option<String>> for Ini { |
| 361 |
fn index_mut<'a>(&'a mut self, index: &Option<String>) -> &'a mut Properties { |
| 362 |
match self.sections.get_mut(index) { |
| 363 |
Some(p) => p, |
| 364 |
None => panic!("Section `{:?}` does not exist", index), |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
impl<'q> Index<&'q str> for Ini { |
| 370 |
type Output = Properties; |
| 371 |
|
| 372 |
fn index<'a>(&'a self, index: &'q str) -> &'a Properties { |
| 373 |
match self.sections.get(&Some(index.into())) { |
| 374 |
Some(p) => p, |
| 375 |
None => panic!("Section `{}` does not exist", index), |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
impl<'q> IndexMut<&'q str> for Ini { |
| 381 |
fn index_mut<'a>(&'a mut self, index: &'q str) -> &'a mut Properties { |
| 382 |
match self.sections.get_mut(&Some(index.into())) { |
| 383 |
Some(p) => p, |
| 384 |
None => panic!("Section `{}` does not exist", index), |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
impl Ini { |
| 390 |
/// Write to a file |
| 391 |
pub fn write_to_file<P: AsRef<Path>>(&self, filename: P) -> io::Result<()> { |
| 392 |
self.write_to_file_policy(filename, EscapePolicy::Basics) |
| 393 |
} |
| 394 |
|
| 395 |
/// Write to a file |
| 396 |
pub fn write_to_file_policy<P: AsRef<Path>>( |
| 397 |
&self, |
| 398 |
filename: P, |
| 399 |
policy: EscapePolicy, |
| 400 |
) -> io::Result<()> { |
| 401 |
let mut file = try!( |
| 402 |
OpenOptions::new() |
| 403 |
.write(true) |
| 404 |
.truncate(true) |
| 405 |
.create(true) |
| 406 |
.open(filename.as_ref()) |
| 407 |
); |
| 408 |
self.write_to_policy(&mut file, policy) |
| 409 |
} |
| 410 |
|
| 411 |
/// Write to a writer |
| 412 |
pub fn write_to<W: Write>(&self, writer: &mut W) -> io::Result<()> { |
| 413 |
self.write_to_policy(writer, EscapePolicy::Basics) |
| 414 |
} |
| 415 |
|
| 416 |
/// Write to a writer |
| 417 |
pub fn write_to_policy<W: Write>( |
| 418 |
&self, |
| 419 |
writer: &mut W, |
| 420 |
policy: EscapePolicy, |
| 421 |
) -> io::Result<()> { |
| 422 |
let mut firstline = true; |
| 423 |
|
| 424 |
match self.sections.get(&None) { |
| 425 |
Some(props) => { |
| 426 |
for (k, v) in props.iter() { |
| 427 |
let k_str = escape_str(&k[..], policy); |
| 428 |
let v_str = escape_str(&v[..], policy); |
| 429 |
try!(write!(writer, "{}={}\n", k_str, v_str)); |
| 430 |
} |
| 431 |
firstline = false; |
| 432 |
} |
| 433 |
None => {} |
| 434 |
} |
| 435 |
|
| 436 |
for (section, props) in self.sections.iter().filter(|&(ref s, _)| s.is_some()) { |
| 437 |
if firstline { |
| 438 |
firstline = false; |
| 439 |
} else { |
| 440 |
try!(writer.write_all(b"\n")); |
| 441 |
} |
| 442 |
|
| 443 |
if let &Some(ref section) = section { |
| 444 |
try!(write!(writer, "[{}]\n", escape_str(§ion[..], policy))); |
| 445 |
|
| 446 |
for (k, v) in props.iter() { |
| 447 |
let k_str = escape_str(&k[..], policy); |
| 448 |
let v_str = escape_str(&v[..], policy); |
| 449 |
try!(write!(writer, "{}={}\n", k_str, v_str)); |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
Ok(()) |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
impl Ini { |
| 458 |
/// Load from a string |
| 459 |
pub fn load_from_str(buf: &str) -> Result<Ini, Error> { |
| 460 |
let mut parser = Parser::new(buf.chars(), false); |
| 461 |
parser.parse() |
| 462 |
} |
| 463 |
|
| 464 |
/// Load from a string, but do not interpret '\' as an escape character |
| 465 |
pub fn load_from_str_noescape(buf: &str) -> Result<Ini, Error> { |
| 466 |
let mut parser = Parser::new(buf.chars(), true); |
| 467 |
parser.parse() |
| 468 |
} |
| 469 |
|
| 470 |
/// Load from a reader |
| 471 |
pub fn read_from<R: Read>(reader: &mut R) -> Result<Ini, Error> { |
| 472 |
let mut s = String::new(); |
| 473 |
try!(reader.read_to_string(&mut s).map_err(|err| Error { |
| 474 |
line: 0, |
| 475 |
col: 0, |
| 476 |
msg: format!("{}", err), |
| 477 |
})); |
| 478 |
let mut parser = Parser::new(s.chars(), false); |
| 479 |
parser.parse() |
| 480 |
} |
| 481 |
|
| 482 |
/// Load from a reader, but do not interpret '\' as an escape character |
| 483 |
pub fn read_from_noescape<R: Read>(reader: &mut R) -> Result<Ini, Error> { |
| 484 |
let mut s = String::new(); |
| 485 |
try!(reader.read_to_string(&mut s).map_err(|err| Error { |
| 486 |
line: 0, |
| 487 |
col: 0, |
| 488 |
msg: format!("{}", err), |
| 489 |
})); |
| 490 |
let mut parser = Parser::new(s.chars(), true); |
| 491 |
parser.parse() |
| 492 |
} |
| 493 |
|
| 494 |
/// Load from a file |
| 495 |
pub fn load_from_file<P: AsRef<Path>>(filename: P) -> Result<Ini, Error> { |
| 496 |
let mut reader = match File::open(filename.as_ref()) { |
| 497 |
Err(e) => { |
| 498 |
return Err(Error { |
| 499 |
line: 0, |
| 500 |
col: 0, |
| 501 |
msg: format!("Unable to open `{:?}`: {}", filename.as_ref(), e), |
| 502 |
}) |
| 503 |
} |
| 504 |
Ok(r) => r, |
| 505 |
}; |
| 506 |
Ini::read_from(&mut reader) |
| 507 |
} |
| 508 |
|
| 509 |
/// Load from a file, but do not interpret '\' as an escape character |
| 510 |
pub fn load_from_file_noescape<P: AsRef<Path>>(filename: P) -> Result<Ini, Error> { |
| 511 |
let mut reader = match File::open(filename.as_ref()) { |
| 512 |
Err(e) => { |
| 513 |
return Err(Error { |
| 514 |
line: 0, |
| 515 |
col: 0, |
| 516 |
msg: format!("Unable to open `{:?}`: {}", filename.as_ref(), e), |
| 517 |
}) |
| 518 |
} |
| 519 |
Ok(r) => r, |
| 520 |
}; |
| 521 |
Ini::read_from_noescape(&mut reader) |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
/// Iterator for sections |
| 526 |
pub struct SectionIterator<'a> { |
| 527 |
mapiter: Iter<'a, Option<String>, Properties>, |
| 528 |
} |
| 529 |
|
| 530 |
/// Iterator for mutable sections |
| 531 |
pub struct SectionMutIterator<'a> { |
| 532 |
mapiter: IterMut<'a, Option<String>, Properties>, |
| 533 |
} |
| 534 |
|
| 535 |
impl<'a> Ini { |
| 536 |
/// Immutable iterate though sections |
| 537 |
pub fn iter(&'a self) -> SectionIterator<'a> { |
| 538 |
SectionIterator { |
| 539 |
mapiter: self.sections.iter(), |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
/// Mutable iterate though sections |
| 544 |
/// *Deprecated! Use `iter_mut` instead!* |
| 545 |
pub fn mut_iter(&'a mut self) -> SectionMutIterator<'a> { |
| 546 |
SectionMutIterator { |
| 547 |
mapiter: self.sections.iter_mut(), |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/// Mutable iterate though sections |
| 552 |
pub fn iter_mut(&'a mut self) -> SectionMutIterator<'a> { |
| 553 |
SectionMutIterator { |
| 554 |
mapiter: self.sections.iter_mut(), |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
impl<'a> Iterator for SectionIterator<'a> { |
| 560 |
type Item = (&'a Option<String>, &'a Properties); |
| 561 |
|
| 562 |
#[inline] |
| 563 |
fn next(&mut self) -> Option<(&'a Option<String>, &'a Properties)> { |
| 564 |
self.mapiter.next() |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
impl<'a> Iterator for SectionMutIterator<'a> { |
| 569 |
type Item = (&'a Option<String>, &'a mut Properties); |
| 570 |
|
| 571 |
#[inline] |
| 572 |
fn next(&mut self) -> Option<(&'a Option<String>, &'a mut Properties)> { |
| 573 |
self.mapiter.next() |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
impl<'a> IntoIterator for &'a Ini { |
| 578 |
type Item = (&'a Option<String>, &'a Properties); |
| 579 |
type IntoIter = SectionIterator<'a>; |
| 580 |
|
| 581 |
fn into_iter(self) -> SectionIterator<'a> { |
| 582 |
self.iter() |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
impl<'a> IntoIterator for &'a mut Ini { |
| 587 |
type Item = (&'a Option<String>, &'a mut Properties); |
| 588 |
type IntoIter = SectionMutIterator<'a>; |
| 589 |
|
| 590 |
fn into_iter(self) -> SectionMutIterator<'a> { |
| 591 |
self.iter_mut() |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
pub struct SectionIntoIter { |
| 596 |
iter: IntoIter<Option<String>, Properties>, |
| 597 |
} |
| 598 |
|
| 599 |
impl Iterator for SectionIntoIter { |
| 600 |
type Item = (Option<String>, Properties); |
| 601 |
fn next(&mut self) -> Option<Self::Item> { |
| 602 |
self.iter.next() |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
impl IntoIterator for Ini { |
| 607 |
type Item = (Option<String>, Properties); |
| 608 |
type IntoIter = SectionIntoIter; |
| 609 |
|
| 610 |
fn into_iter(self) -> SectionIntoIter { |
| 611 |
SectionIntoIter { |
| 612 |
iter: self.sections.into_iter(), |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
// Ini parser |
| 618 |
struct Parser<'a> { |
| 619 |
ch: Option<char>, |
| 620 |
rdr: Chars<'a>, |
| 621 |
line: usize, |
| 622 |
col: usize, |
| 623 |
literal: bool, |
| 624 |
} |
| 625 |
|
| 626 |
#[derive(Debug)] |
| 627 |
/// Parse error |
| 628 |
pub struct Error { |
| 629 |
pub line: usize, |
| 630 |
pub col: usize, |
| 631 |
pub msg: String, |
| 632 |
} |
| 633 |
|
| 634 |
impl Display for Error { |
| 635 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 636 |
write!(f, "{}:{} {}", self.line, self.col, self.msg) |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
impl error::Error for Error { |
| 641 |
fn description(&self) -> &str { |
| 642 |
self.msg.as_str() |
| 643 |
} |
| 644 |
|
| 645 |
fn cause(&self) -> Option<&error::Error> { |
| 646 |
None |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
impl<'a> Parser<'a> { |
| 651 |
// Create a parser |
| 652 |
pub fn new(rdr: Chars<'a>, literal: bool) -> Parser<'a> { |
| 653 |
let mut p = Parser { |
| 654 |
ch: None, |
| 655 |
line: 0, |
| 656 |
col: 0, |
| 657 |
rdr: rdr, |
| 658 |
literal: literal, |
| 659 |
}; |
| 660 |
p.bump(); |
| 661 |
p |
| 662 |
} |
| 663 |
|
| 664 |
fn eof(&self) -> bool { |
| 665 |
self.ch.is_none() |
| 666 |
} |
| 667 |
|
| 668 |
fn bump(&mut self) { |
| 669 |
self.ch = self.rdr.next(); |
| 670 |
match self.ch { |
| 671 |
Some('\n') => { |
| 672 |
self.line += 1; |
| 673 |
self.col = 0; |
| 674 |
} |
| 675 |
Some(..) => { |
| 676 |
self.col += 1; |
| 677 |
} |
| 678 |
None => {} |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
fn error<U>(&self, msg: String) -> Result<U, Error> { |
| 683 |
Err(Error { |
| 684 |
line: self.line, |
| 685 |
col: self.col, |
| 686 |
msg: msg.clone(), |
| 687 |
}) |
| 688 |
} |
| 689 |
|
| 690 |
/// Consume all the white space until the end of the line or a tab |
| 691 |
fn parse_whitespace(&mut self) { |
| 692 |
while let Some(c) = self.ch { |
| 693 |
if !c.is_whitespace() && c != '\n' && c != '\t' && c != '\r' { |
| 694 |
break; |
| 695 |
} |
| 696 |
self.bump(); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
/// Consume all the white space except line break |
| 701 |
fn parse_whitespace_except_line_break(&mut self) { |
| 702 |
while let Some(c) = self.ch { |
| 703 |
if (c == '\n' || c == '\r' || !c.is_whitespace()) && c != '\t' { |
| 704 |
break; |
| 705 |
} |
| 706 |
self.bump(); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
/// Parse the whole INI input |
| 711 |
pub fn parse(&mut self) -> Result<Ini, Error> { |
| 712 |
let mut result = Ini::new(); |
| 713 |
let mut curkey: String = "".into(); |
| 714 |
let mut cursec: Option<String> = None; |
| 715 |
|
| 716 |
self.parse_whitespace(); |
| 717 |
while let Some(cur_ch) = self.ch { |
| 718 |
match cur_ch { |
| 719 |
';' | '#' => { |
| 720 |
self.parse_comment(); |
| 721 |
} |
| 722 |
'[' => match self.parse_section() { |
| 723 |
Ok(sec) => { |
| 724 |
let msec = &sec[..].trim(); |
| 725 |
cursec = Some(msec.to_string()); |
| 726 |
result |
| 727 |
.sections |
| 728 |
.entry(cursec.clone()) |
| 729 |
.or_insert(HashMap::new()); |
| 730 |
self.bump(); |
| 731 |
} |
| 732 |
Err(e) => return Err(e), |
| 733 |
}, |
| 734 |
'=' | ':' => { |
| 735 |
if (&curkey[..]).is_empty() { |
| 736 |
return self.error("Missing key".to_string()); |
| 737 |
} |
| 738 |
match self.parse_val() { |
| 739 |
Ok(val) => { |
| 740 |
let mval = val[..].trim().to_owned(); |
| 741 |
let sec = result |
| 742 |
.sections |
| 743 |
.entry(cursec.clone()) |
| 744 |
.or_insert(HashMap::new()); |
| 745 |
sec.insert(curkey, mval); |
| 746 |
curkey = "".into(); |
| 747 |
} |
| 748 |
Err(e) => return Err(e), |
| 749 |
} |
| 750 |
} |
| 751 |
_ => match self.parse_key() { |
| 752 |
Ok(key) => { |
| 753 |
let mkey: String = key[..].trim().to_owned(); |
| 754 |
curkey = mkey.into(); |
| 755 |
} |
| 756 |
Err(e) => return Err(e), |
| 757 |
}, |
| 758 |
} |
| 759 |
|
| 760 |
self.parse_whitespace(); |
| 761 |
} |
| 762 |
|
| 763 |
Ok(result) |
| 764 |
} |
| 765 |
|
| 766 |
fn parse_comment(&mut self) { |
| 767 |
while let Some(c) = self.ch { |
| 768 |
self.bump(); |
| 769 |
if c == '\n' { |
| 770 |
break; |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
fn parse_str_until(&mut self, endpoint: &[Option<char>]) -> Result<String, Error> { |
| 776 |
let mut result: String = String::new(); |
| 777 |
|
| 778 |
while !endpoint.contains(&self.ch) { |
| 779 |
match self.ch { |
| 780 |
None => { |
| 781 |
return self.error(format!("Expecting \"{:?}\" but found EOF.", endpoint)); |
| 782 |
} |
| 783 |
Some('\\') if !self.literal => { |
| 784 |
self.bump(); |
| 785 |
if self.eof() { |
| 786 |
return self.error(format!("Expecting \"{:?}\" but found EOF.", endpoint)); |
| 787 |
} |
| 788 |
match self.ch.unwrap() { |
| 789 |
'0' => result.push('\0'), |
| 790 |
'a' => result.push('\x07'), |
| 791 |
'b' => result.push('\x08'), |
| 792 |
't' => result.push('\t'), |
| 793 |
'r' => result.push('\r'), |
| 794 |
'n' => result.push('\n'), |
| 795 |
'\n' => (), |
| 796 |
'x' => { |
| 797 |
// Unicode 4 character |
| 798 |
let mut code: String = String::with_capacity(4); |
| 799 |
for _ in 0..4 { |
| 800 |
self.bump(); |
| 801 |
if self.eof() { |
| 802 |
return self.error(format!( |
| 803 |
"Expecting \"{:?}\" but found EOF.", |
| 804 |
endpoint |
| 805 |
)); |
| 806 |
} else if let Some('\\') = self.ch { |
| 807 |
self.bump(); |
| 808 |
if self.ch != Some('\n') { |
| 809 |
return self.error(format!( |
| 810 |
"Expecting \"\\\\n\" but \ |
| 811 |
found \"{:?}\".", |
| 812 |
self.ch |
| 813 |
)); |
| 814 |
} |
| 815 |
} |
| 816 |
code.push(self.ch.unwrap()); |
| 817 |
} |
| 818 |
let r = u32::from_str_radix(&code[..], 16); |
| 819 |
match r { |
| 820 |
Ok(c) => result.push(char::from_u32(c).unwrap()), |
| 821 |
Err(_) => return self.error("Unknown character.".to_string()), |
| 822 |
} |
| 823 |
} |
| 824 |
c => result.push(c), |
| 825 |
} |
| 826 |
} |
| 827 |
Some(c) => { |
| 828 |
result.push(c); |
| 829 |
} |
| 830 |
} |
| 831 |
self.bump(); |
| 832 |
} |
| 833 |
Ok(result) |
| 834 |
} |
| 835 |
|
| 836 |
fn parse_section(&mut self) -> Result<String, Error> { |
| 837 |
// Skip [ |
| 838 |
self.bump(); |
| 839 |
self.parse_str_until(&[Some(']')]) |
| 840 |
} |
| 841 |
|
| 842 |
fn parse_key(&mut self) -> Result<String, Error> { |
| 843 |
self.parse_str_until(&[Some('='), Some(':')]) |
| 844 |
} |
| 845 |
|
| 846 |
fn parse_val(&mut self) -> Result<String, Error> { |
| 847 |
self.bump(); |
| 848 |
// Issue #35: Allow empty value |
| 849 |
self.parse_whitespace_except_line_break(); |
| 850 |
|
| 851 |
match self.ch { |
| 852 |
None => Ok(String::new()), |
| 853 |
Some('"') => { |
| 854 |
self.bump(); |
| 855 |
self.parse_str_until(&[Some('"')]).and_then(|s| { |
| 856 |
self.bump(); // Eats the last " |
| 857 |
Ok(s) |
| 858 |
}) |
| 859 |
} |
| 860 |
Some('\'') => { |
| 861 |
self.bump(); |
| 862 |
self.parse_str_until(&[Some('\'')]).and_then(|s| { |
| 863 |
self.bump(); // Eats the last ' |
| 864 |
Ok(s) |
| 865 |
}) |
| 866 |
} |
| 867 |
_ => self.parse_str_until(&[Some('\n'), Some('\r'), Some(';'), Some('#'), None]), |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
// ------------------------------------------------------------------------------ |
| 873 |
|
| 874 |
#[cfg(test)] |
| 875 |
mod test { |
| 876 |
use ini::*; |
| 877 |
|
| 878 |
#[test] |
| 879 |
fn load_from_str_with_valid_input() { |
| 880 |
let input = "[sec1]\nkey1=val1\nkey2=377\n[sec2]foo=bar\n"; |
| 881 |
let opt = Ini::load_from_str(input); |
| 882 |
assert!(opt.is_ok()); |
| 883 |
|
| 884 |
let output = opt.unwrap(); |
| 885 |
assert_eq!(output.sections.len(), 2); |
| 886 |
assert!(output.sections.contains_key(&Some("sec1".into()))); |
| 887 |
|
| 888 |
let sec1 = &output.sections[&Some("sec1".into())]; |
| 889 |
assert_eq!(sec1.len(), 2); |
| 890 |
let key1: String = "key1".into(); |
| 891 |
assert!(sec1.contains_key(&key1)); |
| 892 |
let key2: String = "key2".into(); |
| 893 |
assert!(sec1.contains_key(&key2)); |
| 894 |
let val1: String = "val1".into(); |
| 895 |
assert_eq!(sec1[&key1], val1); |
| 896 |
let val2: String = "377".into(); |
| 897 |
assert_eq!(sec1[&key2], val2); |
| 898 |
} |
| 899 |
|
| 900 |
#[test] |
| 901 |
fn load_from_str_without_ending_newline() { |
| 902 |
let input = "[sec1]\nkey1=val1\nkey2=377\n[sec2]foo=bar"; |
| 903 |
let opt = Ini::load_from_str(input); |
| 904 |
assert!(opt.is_ok()); |
| 905 |
} |
| 906 |
|
| 907 |
#[test] |
| 908 |
fn test_parse_comment() { |
| 909 |
let input = "; abcdefghijklmn\n"; |
| 910 |
let opt = Ini::load_from_str(input); |
| 911 |
assert!(opt.is_ok()); |
| 912 |
} |
| 913 |
|
| 914 |
#[test] |
| 915 |
fn test_inline_comment() { |
| 916 |
let input = " |
| 917 |
[section name] |
| 918 |
name = hello # abcdefg |
| 919 |
gender = mail ; abdddd |
| 920 |
"; |
| 921 |
let ini = Ini::load_from_str(input).unwrap(); |
| 922 |
assert_eq!(ini.get_from(Some("section name"), "name").unwrap(), "hello"); |
| 923 |
} |
| 924 |
|
| 925 |
#[test] |
| 926 |
fn test_sharp_comment() { |
| 927 |
let input = " |
| 928 |
[section name] |
| 929 |
name = hello |
| 930 |
# abcdefg |
| 931 |
"; |
| 932 |
let ini = Ini::load_from_str(input).unwrap(); |
| 933 |
assert_eq!(ini.get_from(Some("section name"), "name").unwrap(), "hello"); |
| 934 |
} |
| 935 |
|
| 936 |
#[test] |
| 937 |
fn test_iter() { |
| 938 |
let input = " |
| 939 |
[section name] |
| 940 |
name = hello # abcdefg |
| 941 |
gender = mail ; abdddd |
| 942 |
"; |
| 943 |
let mut ini = Ini::load_from_str(input).unwrap(); |
| 944 |
|
| 945 |
for (_, _) in &mut ini {} |
| 946 |
for (_, _) in &ini {} |
| 947 |
for (_, _) in ini {} |
| 948 |
} |
| 949 |
|
| 950 |
#[test] |
| 951 |
fn test_colon() { |
| 952 |
let input = " |
| 953 |
[section name] |
| 954 |
name: hello # abcdefg |
| 955 |
gender : mail ; abdddd |
| 956 |
"; |
| 957 |
let ini = Ini::load_from_str(input).unwrap(); |
| 958 |
assert_eq!(ini.get_from(Some("section name"), "name").unwrap(), "hello"); |
| 959 |
assert_eq!( |
| 960 |
ini.get_from(Some("section name"), "gender").unwrap(), |
| 961 |
"mail" |
| 962 |
); |
| 963 |
} |
| 964 |
|
| 965 |
#[test] |
| 966 |
fn test_string() { |
| 967 |
let input = " |
| 968 |
[section name] |
| 969 |
# This is a comment |
| 970 |
Key = \"Value\" |
| 971 |
"; |
| 972 |
let ini = Ini::load_from_str(input).unwrap(); |
| 973 |
assert_eq!(ini.get_from(Some("section name"), "Key").unwrap(), "Value"); |
| 974 |
} |
| 975 |
|
| 976 |
#[test] |
| 977 |
fn test_string_multiline() { |
| 978 |
let input = " |
| 979 |
[section name] |
| 980 |
# This is a comment |
| 981 |
Key = \"Value |
| 982 |
Otherline\" |
| 983 |
"; |
| 984 |
let ini = Ini::load_from_str(input).unwrap(); |
| 985 |
assert_eq!( |
| 986 |
ini.get_from(Some("section name"), "Key").unwrap(), |
| 987 |
"Value\nOtherline" |
| 988 |
); |
| 989 |
} |
| 990 |
|
| 991 |
#[test] |
| 992 |
fn test_string_comment() { |
| 993 |
let input = " |
| 994 |
[section name] |
| 995 |
# This is a comment |
| 996 |
Key = \"Value # This is not a comment ; at all\" |
| 997 |
Stuff = Other |
| 998 |
"; |
| 999 |
let ini = Ini::load_from_str(input).unwrap(); |
| 1000 |
assert_eq!( |
| 1001 |
ini.get_from(Some("section name"), "Key").unwrap(), |
| 1002 |
"Value # This is not a comment ; at all" |
| 1003 |
); |
| 1004 |
} |
| 1005 |
|
| 1006 |
#[test] |
| 1007 |
fn test_string_single() { |
| 1008 |
let input = " |
| 1009 |
[section name] |
| 1010 |
# This is a comment |
| 1011 |
Key = 'Value' |
| 1012 |
Stuff = Other |
| 1013 |
"; |
| 1014 |
let ini = Ini::load_from_str(input).unwrap(); |
| 1015 |
assert_eq!(ini.get_from(Some("section name"), "Key").unwrap(), "Value"); |
| 1016 |
} |
| 1017 |
|
| 1018 |
#[test] |
| 1019 |
fn test_string_includes_quote() { |
| 1020 |
let input = " |
| 1021 |
[Test] |
| 1022 |
Comment[tr]=İnternet'e erişin |
| 1023 |
Comment[uk]=Доступ до Інтернету |
| 1024 |
"; |
| 1025 |
let ini = Ini::load_from_str(input).unwrap(); |
| 1026 |
assert_eq!( |
| 1027 |
ini.get_from(Some("Test"), "Comment[tr]").unwrap(), |
| 1028 |
"İnternet'e erişin" |
| 1029 |
); |
| 1030 |
} |
| 1031 |
|
| 1032 |
#[test] |
| 1033 |
fn test_string_single_multiline() { |
| 1034 |
let input = " |
| 1035 |
[section name] |
| 1036 |
# This is a comment |
| 1037 |
Key = 'Value |
| 1038 |
Otherline' |
| 1039 |
Stuff = Other |
| 1040 |
"; |
| 1041 |
let ini = Ini::load_from_str(input).unwrap(); |
| 1042 |
assert_eq!( |
| 1043 |
ini.get_from(Some("section name"), "Key").unwrap(), |
| 1044 |
"Value\nOtherline" |
| 1045 |
); |
| 1046 |
} |
| 1047 |
|
| 1048 |
#[test] |
| 1049 |
fn test_string_single_comment() { |
| 1050 |
let input = " |
| 1051 |
[section name] |
| 1052 |
# This is a comment |
| 1053 |
Key = 'Value # This is not a comment ; at all' |
| 1054 |
"; |
| 1055 |
let ini = Ini::load_from_str(input).unwrap(); |
| 1056 |
assert_eq!( |
| 1057 |
ini.get_from(Some("section name"), "Key").unwrap(), |
| 1058 |
"Value # This is not a comment ; at all" |
| 1059 |
); |
| 1060 |
} |
| 1061 |
|
| 1062 |
#[test] |
| 1063 |
fn load_from_str_with_valid_empty_input() { |
| 1064 |
let input = "key1=\nkey2=val2\n"; |
| 1065 |
let opt = Ini::load_from_str(input); |
| 1066 |
assert!(opt.is_ok()); |
| 1067 |
|
| 1068 |
let output = opt.unwrap(); |
| 1069 |
assert_eq!(output.sections.len(), 1); |
| 1070 |
assert!(output.sections.contains_key(&None::<String>)); |
| 1071 |
|
| 1072 |
let sec1 = &output.sections[&None::<String>]; |
| 1073 |
assert_eq!(sec1.len(), 2); |
| 1074 |
let key1: String = "key1".into(); |
| 1075 |
assert!(sec1.contains_key(&key1)); |
| 1076 |
let key2: String = "key2".into(); |
| 1077 |
assert!(sec1.contains_key(&key2)); |
| 1078 |
let val1: String = "".into(); |
| 1079 |
assert_eq!(sec1[&key1], val1); |
| 1080 |
let val2: String = "val2".into(); |
| 1081 |
assert_eq!(sec1[&key2], val2); |
| 1082 |
} |
| 1083 |
|
| 1084 |
#[test] |
| 1085 |
fn load_from_str_with_crlf() { |
| 1086 |
let input = "key1=val1\r\nkey2=val2\r\n"; |
| 1087 |
let opt = Ini::load_from_str(input); |
| 1088 |
assert!(opt.is_ok()); |
| 1089 |
|
| 1090 |
let output = opt.unwrap(); |
| 1091 |
assert_eq!(output.sections.len(), 1); |
| 1092 |
assert!(output.sections.contains_key(&None::<String>)); |
| 1093 |
let sec1 = &output.sections[&None::<String>]; |
| 1094 |
assert_eq!(sec1.len(), 2); |
| 1095 |
let key1: String = "key1".into(); |
| 1096 |
assert!(sec1.contains_key(&key1)); |
| 1097 |
let key2: String = "key2".into(); |
| 1098 |
assert!(sec1.contains_key(&key2)); |
| 1099 |
let val1: String = "val1".into(); |
| 1100 |
assert_eq!(sec1[&key1], val1); |
| 1101 |
let val2: String = "val2".into(); |
| 1102 |
assert_eq!(sec1[&key2], val2); |
| 1103 |
} |
| 1104 |
|
| 1105 |
#[test] |
| 1106 |
fn load_from_str_with_cr() { |
| 1107 |
let input = "key1=val1\rkey2=val2\r"; |
| 1108 |
let opt = Ini::load_from_str(input); |
| 1109 |
assert!(opt.is_ok()); |
| 1110 |
|
| 1111 |
let output = opt.unwrap(); |
| 1112 |
assert_eq!(output.sections.len(), 1); |
| 1113 |
assert!(output.sections.contains_key(&None::<String>)); |
| 1114 |
let sec1 = &output.sections[&None::<String>]; |
| 1115 |
assert_eq!(sec1.len(), 2); |
| 1116 |
let key1: String = "key1".into(); |
| 1117 |
assert!(sec1.contains_key(&key1)); |
| 1118 |
let key2: String = "key2".into(); |
| 1119 |
assert!(sec1.contains_key(&key2)); |
| 1120 |
let val1: String = "val1".into(); |
| 1121 |
assert_eq!(sec1[&key1], val1); |
| 1122 |
let val2: String = "val2".into(); |
| 1123 |
assert_eq!(sec1[&key2], val2); |
| 1124 |
} |
| 1125 |
|
| 1126 |
#[test] |
| 1127 |
fn get_with_non_static_key() { |
| 1128 |
let input = "key1=val1\nkey2=val2\n"; |
| 1129 |
let opt = Ini::load_from_str(input).unwrap(); |
| 1130 |
|
| 1131 |
let sec1 = &opt.sections[&None::<String>]; |
| 1132 |
|
| 1133 |
let key = "key1".to_owned(); |
| 1134 |
sec1.get(&key).unwrap(); |
| 1135 |
} |
| 1136 |
|
| 1137 |
#[test] |
| 1138 |
fn load_from_str_noescape() { |
| 1139 |
let input = "path=C:\\Windows\\Some\\Folder\\"; |
| 1140 |
let opt = Ini::load_from_str_noescape(input); |
| 1141 |
assert!(opt.is_ok()); |
| 1142 |
|
| 1143 |
let output = opt.unwrap(); |
| 1144 |
assert_eq!(output.sections.len(), 1); |
| 1145 |
let sec = &output.sections[&None::<String>]; |
| 1146 |
assert_eq!(sec.len(), 1); |
| 1147 |
assert!(sec.contains_key("path")); |
| 1148 |
assert_eq!(sec["path"], "C:\\Windows\\Some\\Folder\\"); |
| 1149 |
} |
| 1150 |
} |