use std::borrow::Cow;
use crate::{ArgError, Reader, Result, SetFromRead, reader::ReadFmt};
pub enum ParseFArg<'a, 'f> {
Str(Cow<'a, str>),
Arg(&'a mut dyn SetFromRead, &'f ReadFmt<'f>),
}
pub fn parsef<'a, 'f>(
r: &mut Reader,
args: impl AsMut<[ParseFArg<'a, 'f>]>,
) -> Result<()> {
let res = parsef_part(r, args)?;
if r.peek()?.is_none() {
Ok(())
} else {
Err(res.unwrap_or_else(|| r.err_parse("Unused input")))
}
}
pub fn parsef_part<'a, 'f>(
r: &mut Reader,
mut args: impl AsMut<[ParseFArg<'a, 'f>]>,
) -> Result<Option<ArgError>> {
let mut last_err = None;
for a in args.as_mut() {
last_err = match a {
ParseFArg::Arg(a, fmt) => a.set_from_read(r, fmt)?,
ParseFArg::Str(a) => {
r.expect(a)?;
None
}
};
}
Ok(last_err)
}