batch_mode_batch_index/
batch_index.rs

1// ---------------- [ File: src/batch_index.rs ]
2crate::ix!();
3
4/// Represents the type of index found in the file names.
5#[derive(Serialize,Deserialize,Debug,Clone,PartialEq,Eq,Hash,PartialOrd,Ord)]
6pub enum BatchIndex {
7    Usize(usize),
8    Uuid(Uuid),
9}
10
11impl BatchIndex {
12
13    pub fn new() -> Self {
14        BatchIndex::Uuid(Uuid::new_v4())
15    }
16
17    pub fn from_uuid_str(x: &str) -> Result<Self,UuidParseError> {
18        Ok(BatchIndex::Uuid(Uuid::parse_str(x)?))
19    }
20}
21
22impl Display for BatchIndex {
23    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
24        match self {
25            BatchIndex::Usize(value) => write!(f, "{}", value),
26            BatchIndex::Uuid(value) => write!(f, "{}", value),
27        }
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34    use regex::Regex;
35
36    #[test]
37    fn test_generate_batch_file_regex_usize() {
38        let index = BatchIndex::Usize(4);
39        let regex = index.file_pattern();
40        assert!(regex.is_match("batch_input_4.jsonl"));
41        assert!(regex.is_match("batch_output_4.jsonl"));
42        assert!(regex.is_match("batch_error_4.jsonl"));
43        assert!(!regex.is_match("batch_input_5.jsonl"));
44        assert!(!regex.is_match("batch_unknown_4.jsonl"));
45        assert!(!regex.is_match("batch_input_4.txt"));
46        assert!(!regex.is_match("input_batch_4.jsonl"));
47    }
48
49    #[test]
50    fn test_generate_batch_file_regex_uuid() {
51        let index = BatchIndex::from_uuid_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
52        let regex = index.file_pattern();
53        assert!(regex.is_match("batch_input_550e8400-e29b-41d4-a716-446655440000.jsonl"));
54        assert!(regex.is_match("batch_output_550e8400-e29b-41d4-a716-446655440000.jsonl"));
55        assert!(regex.is_match("batch_error_550e8400-e29b-41d4-a716-446655440000.jsonl"));
56        assert!(!regex.is_match("batch_input_123e4567-e89b-12d3-a456-426655440000.jsonl"));
57        assert!(!regex.is_match("batch_error_zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz.jsonl"));
58        assert!(!regex.is_match("batch_output_550e8400e29b41d4a716446655440000.jsonl"));
59        assert!(!regex.is_match("batch_error_550e8400-e29b-41d4-a716-446655440000.txt"));
60    }
61
62    #[test]
63    fn test_generate_batch_file_regex_edge_cases() {
64        // Testing edge cases for both Usize and UUID patterns
65        let regex_usize = BatchIndex::Usize(0).file_pattern();
66        assert!(regex_usize.is_match("batch_input_0.jsonl"));
67        assert!(regex_usize.is_match("batch_output_0.jsonl"));
68        assert!(regex_usize.is_match("batch_error_0.jsonl"));
69        assert!(!regex_usize.is_match("batch_input_1.jsonl"));
70        assert!(!regex_usize.is_match("batch_input_.json"));
71
72        let regex_uuid = BatchIndex::from_uuid_str("123e4567-e89b-12d3-a456-426655440000").unwrap().file_pattern();
73        assert!(regex_uuid.is_match("batch_input_123e4567-e89b-12d3-a456-426655440000.jsonl"));
74        assert!(regex_uuid.is_match("batch_output_123e4567-e89b-12d3-a456-426655440000.jsonl"));
75        assert!(regex_uuid.is_match("batch_error_123e4567-e89b-12d3-a456-426655440000.jsonl"));
76        assert!(!regex_uuid.is_match("batch_input_123e4567-e89b-12d3-a456-42665544000.jsonl"));
77        assert!(!regex_uuid.is_match("batch_input_123e4567e89b12d3a456426655440000.jsonl"));
78        assert!(!regex_uuid.is_match("batch_input_123e4567-e89b-12d3-a456-426655440000.txt"));
79    }
80}