Crates.io | ini_puga |
lib.rs | ini_puga |
version | 0.2.2 |
source | src |
created_at | 2023-02-26 17:30:17.450721 |
updated_at | 2023-04-20 13:11:43.507635 |
description | A library to manage INI files. |
homepage | http://puga.dev |
repository | |
max_upload_size | |
id | 795360 |
size | 14,849 |
ini_puga
handles ini files in an easy way, without dealing with errors(1)
and thanks to generic types, only 2 methods get
and get_vector
are enought to
get an integer, float, boolen, and so on.
There are 2 more methods get_from_hex
and get_vector_from_hex
to convert hex
numbers from strings to integer types.
(1) Load
is the only one that may returns a Result IO:Error.
use ini_puga::Ini;
const DEFAULT_CONFIG: &str = r#"
my value without section = true
pi=3.14159
[config]
theme = Dark
text scale = 1.2
background color = faee89
font color =
favourites colors = f53298,a0ffff,fa0e78
empty value =
screen width = 1080
resolutions = 640,480,3.5,whatever,720,1080
resolutions =
use plugins = true
themes path = themes/
plugins path = plugins/
[user data]
name = José Puga
id = 700101
fav lang = Rust
fav pet = Rustacean
"#;
let mut ini = Ini::new();
ini.read(DEFAULT_CONFIG.to_string());
// Of course, you can also load from a file.
// ini.load("config.ini").expect("***ERROR OPENING FILE ***");
assert_eq!(ini.get::<f32>("config", "text scale", 0.0), 1.2);
assert_eq!(ini.get::<i64>("user data", "id", 999), 700101);
assert_eq!(ini.get::<i64>("user data", "id2", 999), 999);
//Vectors. Non valid values are inserted in the vector as default value
let v = ini.get_vector::<i32>("config", "resolutions", 240, ',');
assert_eq!(v[2], 240);
assert_eq!(v[5], 1080);
//Prints [640,480,240,240,720,1080]
println!("{:?}", v);
let no_name: String = String::from("anonymous");
assert_ne!(
ini.get::<String>("user data", "name", no_name), "anonymous");
// Check Hex numbers
assert_eq!(ini.get_from_hex::<u32>("config", "background color", 0), 0xfaee89);
//Empty or erroneus values always gets default.
assert_eq!(ini.get_from_hex::<u32>("config", "font color", 25), 25);
let vh = ini.get_vector_from_hex::<u32>("config", "favourites colors", 0xffffff, ',');
assert_eq!(vh[1], 0xa0ffff);
assert_eq!(vh[2], 0xfa0e78);
//Prints [16069272,10551295,16387704]
println!("{:?}", vh);
// Check sections and keys
assert_eq!(ini.section_exists("config"), true);
assert_eq!(ini.key_exists("NONEXISTENT section", "foo"), false);
let sn = ini.get_sections_names();
//len +1 because empty section always exists
assert_eq!(sn.len(), 3);
//Prints (random order) ["config", "", "user data"]
println!("{:#?}", sn);
let ks = ini.get_keys_from_section("");
assert_eq!(ks.len(), 2);
//Prints (random order) ["my value without section", "pi"]
println!("{:#?}", ks);
// Case sensitive
assert_eq!(ini.key_exists("", "pi"), true);
assert_eq!(ini.key_exists("", "PI"), false);
// Print trait implemented
println!("{}", ini);
License: MIT