Parse interface changes

This commit is contained in:
soup 2024-05-07 22:30:51 -04:00 committed by code
parent 7e44e93125
commit d5ccf62481

View file

@ -123,6 +123,23 @@ pub mod buf {
} }
} }
impl<'a, R, T> Push<T> for &'a mut R
where
R: Push<T>,
{
fn push_checked(&mut self, item: T) -> Result<(), OutOfSpace> {
(**self).push_checked(item)
}
fn push(&mut self, item: T) {
(**self).push(item)
}
unsafe fn push_unchecked(&mut self, item: T) {
(**self).push(item)
}
}
pub struct OutOfSpace; pub struct OutOfSpace;
pub trait Push<T> { pub trait Push<T> {
fn push_checked(&mut self, item: T) -> Result<(), OutOfSpace> { fn push_checked(&mut self, item: T) -> Result<(), OutOfSpace> {
@ -332,12 +349,13 @@ pub mod cli {
'b, 'b,
A: AsRef<str>, A: AsRef<str>,
B: From<&'b str>, B: From<&'b str>,
P: Push<ParseError>, PB: Push<B>,
PE: Push<ParseError>,
>( >(
&mut self, &mut self,
args: &'b [A], args: &'b [A],
mut out: Option<&mut Vec<B>>, mut out: PB,
mut errors: Option<&mut P>, mut errors: PE,
) { ) {
let mut it = args.iter(); let mut it = args.iter();
fn next_param<'a, A: AsRef<str> + 'a>( fn next_param<'a, A: AsRef<str> + 'a>(
@ -400,24 +418,21 @@ pub mod cli {
}, },
} }
} else { } else {
if let Some(out) = out.as_mut() { out.push(arg.as_ref().into());
out.push(arg.as_ref().into());
}
Ok(()) Ok(())
} }
}; };
if let (Some(err), Some(errors)) = (go().err(), errors.as_mut()) if let Some(err) = go().err() {
{
errors.push(err); errors.push(err);
} }
} }
} }
pub fn parse_env<P: Push<ParseError>>( pub fn parse_env<PS: Push<String>, PE: Push<ParseError>>(
&mut self, &mut self,
out: Option<&mut Vec<String>>, out: PS,
errors: Option<&mut P>, errors: PE,
) { ) {
let env_args = std::env::args().collect::<Vec<_>>(); let env_args = std::env::args().collect::<Vec<_>>();
self.parse(env_args.as_slice(), out, errors); self.parse(env_args.as_slice(), out, errors);
@ -514,8 +529,8 @@ pub mod cli {
let mut errors = Buf::new(1); let mut errors = Buf::new(1);
cli.parse( cli.parse(
&["--foo", "foo", "--bar", "854", "-b", "arg"], &["--foo", "foo", "--bar", "854", "-b", "arg"],
Some(&mut args), &mut args,
Some(&mut errors), &mut errors,
); );
assert_eq!(foo, "foo"); assert_eq!(foo, "foo");
assert_eq!(bar, 854); assert_eq!(bar, 854);