neure/re/
into.rs

1use crate::ctx::Context;
2use crate::ctx::Match;
3use crate::re::BoxedRegex;
4use crate::re::Regex;
5
6use std::cell::Cell;
7use std::cell::RefCell;
8use std::rc::Rc;
9use std::sync::Arc;
10use std::sync::Mutex;
11
12use super::ctor::BoxedCtor;
13use super::ctor::DynamicArcCtor;
14use super::ctor::DynamicBoxedCtor;
15use super::ctor::DynamicRcCtor;
16use super::Ctor;
17use super::DynamicArcRegex;
18use super::DynamicBoxedCtorSync;
19use super::DynamicBoxedRegex;
20use super::DynamicRcRegex;
21use super::WrappedTy;
22
23pub trait RegexIntoOp
24where
25    Self: Sized,
26{
27    fn into_box_regex(self) -> WrappedTy<BoxedRegex<Self>>;
28
29    fn into_rc_regex(self) -> WrappedTy<Rc<Self>>;
30
31    fn into_arc_regex(self) -> WrappedTy<Arc<Self>>;
32
33    fn into_cell_regex(self) -> WrappedTy<Cell<Self>>;
34
35    fn into_refcell_regex(self) -> WrappedTy<RefCell<Self>>;
36
37    fn into_mutex_regex(self) -> WrappedTy<Mutex<Self>>;
38
39    fn into_dyn_regex<'a, 'b, C>(
40        self,
41    ) -> WrappedTy<DynamicBoxedRegex<'b, C, <Self as Regex<C>>::Ret>>
42    where
43        C: Context<'a>,
44        Self: Regex<C> + 'b;
45
46    fn into_dyn_arc_regex<'a, 'b, C>(
47        self,
48    ) -> WrappedTy<DynamicArcRegex<'b, C, <Self as Regex<C>>::Ret>>
49    where
50        C: Context<'a>,
51        Self: Regex<C> + 'b;
52
53    fn into_dyn_rc_regex<'a, 'b, C>(
54        self,
55    ) -> WrappedTy<DynamicRcRegex<'b, C, <Self as Regex<C>>::Ret>>
56    where
57        C: Context<'a>,
58        Self: Regex<C> + 'b;
59}
60
61impl<T> RegexIntoOp for T {
62    fn into_box_regex(self) -> WrappedTy<BoxedRegex<Self>> {
63        WrappedTy {
64            value: BoxedRegex::new(self),
65        }
66    }
67
68    fn into_rc_regex(self) -> WrappedTy<Rc<Self>> {
69        WrappedTy::new(Rc::new(self))
70    }
71
72    fn into_arc_regex(self) -> WrappedTy<Arc<Self>> {
73        WrappedTy::new(Arc::new(self))
74    }
75
76    fn into_cell_regex(self) -> WrappedTy<Cell<Self>> {
77        WrappedTy::new(Cell::new(self))
78    }
79
80    fn into_refcell_regex(self) -> WrappedTy<RefCell<Self>> {
81        WrappedTy::new(RefCell::new(self))
82    }
83
84    fn into_mutex_regex(self) -> WrappedTy<Mutex<Self>> {
85        WrappedTy::new(Mutex::new(self))
86    }
87
88    fn into_dyn_regex<'a, 'b, C>(
89        self,
90    ) -> WrappedTy<DynamicBoxedRegex<'b, C, <Self as Regex<C>>::Ret>>
91    where
92        C: Context<'a>,
93        Self: Regex<C> + 'b,
94    {
95        WrappedTy {
96            value: DynamicBoxedRegex::new(self),
97        }
98    }
99
100    fn into_dyn_arc_regex<'a, 'b, C>(
101        self,
102    ) -> WrappedTy<DynamicArcRegex<'b, C, <Self as Regex<C>>::Ret>>
103    where
104        C: Context<'a>,
105        Self: Regex<C> + 'b,
106    {
107        WrappedTy {
108            value: DynamicArcRegex::new(self),
109        }
110    }
111
112    fn into_dyn_rc_regex<'a, 'b, C>(
113        self,
114    ) -> WrappedTy<DynamicRcRegex<'b, C, <Self as Regex<C>>::Ret>>
115    where
116        C: Context<'a>,
117        Self: Regex<C> + 'b,
118    {
119        WrappedTy {
120            value: DynamicRcRegex::new(self),
121        }
122    }
123}
124
125pub trait ConstructIntoOp
126where
127    Self: Sized,
128{
129    fn into_box(self) -> WrappedTy<BoxedCtor<Self>>;
130
131    fn into_rc(self) -> WrappedTy<Rc<Self>>;
132
133    fn into_arc(self) -> WrappedTy<Arc<Self>>;
134
135    fn into_cell(self) -> WrappedTy<Cell<Self>>;
136
137    fn into_refcell(self) -> WrappedTy<RefCell<Self>>;
138
139    fn into_mutex(self) -> WrappedTy<Mutex<Self>>;
140
141    fn into_dyn<'a, 'b, C, M, O, H, A>(self) -> WrappedTy<DynamicBoxedCtor<'a, 'b, C, M, O, H, A>>
142    where
143        C: Context<'a> + Match<C>,
144        Self: Ctor<'a, C, M, O, H, A> + 'b;
145
146    fn into_dyn_sync<'a, 'b, C, M, O, H, A>(
147        self,
148    ) -> WrappedTy<DynamicBoxedCtorSync<'a, 'b, C, M, O, H, A>>
149    where
150        C: Context<'a> + Match<C>,
151        Self: Ctor<'a, C, M, O, H, A> + Send + 'b;
152
153    fn into_dyn_arc<'a, 'b, C, M, O, H, A>(
154        self,
155    ) -> WrappedTy<DynamicArcCtor<'a, 'b, C, M, O, H, A>>
156    where
157        C: Context<'a> + Match<C>,
158        Self: Ctor<'a, C, M, O, H, A> + 'b;
159
160    fn into_dyn_rc<'a, 'b, C, M, O, H, A>(self) -> WrappedTy<DynamicRcCtor<'a, 'b, C, M, O, H, A>>
161    where
162        C: Context<'a> + Match<C>,
163        Self: Ctor<'a, C, M, O, H, A> + 'b;
164}
165
166impl<T> ConstructIntoOp for T
167where
168    Self: Sized,
169{
170    ///
171    /// Return a type that wraps `Ctor` with Box.
172    ///
173    /// # Example
174    ///
175    /// ```
176    /// # use neure::{err::Error, prelude::*};
177    /// #
178    /// # fn main() -> color_eyre::Result<()> {
179    /// #   color_eyre::install()?;
180    ///     let re = b'+'
181    ///         .or(b'-')
182    ///         .then(u8::is_ascii_hexdigit)
183    ///         .then(u8::is_ascii_hexdigit.repeat_times::<3>())
184    ///         .pat()
185    ///         .map(|v: &[u8]| String::from_utf8(v.to_vec()).map_err(|_| Error::Uid(0)))
186    ///         .into_box();
187    ///
188    ///     assert_eq!(BytesCtx::new(b"+AE00").ctor(&re)?, "+AE00");
189    ///     assert!(BytesCtx::new(b"-GH66").ctor(&re).is_err());
190    ///     assert_eq!(BytesCtx::new(b"-83FD").ctor(&re)?, "-83FD");
191    ///     Ok(())
192    /// # }
193    /// ```
194    fn into_box(self) -> WrappedTy<BoxedCtor<Self>> {
195        WrappedTy::new(Box::new(self))
196    }
197
198    ///
199    /// Return a type that wrap `Ctor` with `Rc`.
200    ///
201    /// # Example
202    ///
203    /// ```
204    /// # use neure::prelude::*;
205    /// #
206    /// # fn main() -> color_eyre::Result<()> {
207    ///     color_eyre::install()?;
208    ///     let year = char::is_ascii_digit.repeat_times::<4>();
209    ///     let num = char::is_ascii_digit.repeat_times::<2>();
210    ///     let date = year.sep_once("-", num.sep_once("-", num)).into_rc();
211    ///     let time = num.sep_once(":", num.sep_once(":", num));
212    ///     let datetime = date.clone().sep_once(" ", time);
213    ///
214    ///     assert_eq!(
215    ///         CharsCtx::new("2024-01-08").ctor(&date)?,
216    ///         ("2024", ("01", "08"))
217    ///     );
218    ///     assert_eq!(
219    ///         CharsCtx::new("2024-01-08 10:01:13").ctor(&datetime)?,
220    ///         (("2024", ("01", "08")), ("10", ("01", "13")))
221    ///     );
222    ///     Ok(())
223    /// # }
224    /// ```
225    fn into_rc(self) -> WrappedTy<Rc<Self>> {
226        WrappedTy::new(Rc::new(self))
227    }
228
229    fn into_arc(self) -> WrappedTy<Arc<Self>> {
230        WrappedTy::new(Arc::new(self))
231    }
232
233    fn into_cell(self) -> WrappedTy<Cell<Self>> {
234        WrappedTy::new(Cell::new(self))
235    }
236
237    fn into_refcell(self) -> WrappedTy<RefCell<Self>> {
238        WrappedTy::new(RefCell::new(self))
239    }
240
241    fn into_mutex(self) -> WrappedTy<Mutex<Self>> {
242        WrappedTy::new(Mutex::new(self))
243    }
244
245    /// # Example 2
246    ///
247    /// ```
248    /// # use neure::{err::Error, prelude::*};
249    /// #
250    /// # fn main() -> color_eyre::Result<()> {
251    ///     color_eyre::install()?;
252    ///     let num = u8::is_ascii_digit
253    ///         .repeat_one()
254    ///         .map(|v: &[u8]| String::from_utf8(v.to_vec()).map_err(|_| Error::Uid(0)))
255    ///         .map(map::from_str::<usize>());
256    ///     let num = num.clone().sep_once(b",", num);
257    ///     let re = num.into_dyn();
258    ///
259    ///     assert_eq!(BytesCtx::new(b"3,0").ctor(&re)?, (3, 0));
260    ///     assert_eq!(BytesCtx::new(b"2,1").ctor(&re)?, (2, 1));
261    ///     assert_eq!(BytesCtx::new(b"0,3").ctor(&re)?, (0, 3));
262    ///     Ok(())
263    /// # }
264    /// ```
265    fn into_dyn<'a, 'b, C, M, O, H, A>(self) -> WrappedTy<DynamicBoxedCtor<'a, 'b, C, M, O, H, A>>
266    where
267        C: Context<'a> + Match<C>,
268        Self: Ctor<'a, C, M, O, H, A> + 'b,
269    {
270        WrappedTy {
271            value: DynamicBoxedCtor::new(self),
272        }
273    }
274
275    fn into_dyn_sync<'a, 'b, C, M, O, H, A>(
276        self,
277    ) -> WrappedTy<DynamicBoxedCtorSync<'a, 'b, C, M, O, H, A>>
278    where
279        C: Context<'a> + Match<C>,
280        Self: Ctor<'a, C, M, O, H, A> + Send + 'b,
281    {
282        WrappedTy {
283            value: DynamicBoxedCtorSync::new(self),
284        }
285    }
286
287    fn into_dyn_arc<'a, 'b, C, M, O, H, A>(self) -> WrappedTy<DynamicArcCtor<'a, 'b, C, M, O, H, A>>
288    where
289        C: Context<'a> + Match<C>,
290        Self: Ctor<'a, C, M, O, H, A> + 'b,
291    {
292        WrappedTy {
293            value: DynamicArcCtor::new(self),
294        }
295    }
296
297    fn into_dyn_rc<'a, 'b, C, M, O, H, A>(self) -> WrappedTy<DynamicRcCtor<'a, 'b, C, M, O, H, A>>
298    where
299        C: Context<'a> + Match<C>,
300        Self: Ctor<'a, C, M, O, H, A> + 'b,
301    {
302        WrappedTy {
303            value: DynamicRcCtor::new(self),
304        }
305    }
306}