use crate::primitives::{IntoInner, Primitives};
use crate::types::{Input, ParseResult};
pub fn run_parser<I, F, T, E>(input: I, parser: F) -> (I, Result<T, E>)
where
I: Input,
F: FnOnce(I) -> ParseResult<I, T, E>,
{
parser(input).into_inner()
}
pub fn parse_only<'a, I, T, E, F>(parser: F, input: &'a [I]) -> Result<T, (&'a [I], E)>
where
I: Copy + PartialEq,
F: FnOnce(&'a [I]) -> ParseResult<&'a [I], T, E>,
{
match parser(input).into_inner() {
(_, Ok(t)) => Ok(t),
(mut b, Err(e)) => Err((b.consume_remaining(), e)),
}
}
pub fn parse_only_str<'a, T, E, F>(parser: F, input: &'a str) -> Result<T, (&'a str, E)>
where
F: FnOnce(&'a str) -> ParseResult<&'a str, T, E>,
{
match parser(input).into_inner() {
(_, Ok(t)) => Ok(t),
(mut b, Err(e)) => Err((b.consume_remaining(), e)),
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::primitives::Primitives;
use crate::types::Input;
#[test]
fn inspect_input() {
let mut input = None;
assert_eq!(
parse_only(
|i| {
input = Some(i.to_vec());
i.ret::<_, ()>("the result")
},
b"the input"
),
Ok("the result")
);
assert_eq!(input, Some(b"the input".to_vec()));
}
#[test]
fn err() {
assert_eq!(
parse_only(
|mut i| {
i.consume(4);
i.err::<(), _>("my error")
},
b"the input"
),
Err((&b"input"[..], "my error"))
);
}
#[test]
fn inspect_input_str() {
let mut input = None;
assert_eq!(
parse_only_str(
|i| {
input = Some(i.to_owned());
i.ret::<_, ()>("the result")
},
"the input"
),
Ok("the result")
);
assert_eq!(input, Some("the input".to_owned()));
}
#[test]
fn err_str() {
assert_eq!(
parse_only_str(
|mut i| {
i.consume(4);
i.err::<(), _>("my error")
},
"the input"
),
Err(("input", "my error"))
);
}
}