use std::borrow::Cow;
use bstr::BStr;
use crate::file::Metadata;
use crate::{value, AsKey, File};
impl File<'_> {
pub fn string(&self, key: impl AsKey) -> Option<Cow<'_, BStr>> {
self.string_filter(key, |_| true)
}
pub fn string_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<Cow<'_, BStr>> {
self.string_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), |_| true)
}
pub fn string_filter(&self, key: impl AsKey, filter: impl FnMut(&Metadata) -> bool) -> Option<Cow<'_, BStr>> {
let key = key.try_as_key()?;
self.raw_value_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
.ok()
}
pub fn string_filter_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Cow<'_, BStr>> {
self.raw_value_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
.ok()
}
pub fn path(&self, key: impl AsKey) -> Option<crate::Path<'_>> {
self.path_filter(key, |_| true)
}
pub fn path_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<crate::Path<'_>> {
self.path_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), |_| true)
}
pub fn path_filter(&self, key: impl AsKey, filter: impl FnMut(&Metadata) -> bool) -> Option<crate::Path<'_>> {
let key = key.try_as_key()?;
self.path_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
}
pub fn path_filter_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<crate::Path<'_>> {
self.raw_value_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
.ok()
.map(crate::Path::from)
}
pub fn boolean(&self, key: impl AsKey) -> Option<Result<bool, value::Error>> {
self.boolean_filter(key, |_| true)
}
pub fn boolean_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<Result<bool, value::Error>> {
self.boolean_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), |_| true)
}
pub fn boolean_filter(
&self,
key: impl AsKey,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<bool, value::Error>> {
let key = key.try_as_key()?;
self.boolean_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
}
pub fn boolean_filter_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
mut filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<bool, value::Error>> {
let section_name = section_name.as_ref();
let section_ids = self
.section_ids_by_name_and_subname(section_name, subsection_name)
.ok()?;
let key = value_name.as_ref();
for section_id in section_ids.rev() {
let section = self.sections.get(§ion_id).expect("known section id");
if !filter(section.meta()) {
continue;
}
match section.value_implicit(key) {
Some(Some(v)) => return Some(crate::Boolean::try_from(v).map(Into::into)),
Some(None) => return Some(Ok(true)),
None => continue,
}
}
None
}
pub fn integer(&self, key: impl AsKey) -> Option<Result<i64, value::Error>> {
self.integer_filter(key, |_| true)
}
pub fn integer_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<Result<i64, value::Error>> {
self.integer_filter_by(section_name, subsection_name, value_name, |_| true)
}
pub fn integer_filter(
&self,
key: impl AsKey,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<i64, value::Error>> {
let key = key.try_as_key()?;
self.integer_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
}
pub fn integer_filter_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<i64, value::Error>> {
let int = self
.raw_value_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
.ok()?;
Some(crate::Integer::try_from(int.as_ref()).and_then(|b| {
b.to_decimal()
.ok_or_else(|| value::Error::new("Integer overflow", int.into_owned()))
}))
}
pub fn strings(&self, key: impl AsKey) -> Option<Vec<Cow<'_, BStr>>> {
let key = key.try_as_key()?;
self.strings_by(key.section_name, key.subsection_name, key.value_name)
}
pub fn strings_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<Vec<Cow<'_, BStr>>> {
self.raw_values_by(section_name.as_ref(), subsection_name, value_name.as_ref())
.ok()
}
pub fn strings_filter(&self, key: impl AsKey, filter: impl FnMut(&Metadata) -> bool) -> Option<Vec<Cow<'_, BStr>>> {
let key = key.try_as_key()?;
self.strings_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
}
pub fn strings_filter_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Vec<Cow<'_, BStr>>> {
self.raw_values_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
.ok()
}
pub fn integers(&self, key: impl AsKey) -> Option<Result<Vec<i64>, value::Error>> {
self.integers_filter(key, |_| true)
}
pub fn integers_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
) -> Option<Result<Vec<i64>, value::Error>> {
self.integers_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), |_| true)
}
pub fn integers_filter(
&self,
key: impl AsKey,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<Vec<i64>, value::Error>> {
let key = key.try_as_key()?;
self.integers_filter_by(key.section_name, key.subsection_name, key.value_name, filter)
}
pub fn integers_filter_by(
&self,
section_name: impl AsRef<str>,
subsection_name: Option<&BStr>,
value_name: impl AsRef<str>,
filter: impl FnMut(&Metadata) -> bool,
) -> Option<Result<Vec<i64>, value::Error>> {
self.raw_values_filter_by(section_name.as_ref(), subsection_name, value_name.as_ref(), filter)
.ok()
.map(|values| {
values
.into_iter()
.map(|v| {
crate::Integer::try_from(v.as_ref()).and_then(|int| {
int.to_decimal()
.ok_or_else(|| value::Error::new("Integer overflow", v.into_owned()))
})
})
.collect()
})
}
}