zonyitoo / rust-ini

INI file parser in Rust
MIT License
305 stars 79 forks source link

Ini method general_section() panics #101

Closed initinll closed 1 year ago

initinll commented 1 year ago

Describe the bug

The Ini method general_section panics when there is no general section in *.ini file.

pub fn general_section(&self) -> &Properties

Step to reproduce -

sample.ini

[normal]
sound-file=/usr/share/sounds/freedesktop/stereo/dialog-information.oga

[critical]
border-color=FAB387ff
default-timeout=20
sound-file=/usr/share/sounds/freedesktop/stereo/dialog-warning.oga

main.rs

use ini::{Ini, Error};

fn main() {    

    let conf: Result<Ini, Error> = Ini::load_from_file(r"C:\Projects\test\sample.ini");

    if let Ok(ini) = conf {

        let properties = ini.general_section();

        if !properties.is_empty() {
            for property in properties.iter() {
                println!("{} {}", property.0, property.1);
            }
        }
    }
}

output

C:\Projects\rust-ini-test>cargo run
   Compiling concepts v0.1.0 (C:\Projects\rust-ini-test)
    Finished dev [unoptimized + debuginfo] target(s) in 2.97s
     Running `target\debug\rust-ini-test.exe`
thread 'main' panicked at 'There is no general section in this Ini', C:\Users\test\.cargo\registry\src\github.com-1ecc6299db9ec823\rust-ini-0.18.0\src\lib.rs:530:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\rust-ini-test.exe` (exit code: 101)

Possible Fix

The Ini method general_section should return Option of Properties

pub fn general_section(&self) -> Option<&Properties>
zonyitoo commented 1 year ago

Or maybe we could add an empty General Section when parsing from file, which extends this commit:

https://github.com/zonyitoo/rust-ini/commit/1cbfb75b9e2209b9887e90f1d44fae004b3df2b6

initinll commented 1 year ago

Hi @zonyitoo,

Yes, this is already fixed by commit - https://github.com/zonyitoo/rust-ini/commit/1cbfb75b9e2209b9887e90f1d44fae004b3df2b6 , can't reproduce this issue on forked repository.

Closing this issue 👍