|
44 | 44 |
|
45 | 45 | use std::{ |
46 | 46 | borrow::Cow, |
47 | | - char, |
48 | | - error, |
| 47 | + char, error, |
49 | 48 | fmt::{self, Display}, |
50 | 49 | fs::{File, OpenOptions}, |
51 | 50 | io::{self, Read, Seek, SeekFrom, Write}, |
@@ -300,53 +299,79 @@ impl Default for WriteOption { |
300 | 299 | } |
301 | 300 | } |
302 | 301 |
|
| 302 | +#[cfg(all(feature = "case-insensitive", not(feature = "nested-sections")))] |
| 303 | +pub type SectionKey = Option<UniCase<String>>; |
| 304 | +#[cfg(all(feature = "case-insensitive", feature = "nested-sections"))] |
| 305 | +pub type SectionKey = Option<Vec<UniCase<String>>>; |
| 306 | +#[cfg(all(not(feature = "case-insensitive"), not(feature = "nested-sections")))] |
| 307 | +pub type SectionKey = Option<String>; |
| 308 | +#[cfg(all(not(feature = "case-insensitive"), feature = "nested-sections"))] |
| 309 | +pub type SectionKey = Option<Vec<String>>; |
| 310 | + |
303 | 311 | cfg_if! { |
304 | 312 | if #[cfg(feature = "case-insensitive")] { |
305 | | - /// Internal storage of section's key |
306 | | - pub type SectionKey = Option<UniCase<String>>; |
307 | | - /// Internal storage of property's key |
308 | 313 | pub type PropertyKey = UniCase<String>; |
309 | | - |
310 | 314 | macro_rules! property_get_key { |
311 | 315 | ($s:expr) => { |
312 | 316 | &UniCase::from($s) |
313 | 317 | }; |
314 | 318 | } |
315 | | - |
316 | 319 | macro_rules! property_insert_key { |
317 | 320 | ($s:expr) => { |
318 | 321 | UniCase::from($s) |
319 | 322 | }; |
320 | 323 | } |
321 | | - |
322 | | - macro_rules! section_key { |
323 | | - ($s:expr) => { |
324 | | - $s.map(|s| UniCase::from(s.into())) |
325 | | - }; |
326 | | - } |
327 | | - |
328 | 324 | } else { |
329 | | - /// Internal storage of section's key |
330 | | - pub type SectionKey = Option<String>; |
331 | | - /// Internal storage of property's key |
332 | 325 | pub type PropertyKey = String; |
333 | | - |
334 | 326 | macro_rules! property_get_key { |
335 | 327 | ($s:expr) => { |
336 | 328 | $s |
337 | 329 | }; |
338 | 330 | } |
339 | | - |
340 | 331 | macro_rules! property_insert_key { |
341 | 332 | ($s:expr) => { |
342 | 333 | $s |
343 | 334 | }; |
344 | 335 | } |
| 336 | + } |
| 337 | +} |
345 | 338 |
|
346 | | - macro_rules! section_key { |
347 | | - ($s:expr) => { |
348 | | - $s.map(Into::into) |
349 | | - }; |
| 339 | +cfg_if! { |
| 340 | + if #[cfg(feature = "case-insensitive")] { |
| 341 | + cfg_if! { |
| 342 | + if #[cfg(feature = "nested-sections")] { |
| 343 | + macro_rules! section_key { |
| 344 | + ($s:expr) => { |
| 345 | + $s.map(|s| { |
| 346 | + s.split("][") |
| 347 | + .map(|part| UniCase::from(part.to_string())) |
| 348 | + .collect::<Vec<_>>() |
| 349 | + }) |
| 350 | + }; |
| 351 | + } |
| 352 | + } else { |
| 353 | + macro_rules! section_key { |
| 354 | + ($s:expr) => { |
| 355 | + $s.map(|s| UniCase::from(s.into())) |
| 356 | + }; |
| 357 | + } |
| 358 | + } |
| 359 | + } |
| 360 | + } else { |
| 361 | + cfg_if! { |
| 362 | + if #[cfg(feature = "nested-sections")] { |
| 363 | + macro_rules! section_key { |
| 364 | + ($s:expr) => { |
| 365 | + $s.map(|s| s.split("][").map(Into::into).collect::<Vec<_>>()) |
| 366 | + }; |
| 367 | + } |
| 368 | + } else { |
| 369 | + macro_rules! section_key { |
| 370 | + ($s:expr) => { |
| 371 | + $s.map(Into::into) |
| 372 | + }; |
| 373 | + } |
| 374 | + } |
350 | 375 | } |
351 | 376 | } |
352 | 377 | } |
@@ -764,8 +789,19 @@ impl Ini { |
764 | 789 | } |
765 | 790 |
|
766 | 791 | /// Iterate with sections |
767 | | - pub fn sections(&self) -> impl DoubleEndedIterator<Item = Option<&str>> { |
768 | | - self.sections.keys().map(|s| s.as_ref().map(AsRef::as_ref)) |
| 792 | + pub fn sections(&self) -> impl DoubleEndedIterator<Item = Option<String>> + '_ { |
| 793 | + self.sections.keys().map(|key| { |
| 794 | + key.as_ref().map(|parts| { |
| 795 | + #[cfg(feature = "nested-sections")] |
| 796 | + { |
| 797 | + parts.join("][") |
| 798 | + } |
| 799 | + #[cfg(not(feature = "nested-sections"))] |
| 800 | + { |
| 801 | + parts.clone() |
| 802 | + } |
| 803 | + }) |
| 804 | + }) |
769 | 805 | } |
770 | 806 |
|
771 | 807 | /// Set key-value to a section |
@@ -959,12 +995,11 @@ impl Ini { |
959 | 995 | } |
960 | 996 |
|
961 | 997 | if let Some(ref section) = *section { |
962 | | - write!( |
963 | | - writer, |
964 | | - "[{}]{}", |
965 | | - escape_str(§ion[..], opt.escape_policy), |
966 | | - opt.line_separator |
967 | | - )?; |
| 998 | + #[cfg(feature = "nested-sections")] |
| 999 | + let section_str = format!("[{}]", parts.join("][")); |
| 1000 | + #[cfg(not(feature = "nested-sections"))] |
| 1001 | + let section_str = format!("[{}]", escape_str(section, opt.escape_policy)); |
| 1002 | + write!(writer, "{}{}", section_str, opt.line_separator)?; |
968 | 1003 | } |
969 | 1004 | for (k, v) in props.iter() { |
970 | 1005 | let k_str = escape_str(k, opt.escape_policy); |
|
0 commit comments