From b7752b6ffd1fa177978dae7006336dadd30be925 Mon Sep 17 00:00:00 2001 From: yuanbohan Date: Sun, 9 Jun 2024 14:53:15 +0800 Subject: [PATCH] docs --- Cargo.toml | 2 +- README.md | 2 +- src/lib.rs | 39 +++++++++++++++++++-------------------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0e0f2f5..6d48b74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grok-rs" -version = "0.1.2" +version = "0.1.3" edition = "2021" readme = "README.md" description = "Rust port of elastic Grok processor" diff --git a/README.md b/README.md index ed5b1ab..a25c5bd 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ the `grok_rs` is a rust port of Elastic Grok processor, inspired by [grok-go][gr ```toml [dependencies] -grok-rs = "0.1.2" +grok-rs = "0.1.3" ``` ## Example diff --git a/src/lib.rs b/src/lib.rs index 6f630ac..be114ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,11 +8,14 @@ //! - float //! - double //! - bool +//! - boolean +//! +//! If the type is not specified, then the value will be kept as string. //! //! # Usage //! -//! Initiate a Grok instance with the default patterns, or add custom patterns,then compile the your pattern, -//! and parse the input string based on the pattern. +//! Initiate a Grok instance which includes the default patterns, or add custom patterns, +//! then compile your whole pattern, and parse the input string based on the pattern. //! //! ``` //! use std::collections::HashMap; @@ -21,10 +24,7 @@ //! let mut grok = Grok::default(); //! grok.add_pattern("NAME", r"[A-z0-9._-]+"); //! let pattern = grok.compile("%{NAME}", false).unwrap(); -//! let expected: HashMap = [("NAME", "admin")] -//! .into_iter() -//! .map(|(k, v)| (k.to_string(), Value::String(v.to_string()))) -//! .collect(); +//! let expected = HashMap::from([("NAME".to_string(), Value::String("admin".into()))]); //! //! assert_eq!(expected, pattern.parse("admin").unwrap()); //! assert_eq!(expected, pattern.parse("admin user").unwrap()); @@ -116,14 +116,8 @@ impl Pattern { /// let grok = Grok::default(); /// let pattern = grok.compile("%{USERNAME}", false).unwrap(); /// let result = pattern.parse("admin admin@example.com").unwrap(); - /// let expected = [("USERNAME", "admin")] - /// .into_iter() - /// .map(|(k, v)| (k.to_string(), Value::String(v.to_string()))) - /// .collect::>(); + /// let expected = HashMap::from([("USERNAME".to_string(), Value::String("admin".into()))]); /// assert_eq!(expected, result); - /// - /// let empty = pattern.parse("✅").unwrap(); - /// assert!(empty.is_empty()); /// ``` pub fn parse(&self, s: &str) -> Result, String> { let mut map = HashMap::new(); @@ -334,6 +328,17 @@ mod tests { } } + #[test] + fn test_pattern_parse_no_captures() { + let grok = Grok::default(); + let pattern = grok.compile("%{USERNAME}", false).unwrap(); + + assert!(pattern.parse("$#@").unwrap().is_empty()); + assert!(pattern.parse("").unwrap().is_empty()); + assert!(pattern.parse("✅🚀🌍").unwrap().is_empty()); + assert!(pattern.parse(" ").unwrap().is_empty()); + } + #[test] fn test_composite_or_pattern() { let mut grok = Grok::default(); @@ -858,13 +863,7 @@ mod tests { for value in values { let m = p.parse(value).unwrap(); let result = m.get("result").unwrap(); - assert_eq!( - &Value::String(value.to_string()), - result, - "pattern: {}, value: {}", - pattern, - value - ); + assert_eq!(&Value::String(value.to_string()), result); } } }