pub struct PadUnit<C, P, T> { /* private fields */ }Expand description
First try to match P. If the match succeeds, then try to match T.
Ctor
It will return result of P, and ignoring the result of T.
Example
color_eyre::install()?;
let protocol = "https".or("http".or("ftp"));
let protocol = protocol.pad("://");
let domain = neu::alphabetic().repeat_one_more();
let domain = domain.sep(".").at_least(2);
let url = domain.padded(protocol);
let mut ctx = CharsCtx::new(r#"https://www.mozilla.org"#);
assert_eq!(ctx.ctor(&url)?, ["www", "mozilla", "org"]);
Ok(())Implementations§
Trait Implementations§
source§impl<'a, C, P, T, M, O> Ctor<'a, C, M, O> for PadUnit<C, P, T>where
T: Regex<C, Ret = Span>,
P: Ctor<'a, C, M, O>,
C: Context<'a> + Match<C>,
impl<'a, C, P, T, M, O> Ctor<'a, C, M, O> for PadUnit<C, P, T>where T: Regex<C, Ret = Span>, P: Ctor<'a, C, M, O>, C: Context<'a> + Match<C>,
source§impl<'a, C, P, T> Regex<C> for PadUnit<C, P, T>where
T: Regex<C, Ret = Span>,
P: Regex<C, Ret = Span>,
C: Context<'a> + Match<C>,
impl<'a, C, P, T> Regex<C> for PadUnit<C, P, T>where T: Regex<C, Ret = Span>, P: Regex<C, Ret = Span>, C: Context<'a> + Match<C>,
impl<C: Copy, P: Copy, T: Copy> Copy for PadUnit<C, P, T>
Auto Trait Implementations§
impl<C, P, T> RefUnwindSafe for PadUnit<C, P, T>where C: RefUnwindSafe, P: RefUnwindSafe, T: RefUnwindSafe,
impl<C, P, T> Send for PadUnit<C, P, T>where C: Send, P: Send, T: Send,
impl<C, P, T> Sync for PadUnit<C, P, T>where C: Sync, P: Sync, T: Sync,
impl<C, P, T> Unpin for PadUnit<C, P, T>where C: Unpin, P: Unpin, T: Unpin,
impl<C, P, T> UnwindSafe for PadUnit<C, P, T>where C: UnwindSafe, P: UnwindSafe, T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<'a, C, M, O, I> BoxedCtorHelper<'a, C, M, O> for Iwhere
I: Ctor<'a, C, M, O>,
C: Context<'a> + Match<C>,
impl<'a, C, M, O, I> BoxedCtorHelper<'a, C, M, O> for Iwhere I: Ctor<'a, C, M, O>, C: Context<'a> + Match<C>,
fn into_boxed_ctor(self) -> BoxedCtor<C, I>
source§impl<'a, C, T> ConstructOp<'a, C> for Twhere
T: Regex<C>,
C: Context<'a> + Match<C>,
impl<'a, C, T> ConstructOp<'a, C> for Twhere T: Regex<C>, C: Context<'a> + Match<C>,
source§fn pat(self) -> Pattern<C, T>
fn pat(self) -> Pattern<C, T>
Call .try_mat to match regex P.
Example
color_eyre::install()?;
let digit = re!(['0' - '9']+);
let digit = digit.map(|v: &str| Ok(v.parse::<i64>().unwrap()));
let digits = digit.sep(",".ws());
let array = digits.quote("[", "]");
let mut ctx = CharsCtx::new("[2, 4, 8, 16, 42]");
assert_eq!(ctx.ctor(&array)?, vec![2, 4, 8, 16, 42]);
assert_eq!(ctx.reset().ctor(&array.pat())?, "[2, 4, 8, 16, 42]");
Ok(())source§fn opt(self) -> OptionPat<C, T>
fn opt(self) -> OptionPat<C, T>
Match P and return the result wrapped by Option, ignoring the error.
Example
color_eyre::install()?;
let num = neu::digit(10)
.repeat_one_more()
.map(re::map::from_str::<usize>())
.opt();
assert_eq!(CharsCtx::new("foo").ctor(&num)?, None);
assert_eq!(CharsCtx::new("955").ctor(&num)?, Some(955));
Ok(())source§fn quote<L, R>(self, left: L, right: R) -> Quote<C, T, L, R>
fn quote<L, R>(self, left: L, right: R) -> Quote<C, T, L, R>
First try to match L. If it is succeeds, then try to match P.
If it is succeeds, then try to match R.
Example
color_eyre::install()?;
let ascii = neu::ascii().repeat_one();
let lit = ascii.quote("'", "'");
let ele = lit.sep(",".ws());
let arr = ele.quote("[", "]");
let mut ctx = CharsCtx::new("['a', 'c', 'd', 'f']");
assert_eq!(ctx.ctor(&arr)?, ["a", "c", "d", "f"]);
Ok(())source§fn sep<S>(self, sep: S) -> Separate<C, T, S>
fn sep<S>(self, sep: S) -> Separate<C, T, S>
Match regex P as many times as possible, with S as the delimiter.
Example
color_eyre::install()?;
let name = re!([^ ',' ']' '[']+);
let sep = ','.repeat_one().ws();
let arr = name.sep(sep);
let arr = arr.quote("[", "]");
let mut ctx = CharsCtx::new(r#"[c, rust, java, c++]"#);
assert_eq!(ctx.ctor(&arr)?, vec!["c", "rust", "java", "c++"]);
Ok(())source§fn sep_once<S, R>(self, sep: S, right: R) -> SepOnce<C, T, S, R>
fn sep_once<S, R>(self, sep: S, right: R) -> SepOnce<C, T, S, R>
Match L and R separated by S.
Example
color_eyre::install()?;
let key = neu::alphabetic().repeat_one_more().ws();
let val = neu::whitespace().or(',').not().repeat_one_more().ws();
let sep = "=>".ws();
let ele = key.sep_once(sep, val);
let hash = ele.sep(",".ws()).quote("{".ws(), "}");
let mut ctx = CharsCtx::new(
r#"{
c => c11,
cpp => c++23,
rust => 2021,
}"#,
);
assert_eq!(
ctx.ctor(&hash)?,
[("c", "c11"), ("cpp", "c++23"), ("rust", "2021")]
);
Ok(())source§fn sep_collect<S, O, V>(self, sep: S) -> SepCollect<C, T, S, O, V>
fn sep_collect<S, O, V>(self, sep: S) -> SepCollect<C, T, S, O, V>
Match regex P as many times as possible, with S as the delimiter.
Example
color_eyre::install()?;
let key = neu::alphabetic().repeat_one_more().ws();
let val = neu::whitespace().or(',').not().repeat_one_more().ws();
let sep = "=>".ws();
let ele = key.sep_once(sep, val);
let hash = ele.sep_collect(",".ws()).quote("{".ws(), "}");
let mut ctx = CharsCtx::new(
r#"{
c => c11,
cpp => c++23,
rust => 2021,
}"#,
);
let hash: HashMap<&str, &str> = ctx.ctor(&hash)?;
assert_eq!(hash.get("c"), Some(&"c11"));
assert_eq!(hash.get("cpp"), Some(&"c++23"));
assert_eq!(hash.get("rust"), Some(&"2021"));
Ok(())source§fn or<P>(self, pat: P) -> Or<C, T, P>
fn or<P>(self, pat: P) -> Or<C, T, P>
First try to match L, if it fails, then try to match R.
Example
color_eyre::install()?;
#[derive(Debug, PartialEq, Eq)]
pub enum V<'a> {
S(&'a str),
}
let cond = neu::re_cond(re::not("\\\""));
let str = re!([^ '"' ]+).set_cond(cond).or("\\\"").repeat(1..).pat();
let str = str.quote("\"", "\"");
let str = str.map(|v| Ok(V::S(v)));
let vals = str.sep(",".ws());
let text = r#""lily\"", "lilei", "lucy""#;
let mut ctx = CharsCtx::new(text);
assert_eq!(
ctx.ctor(&vals)?,
[V::S("lily\\\""), V::S("lilei"), V::S("lucy")]
);
Ok(())source§fn ltm<P>(self, pat: P) -> LongestTokenMatch<C, T, P>
fn ltm<P>(self, pat: P) -> LongestTokenMatch<C, T, P>
Match L and R, return the longest match result.
Example
color_eyre::install()?;
#[derive(Debug, PartialEq, Eq)]
pub struct Val<'a>(&'a str);
let val = "v".ltm("val".ltm("value"));
let val = val.map(|v| Ok(Val(v)));
let val = val.sep(",".ws());
let val = val.quote("{", "}");
let mut ctx = CharsCtx::new(r#"{val, v, value}"#);
assert_eq!(ctx.ctor(&val)?, [Val("val"), Val("v"), Val("value")]);
Ok(())source§fn then<P>(self, then: P) -> Then<C, T, P>
fn then<P>(self, then: P) -> Then<C, T, P>
First try to match P. If it succeeds, then try to match T.
Example
color_eyre::install()?;
let ws = neu::whitespace().repeat_full();
let id = neu::ascii_alphabetic().repeat_one_more();
let st = "struct".ws().then(id)._1();
let en = "enum".ws().then(id)._1();
let ty = st.or(en);
let ty = ty.ws().then(ws.quote("{", "}"))._0();
let mut ctx = CharsCtx::new(r#"struct widget { }"#);
assert_eq!(ctx.ctor(&ty)?, "widget");
Ok(())source§fn if_then<I, P>(self, if: I, then: P) -> IfThen<C, T, I, P>
fn if_then<I, P>(self, if: I, then: P) -> IfThen<C, T, I, P>
First try to match P. If it succeeds, then try to match I.
If it succeeds, then try to match T.
Example
color_eyre::install()?;
let sp = neu::whitespace().repeat_full();
let using = "use"
.sep_once(
"",
neu::ascii_alphanumeric()
.or('*')
.or('_')
.repeat_one_more()
.sep("::"),
)
._1()
.if_then("as", neu::ascii_alphanumeric().repeat_one_more());
for (str, res) in [
(
"use neure::prelude::*",
(vec!["neure", "prelude", "*"], None),
),
("use neure as regex", (vec!["neure"], Some("regex"))),
] {
assert_eq!(CharsCtx::new(str).ignore(sp).ctor(&using)?, res);
}
Ok(())source§fn repeat(self, range: impl Into<CRange<usize>>) -> Repeat<C, T>
fn repeat(self, range: impl Into<CRange<usize>>) -> Repeat<C, T>
Example
color_eyre::install()?;
let int = neu::digit(10).repeat_one_more();
let int = int.map(re::map::from_str_radix::<i32>(10));
let num = int.ws().repeat(3..5);
let mut ctx = CharsCtx::new(r#"1 2 3 4"#);
assert_eq!(ctx.ctor(&num)?, [1, 2, 3, 4]);
Ok(())source§fn pad<P>(self, pat: P) -> PadUnit<C, T, P>
fn pad<P>(self, pat: P) -> PadUnit<C, T, P>
Example
color_eyre::install()?;
let sep = neu!([',' ';']);
let end = neu!(['。' '?' '!']);
let word = sep.or(end).not().repeat_one_more();
let sent = word.sep(sep.repeat_one().ws()).pad(end.repeat_one());
let sent = sent.repeat(1..);
let mut ctx = CharsCtx::new(
r#"暖日晴风初破冻。柳眼眉腮,已觉春心动。酒意诗情谁与共。泪融残粉花钿重。乍试夹衫金缕缝。山枕斜敧,枕损钗头凤。独抱浓愁无好梦。夜阑犹剪灯花弄。"#,
);
assert_eq!(ctx.ctor(&sent)?.len(), 8);
Ok(())source§fn padded<P>(self, pat: P) -> PaddedUnit<C, T, P>
fn padded<P>(self, pat: P) -> PaddedUnit<C, T, P>
Example
color_eyre::install()?;
let num = neu::digit(10).repeat_times::<2>();
let time = num.sep_once(":", num);
let time = time.quote("[", "]").ws();
let star = '*'.repeat_times::<3>().ws();
let name = neu::whitespace().not().repeat_one_more().ws();
let status = "left".or("joined").ws();
let record = name.padded(star).then(status);
let record = time.then(record).repeat(1..);
let mut ctx = CharsCtx::new(
r#"[20:59] *** jpn left
[21:00] *** jpn joined
[21:06] *** guifa left
[21:07] *** guifa joined"#,
);
let records = ctx.ctor(&record)?;
assert_eq!(records[0], (("20", "59"), ("jpn", "left")));
assert_eq!(records[1], (("21", "00"), ("jpn", "joined")));
assert_eq!(records[2], (("21", "06"), ("guifa", "left")));
assert_eq!(records[3], (("21", "07"), ("guifa", "joined")));
Ok(())fn map<F, O>(self, func: F) -> Map<C, T, F, O>
fn collect<O, V>(self) -> Collect<C, T, O, V>
fn if<I, E>(self, if: I, else: E) -> IfRegex<C, T, I, E>where I: Fn(&C) -> Result<bool, Error>,
fn ws( self ) -> PadUnit<C, T, NeureZeroMore<C, AsciiWhiteSpace, <C as Context<'a>>::Item, NullCond>>where C: Context<'a, Item = char>,
source§impl<'a, C, T> DynamicCreateCtorThenHelper<'a, C> for Twhere
C: Context<'a> + Match<C>,
impl<'a, C, T> DynamicCreateCtorThenHelper<'a, C> for Twhere C: Context<'a> + Match<C>,
source§fn dyn_then_ctor<F>(self, func: F) -> DynamicCreateCtorThen<C, T, F>
fn dyn_then_ctor<F>(self, func: F) -> DynamicCreateCtorThen<C, T, F>
Construct a new regex with Ctor implementation based on previous result.
Example
color_eyre::install()?;
let num = u8::is_ascii_digit
.repeat_one()
.map(|v: &[u8]| String::from_utf8(v.to_vec()).map_err(|_| Error::Uid(0)))
.map(re::map::from_str::<usize>());
let num = num.clone().sep_once(b",", num);
let re = num.dyn_then_ctor(|a: &(usize, usize)| {
// leave the a's type empty cause rustc reject compile
Ok(b'+'
.repeat_range(a.0..a.0 + 1)
.then(b'-'.repeat_range(a.1..a.1 + 1)))
});
assert_eq!(
BytesCtx::new(b"3,0+++").ctor(&re)?,
((3, 0), ([43, 43, 43].as_slice(), [].as_slice()))
);
assert_eq!(
BytesCtx::new(b"2,1++-").ctor(&re)?,
((2, 1), ([43, 43].as_slice(), [45].as_slice()))
);
assert_eq!(
BytesCtx::new(b"0,3---").ctor(&re)?,
((0, 3), ([].as_slice(), [45, 45, 45].as_slice()))
);
Ok(())