1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
use std::{fmt::Debug, marker::PhantomData};
use super::Neu;
///
/// Return true if the value matches `L` or `R`.
///
/// # Example
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> color_eyre::Result<()> {
/// # color_eyre::install()?;
/// let aorb = 'a'.or('b').repeat::<1, 2>();
/// let mut ctx = CharsCtx::new("abc");
///
/// assert_eq!(ctx.try_mat(&aorb)?, Span::new(0, 2));
///
/// let aorb = re!(['a' 'b']{1,2});
/// let mut ctx = CharsCtx::new("abc");
///
/// assert_eq!(ctx.try_mat(&aorb)?, Span::new(0, 2));
///
/// Ok(())
/// # }
/// ```
#[derive(Default, Copy)]
pub struct Or<L, R, T>
where
L: Neu<T>,
R: Neu<T>,
{
left: L,
right: R,
marker: PhantomData<T>,
}
impl<L, R, T> Debug for Or<L, R, T>
where
L: Neu<T> + Debug,
R: Neu<T> + Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Or")
.field("left", &self.left)
.field("right", &self.right)
.finish()
}
}
impl<L, R, T> Clone for Or<L, R, T>
where
L: Neu<T> + Clone,
R: Neu<T> + Clone,
{
fn clone(&self) -> Self {
Self {
left: self.left.clone(),
right: self.right.clone(),
marker: self.marker,
}
}
}
impl<L, R, T> Or<L, R, T>
where
L: Neu<T>,
R: Neu<T>,
{
pub fn new(left: L, right: R) -> Self {
Self {
left,
right,
marker: PhantomData,
}
}
pub fn left(&self) -> &L {
&self.left
}
pub fn right(&self) -> &R {
&self.right
}
pub fn left_mut(&mut self) -> &mut L {
&mut self.left
}
pub fn right_mut(&mut self) -> &mut R {
&mut self.right
}
pub fn set_left(&mut self, left: L) -> &mut Self {
self.left = left;
self
}
pub fn set_right(&mut self, right: R) -> &mut Self {
self.right = right;
self
}
}
impl<L, R, T> Neu<T> for Or<L, R, T>
where
L: Neu<T>,
R: Neu<T>,
{
#[inline(always)]
fn is_match(&self, other: &T) -> bool {
let ret = self.left.is_match(other) || self.right.is_match(other);
crate::trace_log!("neu logical `or` -> {ret}");
ret
}
}
///
/// Return true if the value matches `L` or `R`.
///
/// # Example
///
/// ```
/// # use neure::prelude::*;
/// #
/// # fn main() -> color_eyre::Result<()> {
/// # color_eyre::install()?;
/// let re = u8::is_ascii_hexdigit.or(b'g'.or(b'G')).repeat_times::<4>();
/// let re = re.padded(b"0x");
///
/// assert_eq!(BytesCtx::new(b"0xcfag").ctor(&re)?, b"cfag");
/// Ok(())
/// # }
/// ```
pub fn or<T, L: Neu<T>, R: Neu<T>>(left: L, right: R) -> Or<L, R, T> {
Or::new(left, right)
}