patternhunt/lib.rs
1// lib.rs
2#![forbid(unsafe_code)]
3
4#[cfg(feature = "async")]
5pub mod async_glob;
6pub mod batch_io;
7pub mod error;
8pub mod options;
9pub mod patterns;
10pub mod predicates;
11pub mod sync;
12pub mod windows;
13
14pub use crate::error::GlobError;
15pub use crate::options::{GlobOptions, GlobOptionsBuilder};
16pub use crate::patterns::Patterns;
17pub use crate::predicates::Predicates;
18
19use std::path::PathBuf;
20
21/// Main facade for the PatternHunt library
22///
23/// This struct provides high-level APIs for both synchronous
24/// and asynchronous file globbing with pattern matching.
25pub struct PatternHunt;
26
27impl PatternHunt {
28 /// Performs synchronous glob pattern matching
29 ///
30 /// This method searches for files matching the specified patterns
31 /// in the given root directories, with configurable options.
32 ///
33 /// # Arguments
34 ///
35 /// * `patterns` - Array of pattern strings to match
36 /// * `roots` - Array of root directories to search in
37 /// * `opts` - Configuration options for globbing
38 ///
39 /// # Returns
40 ///
41 /// `Ok(Vec<PathBuf>)` with matching paths, or `Err(GlobError)` on failure
42 ///
43 /// # Examples
44 ///
45 /// ```
46 /// use patternhunt::{PatternHunt, GlobOptions};
47 ///
48 /// let options = GlobOptions::default();
49 /// let results = PatternHunt::sync(
50 /// &["*.txt", "*.md"],
51 /// &["."],
52 /// options
53 /// ).unwrap();
54 /// ```
55 pub fn sync(
56 patterns: &[&str],
57 roots: &[&str],
58 opts: GlobOptions,
59 ) -> Result<Vec<PathBuf>, GlobError> {
60 let pats = Patterns::compile_many(patterns, &opts)?;
61 let preds = opts.predicates.clone();
62 let mut results = Vec::new();
63
64 // Process each root directory
65 for r in roots {
66 let _root = std::path::Path::new(r);
67 let mut v = crate::sync::glob_sync(pats.clone(), opts.clone(), preds.clone())?;
68 results.append(&mut v);
69 }
70
71 Ok(results)
72 }
73
74 /// Creates a stream of results for asynchronous glob pattern matching
75 ///
76 /// This method returns a stream that asynchronously yields matching
77 /// paths, suitable for use with async runtimes like Tokio.
78 ///
79 /// # Arguments
80 ///
81 /// * `patterns` - Array of pattern strings to match
82 /// * `roots` - Array of root directories to search in
83 /// * `opts` - Configuration options for globbing
84 ///
85 /// # Returns
86 ///
87 /// `Ok(impl Stream<Item = Result<PathBuf, GlobError>>)` on success,
88 /// or `Err(GlobError)` if pattern compilation fails
89 ///
90 /// # Note
91 ///
92 /// Currently supports single-root operations. For multiple roots,
93 /// consumers should call this method for each root directory.
94 #[cfg(feature = "async")]
95 pub fn stream(
96 patterns: &[&str],
97 _roots: &[&str],
98 opts: GlobOptions,
99 ) -> Result<impl futures::Stream<Item = Result<PathBuf, GlobError>>, GlobError> {
100 let pats = Patterns::compile_many(patterns, &opts)?;
101 let preds = opts.predicates.clone();
102
103 // Simple single-root support for facade
104 // Consumer can call for each root if needed
105 Ok(crate::async_glob::glob_stream(pats, opts, preds))
106 }
107}