batch_mode_batch_index/
batch_index.rs

1// ---------------- [ File: batch-mode-batch-index/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 From<u64> for BatchIndex {
23    fn from(value: u64) -> Self {
24        BatchIndex::Usize(value as usize)
25    }
26}
27
28impl BatchIndex {
29    /// Returns `Some(u64)` if this index is a `Usize(u)`, else returns `None` if it’s a UUID.
30    pub fn as_u64(&self) -> Option<u64> {
31        match self {
32            BatchIndex::Usize(u) => Some(*u as u64),
33            BatchIndex::Uuid(_)  => None,
34        }
35    }
36}
37
38impl Display for BatchIndex {
39    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
40        match self {
41            BatchIndex::Usize(value) => write!(f, "{}", value),
42            BatchIndex::Uuid(value) => write!(f, "{}", value),
43        }
44    }
45}
46
47#[cfg(test)]
48mod verify_batch_index {
49    use super::*;
50
51    #[traced_test]
52    fn verify_file_pattern_for_usize() {
53        info!("Starting test: verify_file_pattern_for_usize (BatchIndex)");
54        let index = BatchIndex::Usize(4);
55        let regex = index.file_pattern();
56
57        debug!("Asserting valid matches for a specific integer-based index...");
58        assert!(regex.is_match("batch_input_4.jsonl"));
59        assert!(regex.is_match("batch_output_4.jsonl"));
60        assert!(regex.is_match("batch_error_4.jsonl"));
61
62        debug!("Asserting invalid matches for a specific integer-based index...");
63        assert!(!regex.is_match("batch_input_5.jsonl"), "Should not match 5");
64        assert!(!regex.is_match("batch_unknown_4.jsonl"), "Unknown type should fail");
65        assert!(!regex.is_match("batch_input_4.txt"), "Wrong extension");
66        assert!(!regex.is_match("input_batch_4.jsonl"), "Wrong prefix order");
67
68        info!("Finished test: verify_file_pattern_for_usize (BatchIndex)");
69    }
70
71    #[traced_test]
72    fn verify_file_pattern_for_uuid() {
73        info!("Starting test: verify_file_pattern_for_uuid (BatchIndex)");
74        let index = BatchIndex::from_uuid_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
75        let regex = index.file_pattern();
76
77        debug!("Asserting valid matches for a specific UUID-based index...");
78        assert!(regex.is_match("batch_input_550e8400-e29b-41d4-a716-446655440000.jsonl"));
79        assert!(regex.is_match("batch_output_550e8400-e29b-41d4-a716-446655440000.jsonl"));
80        assert!(regex.is_match("batch_error_550e8400-e29b-41d4-a716-446655440000.jsonl"));
81
82        debug!("Asserting invalid matches for a specific UUID-based index...");
83        assert!(!regex.is_match("batch_input_123e4567-e89b-12d3-a456-426655440000.jsonl"), "Mismatched UUID");
84        assert!(!regex.is_match("batch_error_550e8400e29b41d4a716446655440000.jsonl"), "Missing dashes");
85        assert!(!regex.is_match("batch_error_550e8400-e29b-41d4-a716-446655440000.txt"), "Wrong extension");
86
87        info!("Finished test: verify_file_pattern_for_uuid (BatchIndex)");
88    }
89
90    #[traced_test]
91    fn verify_file_pattern_edge_cases() {
92        info!("Starting test: verify_file_pattern_edge_cases (BatchIndex)");
93
94        debug!("Checking edge cases for integer-based index 0...");
95        let regex_usize = BatchIndex::Usize(0).file_pattern();
96        assert!(regex_usize.is_match("batch_input_0.jsonl"), "Should match 0");
97        assert!(regex_usize.is_match("batch_error_0.jsonl"), "Should match error file with 0");
98        assert!(!regex_usize.is_match("batch_input_.jsonl"), "Empty numeric part should fail");
99
100        debug!("Checking edge cases for specific UUID...");
101        let regex_uuid = BatchIndex::from_uuid_str("123e4567-e89b-12d3-a456-426655440000").unwrap().file_pattern();
102        assert!(regex_uuid.is_match("batch_input_123e4567-e89b-12d3-a456-426655440000.jsonl"));
103        assert!(!regex_uuid.is_match("batch_input_123e4567e89b12d3a456426655440000.jsonl"), "Missing dashes");
104        assert!(!regex_uuid.is_match("batch_input_123e4567-e89b-12d3-a456-426655440000.txt"), "Wrong extension");
105
106        info!("Finished test: verify_file_pattern_edge_cases (BatchIndex)");
107    }
108
109    #[test]
110    fn test_generate_batch_file_regex_usize() {
111        let index = BatchIndex::Usize(4);
112        let regex = index.file_pattern();
113        assert!(regex.is_match("batch_input_4.jsonl"));
114        assert!(regex.is_match("batch_output_4.jsonl"));
115        assert!(regex.is_match("batch_error_4.jsonl"));
116        assert!(!regex.is_match("batch_input_5.jsonl"));
117        assert!(!regex.is_match("batch_unknown_4.jsonl"));
118        assert!(!regex.is_match("batch_input_4.txt"));
119        assert!(!regex.is_match("input_batch_4.jsonl"));
120    }
121
122    #[test]
123    fn test_generate_batch_file_regex_uuid() {
124        let index = BatchIndex::from_uuid_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
125        let regex = index.file_pattern();
126        assert!(regex.is_match("batch_input_550e8400-e29b-41d4-a716-446655440000.jsonl"));
127        assert!(regex.is_match("batch_output_550e8400-e29b-41d4-a716-446655440000.jsonl"));
128        assert!(regex.is_match("batch_error_550e8400-e29b-41d4-a716-446655440000.jsonl"));
129        assert!(!regex.is_match("batch_input_123e4567-e89b-12d3-a456-426655440000.jsonl"));
130        assert!(!regex.is_match("batch_error_zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz.jsonl"));
131        assert!(!regex.is_match("batch_output_550e8400e29b41d4a716446655440000.jsonl"));
132        assert!(!regex.is_match("batch_error_550e8400-e29b-41d4-a716-446655440000.txt"));
133    }
134
135    #[test]
136    fn test_generate_batch_file_regex_edge_cases() {
137        // Testing edge cases for both Usize and UUID patterns
138        let regex_usize = BatchIndex::Usize(0).file_pattern();
139        assert!(regex_usize.is_match("batch_input_0.jsonl"));
140        assert!(regex_usize.is_match("batch_output_0.jsonl"));
141        assert!(regex_usize.is_match("batch_error_0.jsonl"));
142        assert!(!regex_usize.is_match("batch_input_1.jsonl"));
143        assert!(!regex_usize.is_match("batch_input_.json"));
144
145        let regex_uuid = BatchIndex::from_uuid_str("123e4567-e89b-12d3-a456-426655440000").unwrap().file_pattern();
146        assert!(regex_uuid.is_match("batch_input_123e4567-e89b-12d3-a456-426655440000.jsonl"));
147        assert!(regex_uuid.is_match("batch_output_123e4567-e89b-12d3-a456-426655440000.jsonl"));
148        assert!(regex_uuid.is_match("batch_error_123e4567-e89b-12d3-a456-426655440000.jsonl"));
149        assert!(!regex_uuid.is_match("batch_input_123e4567-e89b-12d3-a456-42665544000.jsonl"));
150        assert!(!regex_uuid.is_match("batch_input_123e4567e89b12d3a456426655440000.jsonl"));
151        assert!(!regex_uuid.is_match("batch_input_123e4567-e89b-12d3-a456-426655440000.txt"));
152    }
153}