Struct Pattern

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

Implementations§

Source§

impl Pattern

Source

pub fn parse(&self, s: &str) -> Result<HashMap<String, Value>, String>

parse the input string based on the pattern, and rename the captured group based on alias.

  • if type is specified, then the value will be converted to the specified type.
  • if the type is not supported, then the value will be kept as string.
  • if the value can’t be converted to the specified type, then an error will be returned.
  • if the value can’t be captured, then an empty map will be returned.
§Example
use std::collections::HashMap;
use grok_rs::{Grok, Value};

let grok = Grok::default();
let pattern = grok.compile("%{USERNAME}", false).unwrap();
let result = pattern.parse("admin admin@example.com").unwrap();
let expected = HashMap::from([("USERNAME".to_string(), Value::String("admin".into()))]);
assert_eq!(expected, result);
Examples found in repository?
examples/add_pattern.rs (line 7)
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 8)
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 8)
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 10)
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 Pattern

Source§

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

Formats the value using the given formatter. Read more

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