Struct CompileError

Source
pub struct CompileError {
    pub message: String,
    pub expression: Option<ExpressionIndex>,
}
Available on crate feature compiler only.
Expand description

Error details returned by the pattern compiler.

This is returned by the compile calls (Database::compile() and Database::compile_multi()) on failure. The caller may inspect the values returned in this type to determine the cause of failure.

Fields§

§message: String

A human-readable error message describing the error.

§Common Errors

Common errors generated during the compile process include:

  • Invalid parameter: An invalid argument was specified in the compile call.

  • Unrecognised flag: An unrecognised value was passed in the flags argument.

  • Pattern matches empty buffer: By default, Vectorscan only supports patterns that will always consume at least one byte of input. Patterns that do not have this property (such as /(abc)?/) will produce this error unless the Flags::ALLOWEMPTY flag is supplied. Note that such patterns will produce a match for every byte when scanned.

  • Embedded anchors not supported: Vectorscan only supports the use of anchor meta-characters (such as ^ and $) in patterns where they could only match at the start or end of a buffer. A pattern containing an embedded anchor, such as /abc^def/, can never match, as there is no way for abc to precede the start of the data stream.

  • Bounded repeat is too large: The pattern contains a repeated construct with very large finite bounds.

  • Unsupported component type: An unsupported PCRE construct was used in the pattern. Consider using chimera for full PCRE support.

  • Unable to generate bytecode: This error indicates that Vectorscan was unable to compile a pattern that is syntactically valid. The most common cause is a pattern that is very long and complex or contains a large repeated subpattern.

  • Unable to allocate memory: The library was unable to allocate temporary storage used during compilation time.

  • Allocator returned misaligned memory: The memory allocator (either libc::malloc() or the allocator set with set_db_allocator()) did not correctly return memory suitably aligned for the largest representable data type on this platform.

  • Internal error: An unexpected error occurred: if this error is reported, please contact the Vectorscan team with a description of the situation.

§expression: Option<ExpressionIndex>

The zero-based number of the expression that caused the error (if this can be determined). For a database with a single expression, this value will be 0:

 use vectorscan::{expression::*, error::*, matchers::*, flags::*};

 let expr: Expression = "as(df".parse()?;
 let index = match expr.compile(Flags::default(), Mode::BLOCK) {
   Err(VectorscanCompileError::Compile(CompileError { expression, .. })) => expression,
   _ => unreachable!(),
 };
 assert_eq!(index, Some(ExpressionIndex(0)));

Note that while this uses the same ExpressionIndex type as in Match, the value is not calculated from any ExprId instances provided to ExpressionSet::with_ids(), but instead just from the expression’s index in the set:

 use vectorscan::{expression::*, error::*, matchers::*, flags::*};

 let e1: Expression = "aa".parse()?;
 let e2: Expression = "as(df".parse()?;
 let set = ExpressionSet::from_exprs([&e1, &e2]).with_ids([ExprId(2), ExprId(3)]);
 let index = match set.compile(Mode::BLOCK) {
   Err(VectorscanCompileError::Compile(CompileError { expression, .. })) => expression,
   _ => unreachable!(),
 };
 assert_eq!(index, Some(ExpressionIndex(1)));

If the error is not specific to an expression, then this value will be None:

 // Using vectorscan::alloc requires the "alloc" feature.
 #[cfg(feature = "alloc")]
 fn main() -> Result<(), vectorscan::error::VectorscanError> {
   use vectorscan::{expression::*, error::*, flags::*, alloc::*};
   use std::{alloc::{GlobalAlloc, Layout}, ptr};

   // Create a broken allocator:
   struct S;
   unsafe impl GlobalAlloc for S {
     unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { ptr::null_mut() }
     unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
   }
   // Set it as the db compile allocator:
   assert!(set_db_allocator(LayoutTracker::new(S.into())).unwrap().is_none());

   let expr: Expression = "a".parse()?;
   let CompileError { message, expression } = match expr.compile(Flags::default(), Mode::BLOCK) {
     Err(VectorscanCompileError::Compile(err)) => err,
     _ => unreachable!(),
   };
   assert_eq!(expression, None);
   assert_eq!(&message, "Could not allocate memory for bytecode.");
   Ok(())
 }

Trait Implementations§

Source§

impl Debug for CompileError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for CompileError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for CompileError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<CompileError> for VectorscanCompileError

Source§

fn from(source: CompileError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.