batch_mode_batch_index/
file_pattern.rs

1// ---------------- [ File: batch-mode-batch-index/src/file_pattern.rs ]
2crate::ix!();
3
4pub trait FilePattern {
5
6    fn file_pattern(&self) -> Regex;
7}
8
9impl FilePattern for BatchIndex {
10
11    /// Constructs a regex pattern for matching batch filenames based on the provided `BatchIndex`.
12    ///
13    /// # Arguments
14    /// * `self` - The `BatchIndex` (either Usize or Uuid) which is used to determine the regex pattern.
15    ///
16    /// # Returns
17    /// * `Regex` - A compiled regex pattern for matching batch file names.
18    ///
19    /// # Panics
20    /// This function will panic if the regex pattern is invalid.
21    fn file_pattern(&self) -> Regex {
22        let index_pattern = match self {
23            BatchIndex::Usize(value) => format!("{}", value),
24            BatchIndex::Uuid(value) => format!("{}", value),
25        };
26
27        Regex::new(&format!(
28            r"^batch_(input|output|error|metadata)_{index_pattern}\.jsonl$",
29            index_pattern = index_pattern
30        ))
31        .expect("Invalid regex pattern")
32    }
33}
34
35impl FilePattern for BatchIndexType {
36
37    /// Constructs a regex pattern for matching batch filenames based on the provided `BatchIndex`.
38    ///
39    /// # Arguments
40    /// * `index_type` - The `BatchIndex` (either Usize or Uuid) which is used to determine the regex pattern.
41    ///
42    /// # Returns
43    /// * `Regex` - A compiled regex pattern for matching batch file names.
44    ///
45    /// # Panics
46    /// This function will panic if the regex pattern is invalid.
47    fn file_pattern(&self) -> Regex {
48        let index_pattern = match self {
49            BatchIndexType::Usize => r"\d+",
50            BatchIndexType::Uuid => r"[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}",
51        };
52
53        Regex::new(&format!(
54            r"batch_(input|output|error|metadata)_{index_pattern}\.jsonl$",
55            index_pattern = index_pattern
56        )).expect("Invalid regex pattern")
57    }
58}