batch_mode_batch_workspace/
find_existing_batch_indices.rs

1// ---------------- [ File: src/find_existing_batch_indices.rs ]
2crate::ix!();
3
4impl BatchWorkspace {
5
6    /// Scans the directory and extracts indices from filenames matching the pattern.
7    pub async fn find_existing_batch_file_indices(self: &Arc<Self>) 
8        -> Result<HashSet<BatchIndex>,BatchWorkspaceError> 
9    {
10        let workdir = self.workdir();
11
12        // Regex to match filenames of the form batch_{input,output,error}_N.jsonl
13        let file_pattern = Regex::new(r"batch_(input|output|error)_(\d+|[a-f0-9\-]{36})\.jsonl$")
14            .expect("Invalid regex pattern");
15
16        let mut indices = HashSet::new();
17
18        let mut dir_entries = fs::read_dir(workdir).await?;
19
20        while let Some(entry) = dir_entries.next_entry().await? {
21            let path = entry.path();
22
23            if let Some(filename) = path.file_name().and_then(|name| name.to_str()) {
24                if let Some(captures) = file_pattern.captures(filename) {
25                    if let Some(index_match) = captures.get(2) {
26                        let index_str = index_match.as_str();
27                        let index = if let Ok(num) = index_str.parse::<usize>() {
28                            BatchIndex::Usize(num)
29                        } else {
30                            BatchIndex::from_uuid_str(index_str)?
31                        };
32                        indices.insert(index);
33                    }
34                }
35            }
36        }
37
38        Ok(indices)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use tokio::fs;
46    use std::path::PathBuf;
47
48    #[tokio::test]
49    async fn test_find_indices() -> Result<(),BatchWorkspaceError> {
50        let workspace = BatchWorkspace::new_mock().await?;
51
52        let indices = workspace.find_existing_batch_file_indices().await?;
53        let mut expected_indices = HashSet::new();
54        expected_indices.insert(BatchIndex::Usize(0));
55        expected_indices.insert(BatchIndex::Usize(1));
56        expected_indices.insert(BatchIndex::Usize(12345));
57        expected_indices.insert(BatchIndex::from_uuid_str("550e8400-e29b-41d4-a716-446655440000").unwrap());
58        expected_indices.insert(BatchIndex::from_uuid_str("f47ac10b-58cc-4372-a567-0e02b2c3d479").unwrap());
59
60        assert_eq!(indices, expected_indices);
61
62        workspace.cleanup_if_temporary().await
63    }
64}