Crates.io | doc-search-dsl |
lib.rs | doc-search-dsl |
version | 0.1.0 |
source | src |
created_at | 2024-08-03 16:51:45.172125 |
updated_at | 2024-08-03 16:51:45.172125 |
description | A Rust procedural macro for creating complex regex patterns using a Domain-Specific Language (DSL). |
homepage | |
repository | |
max_upload_size | |
id | 1324450 |
size | 13,539 |
A Rust procedural macro for creating complex regex patterns using a Domain-Specific Language (DSL). This DSL allows you to define hierarchical regex patterns that can be used with a custom Rule enum for advanced text matching and processing.
cargo add doc_search_dsl lazy_regex
Here's a basic example of how to use the DSL:
use doc_search_dsl::{pat, Rule};
fn main() {
let content = vec![
"Once upon a midnight dreary, while I pondered, weak and weary,",
"Over many a quaint and curious volume of forgotten lore—",
"While I nodded, nearly napping, suddenly there came a tapping,",
"As of some one gently rapping, rapping at my chamber door.",
"''Tis some visitor,' I muttered, 'tapping at my chamber door—",
"Only this and nothing more.",
];
let p = pat! {
all {
"nodded",
"WHILE"i,
"nothing"
}
};
assert!(matches!(p, Rule::And(_)));
assert_eq!(p.occurances(&content), 1);
}
This example creates a pattern that matches if all of "nodded", "WHILE" (case-insensitive), and "nothing" are present in the content.
Let's explore different pattern types, from simple to complex:
The simplest pattern matches a single string:
let p = pat!("dreary");
assert!(matches!(p, Rule::One(_)));
assert_eq!(p.occurances(&content), 1);
This matches any line containing "dreary".
You can add flags to modify the pattern behavior:
let p = pat!("WHILE"i);
assert!(matches!(p, Rule::One(_)));
assert_eq!(p.occurances(&content), 2);
The i
flag makes the pattern case-insensitive, so it matches both "While" and "while".
The any
pattern matches if any of its sub-patterns match:
let p = pat! {
any {
"dreary",
"curious",
"tapping"
}
};
assert!(matches!(p, Rule::Or(_)));
assert_eq!(p.occurances(&content), 3);
This matches lines containing "dreary", "curious", or "tapping".
The all
pattern matches if all of its sub-patterns match:
let p = pat! {
all {
"while",
"pondered",
"weary"
}
};
assert!(matches!(p, Rule::And(_)));
assert_eq!(p.occurances(&content), 1);
This matches only if "while", "pondered", and "weary" are all present in the content.
The sequence
pattern matches its sub-patterns in order:
let p = pat! {
sequence {
"Once upon",
"midnight",
"dreary"
}
};
assert!(matches!(p, Rule::Sequence(_)));
assert_eq!(p.occurances(&content), 1);
This matches the patterns "Once upon", "midnight", and "dreary" in that specific order.
You can nest patterns for more complex matching:
let p = pat! {
any {
all {
"dreary",
"weary"
},
sequence {
"tapping",
"rapping"
}
}
};
assert!(matches!(p, Rule::Or(_)));
assert_eq!(p.occurances(&content), 2);
This matches either (both "dreary" and "weary") or ("tapping" followed by "rapping").
Here's a more complex example combining various pattern types:
let p = pat! {
all {
any {
"dreary",
"curious"
},
sequence {
"while"i,
pat!("I \\w+") // Matches "I" followed by any word
},
pat!(".*more.$") // Matches lines ending with "more."
}
};
assert!(matches!(p, Rule::And(_)));
assert_eq!(p.occurances(&content), 1);
This pattern matches content that:
This DSL provides a flexible and intuitive way to create complex regex patterns in Rust. By combining different pattern types and nesting them, you can create powerful search patterns tailored to your specific needs.
For more detailed information on available methods and advanced usage, please refer to the API documentation.