Struct Grok

Source
pub struct Grok { /* private fields */ }

Implementations§

Source§

impl Grok

Source

pub fn add_pattern<T: Into<String>>(&mut self, name: T, pattern: T)

add a custom pattern, if the pattern is already defined, then it will be overwritten.

§Example
use grok_rs::Grok;

let mut grok = Grok::default();
grok.add_pattern("NAME", r"[A-z0-9._-]+");
Examples found in repository?
examples/add_pattern.rs (line 5)
3fn main() {
4    let mut grok = Grok::default();
5    grok.add_pattern("NAME", r"[A-z0-9._-]+");
6    let pattern = grok.compile("%{NAME}", false).unwrap();
7    let result = pattern.parse("admin").unwrap();
8    println!("{:#?}", result);
9}
More examples
Hide additional examples
examples/type.rs (line 5)
3fn main() {
4    let mut grok = Grok::default();
5    grok.add_pattern("NUMBER", r"\d+");
6
7    let pattern = grok.compile("%{NUMBER:digit:int}", false).unwrap();
8    let result = pattern.parse("hello 123").unwrap();
9    println!("{:#?}", result);
10}
Source

pub fn compile( &self, s: &str, named_capture_only: bool, ) -> Result<Pattern, String>

Compile the pattern, and return a Pattern.

  • if named_capture_only is true, then the unnamed capture group will be ignored.
  • if the pattern is invalid or not found , then an error will be returned.

Due to the compile process is heavy, it’s recommended compile the pattern once and reuse it.

§Example

the USERNAME will be ignored because named_capture_only is true.

use grok_rs::Grok;
let grok = Grok::default();
let pattern = grok.compile("%{USERNAME} %{EMAILADDRESS:email}", true).unwrap();
Examples found in repository?
examples/add_pattern.rs (line 6)
3fn main() {
4    let mut grok = Grok::default();
5    grok.add_pattern("NAME", r"[A-z0-9._-]+");
6    let pattern = grok.compile("%{NAME}", false).unwrap();
7    let result = pattern.parse("admin").unwrap();
8    println!("{:#?}", result);
9}
More examples
Hide additional examples
examples/named_capture_only.rs (line 6)
3fn main() {
4    let grok = Grok::default();
5    let pattern = grok
6        .compile("%{USERNAME} %{EMAILADDRESS:email}", true)
7        .unwrap();
8    let result = pattern.parse("admin admin@example.com").unwrap();
9    println!("{:#?}", result);
10}
examples/type.rs (line 7)
3fn main() {
4    let mut grok = Grok::default();
5    grok.add_pattern("NUMBER", r"\d+");
6
7    let pattern = grok.compile("%{NUMBER:digit:int}", false).unwrap();
8    let result = pattern.parse("hello 123").unwrap();
9    println!("{:#?}", result);
10}
examples/default_patterns.rs (line 7)
3fn main() {
4    let grok = Grok::default();
5    let pattern = grok
6        // USERNAME and EMAILADDRESS are defined in grok-patterns
7        .compile("%{USERNAME}", false)
8        .unwrap();
9
10    let result = pattern.parse("admin admin@example.com").unwrap();
11    println!("{:#?}", result);
12}

Trait Implementations§

Source§

impl Debug for Grok

Source§

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

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

impl Default for Grok

Source§

fn default() -> Grok

Returns the “default value” for a type. Read more
Source§

impl<S: Into<String>, const N: usize> From<[(S, S); N]> for Grok

Source§

fn from(arr: [(S, S); N]) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<String>> FromIterator<(T, T)> for Grok

Source§

fn from_iter<I: IntoIterator<Item = (T, T)>>(iter: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl Freeze for Grok

§

impl RefUnwindSafe for Grok

§

impl Send for Grok

§

impl Sync for Grok

§

impl Unpin for Grok

§

impl UnwindSafe for Grok

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, 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.