Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanbohan committed Jun 9, 2024
1 parent 9790a45 commit b7752b6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 19 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Value> = [("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());
Expand Down Expand Up @@ -116,14 +116,8 @@ impl Pattern {
/// let grok = Grok::default();
/// let pattern = grok.compile("%{USERNAME}", false).unwrap();
/// let result = pattern.parse("admin [email protected]").unwrap();
/// let expected = [("USERNAME", "admin")]
/// .into_iter()
/// .map(|(k, v)| (k.to_string(), Value::String(v.to_string())))
/// .collect::<HashMap<String, Value>>();
/// 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<HashMap<String, Value>, String> {
let mut map = HashMap::new();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit b7752b6

Please sign in to comment.