use std::borrow::ToOwned;
use ini::Properties;
use crate::{
common::UniqueId,
error::{AddContext, ErrorKind},
parse::FromUtf8,
Error,
};
pub fn parse_string(sec: &Properties, key: &str) -> String {
sec.get(key).map(ToOwned::to_owned).unwrap_or_default()
}
pub fn parse_int(sec: &Properties, key: &str) -> i32 {
sec.get(key)
.and_then(|v| v.parse().ok())
.unwrap_or_default()
}
pub fn parse_bool(sec: &Properties, key: &str) -> bool {
sec.get(key).map(|v| v == "1").unwrap_or_default()
}
pub fn parse_unique_id(sec: &Properties, key: &str) -> Result<UniqueId, Error> {
sec.get(key)
.ok_or_else(|| ErrorKind::MissingSection(key.to_owned()))
.and_then(|v| UniqueId::from_utf8(v.as_bytes()))
.map_err(|e| e.context("parse_unique_id"))
}