Function filetools::list_directories_with_filter
source · pub async fn list_directories_with_filter<P: AsRef<Path> + Send>(
path: P,
filter: FtFilter
) -> Result<Vec<PathBuf>>
Expand description
Lists directories in a given directory (not including subdirectories) matching a filter pattern.
This pattern can be a String
, PathBuf
, or a regex::Regex
pattern.
§Sync
For the sync
version, see crate::sync::list_directories_with_filter
§Errors
This function will return an error in the following situations:
- The given path is a file and not a directory
- The given path does not exist
§Example
use regex::Regex;
use std::path::PathBuf;
use filetools::{list_directories_with_filter, FtFilter};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let root = "some/path/containing/dirs";
// List all dirs containing the phrase `log`
let mut filter = FtFilter::Raw("log".to_string());
let mut results = list_directories_with_filter(&root, filter).await?;
// List all dirs containing the path segment `files/test`
filter = FtFilter::Path(PathBuf::from("files/test"));
results = list_directories_with_filter(&root, filter).await?;
// List all dirs ending with `_test`
let re = Regex::new(r"(.*)_test").expect("unable to create regex");
filter = FtFilter::Regex(re);
results = list_directories_with_filter(&root, filter).await?;
Ok(())
}