From e86a9f3ee93d6ea0d19e0af04c5a63b41ca72582 Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 4 Dec 2024 06:28:43 +0800 Subject: refactor: do not refresh documents when `.edit rag-docs` (#1032) --- src/utils/path.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src/utils/path.rs') diff --git a/src/utils/path.rs b/src/utils/path.rs index 635604e..bf3d936 100644 --- a/src/utils/path.rs +++ b/src/utils/path.rs @@ -1,6 +1,7 @@ use std::path::{Component, Path, PathBuf}; use anyhow::{bail, Result}; +use indexmap::IndexSet; pub fn safe_join_path, T2: AsRef>( base_path: T1, @@ -28,8 +29,11 @@ pub fn safe_join_path, T2: AsRef>( } } -pub async fn expand_glob_paths>(paths: &[T]) -> Result> { - let mut new_paths = vec![]; +pub async fn expand_glob_paths>( + paths: &[T], + bail_non_exist: bool, +) -> Result> { + let mut new_paths = IndexSet::new(); for path in paths { let (path_str, suffixes) = parse_glob(path.as_ref())?; let suffixes = if suffixes.is_empty() { @@ -37,7 +41,13 @@ pub async fn expand_glob_paths>(paths: &[T]) -> Result } else { Some(&suffixes) }; - list_files(&mut new_paths, Path::new(&path_str), suffixes).await?; + list_files( + &mut new_paths, + Path::new(&path_str), + suffixes, + bail_non_exist, + ) + .await?; } Ok(new_paths) } @@ -94,19 +104,24 @@ fn parse_glob(path_str: &str) -> Result<(String, Vec)> { #[async_recursion::async_recursion] async fn list_files( - files: &mut Vec, + files: &mut IndexSet, entry_path: &Path, suffixes: Option<&Vec>, + bail_non_exist: bool, ) -> Result<()> { if !entry_path.exists() { - bail!("Not found '{}'", entry_path.display()); + if bail_non_exist { + bail!("Not found '{}'", entry_path.display()); + } else { + return Ok(()); + } } if entry_path.is_dir() { let mut reader = tokio::fs::read_dir(entry_path).await?; while let Some(entry) = reader.next_entry().await? { let path = entry.path(); if path.is_dir() { - list_files(files, &path, suffixes).await?; + list_files(files, &path, suffixes, bail_non_exist).await?; } else { add_file(files, suffixes, &path); } @@ -117,11 +132,11 @@ async fn list_files( Ok(()) } -fn add_file(files: &mut Vec, suffixes: Option<&Vec>, path: &Path) { +fn add_file(files: &mut IndexSet, suffixes: Option<&Vec>, path: &Path) { if is_valid_extension(suffixes, path) { let path = path.display().to_string(); if !files.contains(&path) { - files.push(path); + files.insert(path); } } } -- cgit v1.2.3