|
| 1 | +# INI in Rust |
| 2 | + |
| 3 | +[](https://github.com/zonyitoo/rust-ini/actions/workflows/build-and-test.yml) |
| 4 | +[](https://crates.io/crates/rust-ini) |
| 5 | +[](https://docs.rs/rust-ini) |
| 6 | + |
| 7 | +[INI](http://en.wikipedia.org/wiki/INI_file) is an informal standard for configuration files for some platforms or software. INI files are simple text files with a basic structure composed of "sections" and "properties". |
| 8 | + |
| 9 | +This is an INI file parser in [Rust](http://www.rust-lang.org/). |
| 10 | + |
| 11 | +```toml |
| 12 | +[dependencies] |
| 13 | +rust-ini = "0.19" |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +* Create a Ini configuration file. |
| 19 | + |
| 20 | +```rust |
| 21 | +extern crate ini; |
| 22 | +use ini::Ini; |
| 23 | + |
| 24 | +fn main() { |
| 25 | + let mut conf = Ini::new(); |
| 26 | + conf.with_section(None::<String>) |
| 27 | + .set("encoding", "utf-8"); |
| 28 | + conf.with_section(Some("User")) |
| 29 | + .set("given_name", "Tommy") |
| 30 | + .set("family_name", "Green") |
| 31 | + .set("unicode", "Raspberry树莓"); |
| 32 | + conf.with_section(Some("Book")) |
| 33 | + .set("name", "Rust cool"); |
| 34 | + conf.write_to_file("conf.ini").unwrap(); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Then you will get `conf.ini` |
| 39 | + |
| 40 | +```ini |
| 41 | +encoding=utf-8 |
| 42 | + |
| 43 | +[User] |
| 44 | +given_name=Tommy |
| 45 | +family_name=Green |
| 46 | +unicode=Raspberry\x6811\x8393 |
| 47 | + |
| 48 | +[Book] |
| 49 | +name=Rust cool |
| 50 | +``` |
| 51 | + |
| 52 | +* Read from file `conf.ini` |
| 53 | + |
| 54 | +```rust |
| 55 | +use ini::Ini; |
| 56 | + |
| 57 | +fn main() { |
| 58 | + let conf = Ini::load_from_file("conf.ini").unwrap(); |
| 59 | + |
| 60 | + let section = conf.section(Some("User")).unwrap(); |
| 61 | + let tommy = section.get("given_name").unwrap(); |
| 62 | + let green = section.get("family_name").unwrap(); |
| 63 | + |
| 64 | + println!("{:?} {:?}", tommy, green); |
| 65 | + |
| 66 | + // iterating |
| 67 | + for (sec, prop) in &conf { |
| 68 | + println!("Section: {:?}", sec); |
| 69 | + for (key, value) in prop.iter() { |
| 70 | + println!("{:?}:{:?}", key, value); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +* More details could be found in `examples`. |
| 77 | + |
| 78 | +## License |
| 79 | + |
| 80 | +[The MIT License (MIT)](https://opensource.org/licenses/MIT) |
| 81 | + |
| 82 | +Copyright (c) 2014 Y. T. CHUNG |
| 83 | + |
| 84 | +Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 85 | +this software and associated documentation files (the "Software"), to deal in |
| 86 | +the Software without restriction, including without limitation the rights to |
| 87 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 88 | +the Software, and to permit persons to whom the Software is furnished to do so, |
| 89 | +subject to the following conditions: |
| 90 | + |
| 91 | +The above copyright notice and this permission notice shall be included in all |
| 92 | +copies or substantial portions of the Software. |
| 93 | + |
| 94 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 95 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 96 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 97 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 98 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 99 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments