regex_automata/dfa/
start.rs1use core::mem::size_of;
2
3use crate::util::wire::{self, DeserializeError, Endian, SerializeError};
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14pub enum StartKind {
15    Both,
17    Unanchored,
25    Anchored,
28}
29
30impl StartKind {
31    pub(crate) fn from_bytes(
32        slice: &[u8],
33    ) -> Result<(StartKind, usize), DeserializeError> {
34        wire::check_slice_len(slice, size_of::<u32>(), "start kind bytes")?;
35        let (n, nr) = wire::try_read_u32(slice, "start kind integer")?;
36        match n {
37            0 => Ok((StartKind::Both, nr)),
38            1 => Ok((StartKind::Unanchored, nr)),
39            2 => Ok((StartKind::Anchored, nr)),
40            _ => Err(DeserializeError::generic("unrecognized start kind")),
41        }
42    }
43
44    pub(crate) fn write_to<E: Endian>(
45        &self,
46        dst: &mut [u8],
47    ) -> Result<usize, SerializeError> {
48        let nwrite = self.write_to_len();
49        if dst.len() < nwrite {
50            return Err(SerializeError::buffer_too_small("start kind"));
51        }
52        let n = match *self {
53            StartKind::Both => 0,
54            StartKind::Unanchored => 1,
55            StartKind::Anchored => 2,
56        };
57        E::write_u32(n, dst);
58        Ok(nwrite)
59    }
60
61    pub(crate) fn write_to_len(&self) -> usize {
62        size_of::<u32>()
63    }
64
65    #[cfg_attr(feature = "perf-inline", inline(always))]
66    pub(crate) fn has_unanchored(&self) -> bool {
67        matches!(*self, StartKind::Both | StartKind::Unanchored)
68    }
69
70    #[cfg_attr(feature = "perf-inline", inline(always))]
71    pub(crate) fn has_anchored(&self) -> bool {
72        matches!(*self, StartKind::Both | StartKind::Anchored)
73    }
74}