Parse interface changes
This commit is contained in:
parent
7e44e93125
commit
d5ccf62481
41
src/lib.rs
41
src/lib.rs
|
|
@ -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 trait Push<T> {
|
||||
fn push_checked(&mut self, item: T) -> Result<(), OutOfSpace> {
|
||||
|
|
@ -332,12 +349,13 @@ pub mod cli {
|
|||
'b,
|
||||
A: AsRef<str>,
|
||||
B: From<&'b str>,
|
||||
P: Push<ParseError>,
|
||||
PB: Push<B>,
|
||||
PE: Push<ParseError>,
|
||||
>(
|
||||
&mut self,
|
||||
args: &'b [A],
|
||||
mut out: Option<&mut Vec<B>>,
|
||||
mut errors: Option<&mut P>,
|
||||
mut out: PB,
|
||||
mut errors: PE,
|
||||
) {
|
||||
let mut it = args.iter();
|
||||
fn next_param<'a, A: AsRef<str> + 'a>(
|
||||
|
|
@ -400,24 +418,21 @@ pub mod cli {
|
|||
},
|
||||
}
|
||||
} else {
|
||||
if let Some(out) = out.as_mut() {
|
||||
out.push(arg.as_ref().into());
|
||||
}
|
||||
out.push(arg.as_ref().into());
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
if let (Some(err), Some(errors)) = (go().err(), errors.as_mut())
|
||||
{
|
||||
if let Some(err) = go().err() {
|
||||
errors.push(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_env<P: Push<ParseError>>(
|
||||
pub fn parse_env<PS: Push<String>, PE: Push<ParseError>>(
|
||||
&mut self,
|
||||
out: Option<&mut Vec<String>>,
|
||||
errors: Option<&mut P>,
|
||||
out: PS,
|
||||
errors: PE,
|
||||
) {
|
||||
let env_args = std::env::args().collect::<Vec<_>>();
|
||||
self.parse(env_args.as_slice(), out, errors);
|
||||
|
|
@ -514,8 +529,8 @@ pub mod cli {
|
|||
let mut errors = Buf::new(1);
|
||||
cli.parse(
|
||||
&["--foo", "foo", "--bar", "854", "-b", "arg"],
|
||||
Some(&mut args),
|
||||
Some(&mut errors),
|
||||
&mut args,
|
||||
&mut errors,
|
||||
);
|
||||
assert_eq!(foo, "foo");
|
||||
assert_eq!(bar, 854);
|
||||
|
|
|
|||
Loading…
Reference in a new issue