Struct DynamicBoxedRegex

Source
pub struct DynamicBoxedRegex<'a, C, R> { /* private fields */ }

Implementations§

Source§

impl<'a, C, R> DynamicBoxedRegex<'a, C, R>

Source

pub fn new(inner: impl Regex<C, Ret = R> + 'a) -> Self

Trait Implementations§

Source§

impl<'a, C, O, H, A> Ctor<'a, C, O, O, H, A> for DynamicBoxedRegex<'_, C, Span>
where C: Context<'a> + Match<C>, H: Handler<A, Out = O, Error = Error>, A: Extract<'a, C, Span, Out<'a> = A, Error = Error>,

Source§

fn construct(&self, ctx: &mut C, handler: &mut H) -> Result<O, Error>

Source§

impl<C, R> Regex<C> for DynamicBoxedRegex<'_, C, R>

Source§

type Ret = R

Source§

fn try_parse(&self, ctx: &mut C) -> Result<Self::Ret, Error>

Source§

fn parse(&self, ctx: &mut C) -> bool

Source§

impl<'a, C, R> Wrapped for DynamicBoxedRegex<'a, C, R>

Source§

type Inner = Box<dyn Regex<C, Ret = R> + 'a>

Source§

fn wrap(inner: Self::Inner) -> Self

Source§

fn inner(&self) -> &Self::Inner

Source§

fn inner_mut(&mut self) -> &mut Self::Inner

Auto Trait Implementations§

§

impl<'a, C, R> Freeze for DynamicBoxedRegex<'a, C, R>

§

impl<'a, C, R> !RefUnwindSafe for DynamicBoxedRegex<'a, C, R>

§

impl<'a, C, R> !Send for DynamicBoxedRegex<'a, C, R>

§

impl<'a, C, R> !Sync for DynamicBoxedRegex<'a, C, R>

§

impl<'a, C, R> Unpin for DynamicBoxedRegex<'a, C, R>

§

impl<'a, C, R> !UnwindSafe for DynamicBoxedRegex<'a, C, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ConstructIntoOp for T

Source§

fn into_box(self) -> WrappedTy<BoxedCtor<T>>

Return a type that wraps Ctor with Box.

§Example
    let re = b'+'
        .or(b'-')
        .then(u8::is_ascii_hexdigit)
        .then(u8::is_ascii_hexdigit.repeat_times::<3>())
        .pat()
        .map(|v: &[u8]| String::from_utf8(v.to_vec()).map_err(|_| Error::Uid(0)))
        .into_box();

    assert_eq!(BytesCtx::new(b"+AE00").ctor(&re)?, "+AE00");
    assert!(BytesCtx::new(b"-GH66").ctor(&re).is_err());
    assert_eq!(BytesCtx::new(b"-83FD").ctor(&re)?, "-83FD");
    Ok(())
Source§

fn into_rc(self) -> WrappedTy<Rc<T>>

Return a type that wrap Ctor with Rc.

§Example
    color_eyre::install()?;
    let year = char::is_ascii_digit.repeat_times::<4>();
    let num = char::is_ascii_digit.repeat_times::<2>();
    let date = year.sep_once("-", num.sep_once("-", num)).into_rc();
    let time = num.sep_once(":", num.sep_once(":", num));
    let datetime = date.clone().sep_once(" ", time);

    assert_eq!(
        CharsCtx::new("2024-01-08").ctor(&date)?,
        ("2024", ("01", "08"))
    );
    assert_eq!(
        CharsCtx::new("2024-01-08 10:01:13").ctor(&datetime)?,
        (("2024", ("01", "08")), ("10", ("01", "13")))
    );
    Ok(())
Source§

fn into_dyn<'a, 'b, C, M, O, H, A>( self, ) -> WrappedTy<DynamicBoxedCtor<'a, 'b, C, M, O, H, A>>
where C: Context<'a> + Match<C>, T: Ctor<'a, C, M, O, H, A> + 'b,

§Example 2
    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(map::from_str::<usize>());
    let num = num.clone().sep_once(b",", num);
    let re = num.into_dyn();

    assert_eq!(BytesCtx::new(b"3,0").ctor(&re)?, (3, 0));
    assert_eq!(BytesCtx::new(b"2,1").ctor(&re)?, (2, 1));
    assert_eq!(BytesCtx::new(b"0,3").ctor(&re)?, (0, 3));
    Ok(())
Source§

fn into_arc(self) -> WrappedTy<Arc<T>>

Source§

fn into_cell(self) -> WrappedTy<Cell<T>>

Source§

fn into_refcell(self) -> WrappedTy<RefCell<T>>

Source§

fn into_mutex(self) -> WrappedTy<Mutex<T>>

Source§

fn into_dyn_sync<'a, 'b, C, M, O, H, A>( self, ) -> WrappedTy<DynamicBoxedCtorSync<'a, 'b, C, M, O, H, A>>
where C: Context<'a> + Match<C>, T: Ctor<'a, C, M, O, H, A> + Send + 'b,

Source§

fn into_dyn_arc<'a, 'b, C, M, O, H, A>( self, ) -> WrappedTy<DynamicArcCtor<'a, 'b, C, M, O, H, A>>
where C: Context<'a> + Match<C>, T: Ctor<'a, C, M, O, H, A> + 'b,

Source§

fn into_dyn_rc<'a, 'b, C, M, O, H, A>( self, ) -> WrappedTy<DynamicRcCtor<'a, 'b, C, M, O, H, A>>
where C: Context<'a> + Match<C>, T: Ctor<'a, C, M, O, H, A> + 'b,

Source§

impl<'a, C, T> ConstructOp<'a, C> for T
where T: Regex<C>, C: Context<'a> + Match<C>,

Source§

fn pat(self) -> Pattern<C, T>

Call .try_mat to match regex P.

§Example
    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>

Match P and return the result wrapped by Option, ignoring the error.

§Example
    let num = neu::digit(10)
        .repeat_one_more()
        .map(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>

First try to match L. If it is succeeds, then try to match P. If it is succeeds, then try to match R.

§Example
    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>

Match regex P as many times as possible, with S as the delimiter.

§Example
    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>

Match L and R separated by S.

§Example
    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>

Match regex P as many times as possible, with S as the delimiter.

§Example
    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>

First try to match L, if it fails, then try to match R.

§Example
    #[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>

Match L and R, return the longest match result.

§Example
    #[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>

First try to match P. If it succeeds, then try to match T.

§Example
    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>

First try to match P. If it succeeds, then try to match I. If it succeeds, then try to match T.

§Example
    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>

Repeatedly match regex P, and the number of matches must meet the given range.

§Example
    let int = neu::digit(10).repeat_one_more();
    let int = int.map(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 collect<O, V>(self) -> Collect<C, T, O, V>

Repeatedly match the regex P at least min times.

§Example
    let val = re::consume(2)
        .map(map::from_le_bytes::<i16>())
        .collect::<_, Vec<_>>();

    assert_eq!(
        BytesCtx::new(b"\x2f\0\x1f\0\x0f\0").ctor(&val)?,
        vec![0x2f, 0x1f, 0x0f]
    );

    Ok(())
Source§

fn if<I, E>(self, if: I, else: E) -> IfRegex<C, T, I, E>
where I: Fn(&C) -> Result<bool, Error>,

Construct a branch struct base on the test I(Fn(&C) -> Result<bool, Error>).

§Example
    let val = "file://".r#if(
        // test if it is a file url
        |ctx: &CharsCtx| Ok(ctx.orig()?.starts_with("file")),
        "http://",
    );

    assert_eq!(CharsCtx::new("file://").ctor(&val)?, "file://");
    assert_eq!(CharsCtx::new("http://").ctor(&val)?, "http://");

    Ok(())
Source§

fn pad<P>(self, pat: P) -> Pad<C, T, P>

First try to match P. If the match succeeds, then try to match T.

§Example
    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) -> Padded<C, T, P>

First try to match T. If it succeeds, try to match P.

§Example
    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(())
Source§

fn ws( self, ) -> Pad<C, T, NeureZeroMore<C, AsciiWhiteSpace, <C as Context<'a>>::Item, NullCond>>
where C: Context<'a, Item = char>,

A shortcut for matching trailing ascii spaces.

§Example
    let str = "file://      ";
    let val = "file://".ws();

    assert_eq!(CharsCtx::new(str).ctor(&val)?, "file://");
    assert_eq!(CharsCtx::new(str).try_mat(&val)?, Span::new(0, 13));

    Ok(())
Source§

fn map<F, O>(self, func: F) -> Map<C, T, F, O>

Source§

impl<'a, C, T> DynamicCreateCtorThenHelper<'a, C> for T
where C: Context<'a> + Match<C>,

Source§

fn dyn_then_ctor<F>(self, func: F) -> DynamicCreateCtorThen<C, T, F>

Construct a new regex with Ctor implementation based on previous result.

§Example
    let num = u8::is_ascii_digit
        .repeat_one()
        .map(|v: &[u8]| String::from_utf8(v.to_vec()).map_err(|_| Error::Uid(0)))
        .map(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(())
Source§

impl<'a, C, T> DynamicCreateRegexThenHelper<'a, C> for T
where C: Context<'a> + Match<C>,

Source§

fn dyn_then_regex<F>(self, func: F) -> DynamicCreateRegexThen<C, T, F>

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> RegexIntoOp for T

Source§

fn into_box_regex(self) -> WrappedTy<BoxedRegex<T>>

Source§

fn into_rc_regex(self) -> WrappedTy<Rc<T>>

Source§

fn into_arc_regex(self) -> WrappedTy<Arc<T>>

Source§

fn into_cell_regex(self) -> WrappedTy<Cell<T>>

Source§

fn into_refcell_regex(self) -> WrappedTy<RefCell<T>>

Source§

fn into_mutex_regex(self) -> WrappedTy<Mutex<T>>

Source§

fn into_dyn_regex<'a, 'b, C>( self, ) -> WrappedTy<DynamicBoxedRegex<'b, C, <T as Regex<C>>::Ret>>
where C: Context<'a>, T: Regex<C> + 'b,

Source§

fn into_dyn_arc_regex<'a, 'b, C>( self, ) -> WrappedTy<DynamicArcRegex<'b, C, <T as Regex<C>>::Ret>>
where C: Context<'a>, T: Regex<C> + 'b,

Source§

fn into_dyn_rc_regex<'a, 'b, C>( self, ) -> WrappedTy<DynamicRcRegex<'b, C, <T as Regex<C>>::Ret>>
where C: Context<'a>, T: Regex<C> + 'b,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more