High-performance implementation of plcbundle written in Rust
at main 169 lines 4.4 kB view raw
1//! Processor options and builder, including `QueryMode` for simple path vs JMESPath queries 2use std::path::PathBuf; 3 4/// Configuration options for the PLC bundle processor 5#[derive(Debug, Clone)] 6pub struct Options { 7 /// Directory containing bundle files 8 pub directory: PathBuf, 9 /// Query expression (JMESPath or simple path) 10 pub query: String, 11 /// Query mode (simple or jmespath) 12 pub query_mode: QueryMode, 13 /// Number of worker threads (0 = auto) 14 pub num_threads: usize, 15 /// Output batch size (number of lines before flush) 16 pub batch_size: usize, 17} 18 19#[derive(Debug, Clone, Copy, PartialEq, Eq)] 20pub enum QueryMode { 21 Simple, 22 JmesPath, 23} 24 25impl Default for Options { 26 fn default() -> Self { 27 Self { 28 directory: PathBuf::from("."), 29 query: String::from("did"), 30 query_mode: QueryMode::Simple, 31 num_threads: 0, 32 batch_size: 2000, 33 } 34 } 35} 36 37/// Builder for Options 38pub struct OptionsBuilder { 39 options: Options, 40} 41 42impl OptionsBuilder { 43 pub fn new() -> Self { 44 Self { 45 options: Options::default(), 46 } 47 } 48 49 pub fn directory<P: Into<PathBuf>>(mut self, dir: P) -> Self { 50 self.options.directory = dir.into(); 51 self 52 } 53 54 pub fn query<S: Into<String>>(mut self, query: S) -> Self { 55 self.options.query = query.into(); 56 self 57 } 58 59 pub fn query_mode(mut self, mode: QueryMode) -> Self { 60 self.options.query_mode = mode; 61 self 62 } 63 64 pub fn num_threads(mut self, n: usize) -> Self { 65 self.options.num_threads = n; 66 self 67 } 68 69 pub fn batch_size(mut self, size: usize) -> Self { 70 self.options.batch_size = size; 71 self 72 } 73 74 pub fn build(self) -> Options { 75 self.options 76 } 77} 78 79impl Default for OptionsBuilder { 80 fn default() -> Self { 81 Self::new() 82 } 83} 84 85#[cfg(test)] 86mod tests { 87 use super::*; 88 89 #[test] 90 fn test_options_default() { 91 let opts = Options::default(); 92 assert_eq!(opts.directory, PathBuf::from(".")); 93 assert_eq!(opts.query, "did"); 94 assert_eq!(opts.query_mode, QueryMode::Simple); 95 assert_eq!(opts.num_threads, 0); 96 assert_eq!(opts.batch_size, 2000); 97 } 98 99 #[test] 100 fn test_query_mode_equality() { 101 assert_eq!(QueryMode::Simple, QueryMode::Simple); 102 assert_eq!(QueryMode::JmesPath, QueryMode::JmesPath); 103 assert_ne!(QueryMode::Simple, QueryMode::JmesPath); 104 } 105 106 #[test] 107 fn test_options_builder_new() { 108 let builder = OptionsBuilder::new(); 109 let opts = builder.build(); 110 assert_eq!(opts.query, "did"); 111 assert_eq!(opts.query_mode, QueryMode::Simple); 112 } 113 114 #[test] 115 fn test_options_builder_default() { 116 let builder = OptionsBuilder::default(); 117 let opts = builder.build(); 118 assert_eq!(opts.query, "did"); 119 } 120 121 #[test] 122 fn test_options_builder_directory() { 123 let opts = OptionsBuilder::new().directory("/tmp/test").build(); 124 assert_eq!(opts.directory, PathBuf::from("/tmp/test")); 125 } 126 127 #[test] 128 fn test_options_builder_query() { 129 let opts = OptionsBuilder::new().query("operation.type").build(); 130 assert_eq!(opts.query, "operation.type"); 131 } 132 133 #[test] 134 fn test_options_builder_query_mode() { 135 let opts = OptionsBuilder::new() 136 .query_mode(QueryMode::JmesPath) 137 .build(); 138 assert_eq!(opts.query_mode, QueryMode::JmesPath); 139 } 140 141 #[test] 142 fn test_options_builder_num_threads() { 143 let opts = OptionsBuilder::new().num_threads(4).build(); 144 assert_eq!(opts.num_threads, 4); 145 } 146 147 #[test] 148 fn test_options_builder_batch_size() { 149 let opts = OptionsBuilder::new().batch_size(5000).build(); 150 assert_eq!(opts.batch_size, 5000); 151 } 152 153 #[test] 154 fn test_options_builder_chain() { 155 let opts = OptionsBuilder::new() 156 .directory("/tmp/test") 157 .query("did") 158 .query_mode(QueryMode::JmesPath) 159 .num_threads(8) 160 .batch_size(1000) 161 .build(); 162 163 assert_eq!(opts.directory, PathBuf::from("/tmp/test")); 164 assert_eq!(opts.query, "did"); 165 assert_eq!(opts.query_mode, QueryMode::JmesPath); 166 assert_eq!(opts.num_threads, 8); 167 assert_eq!(opts.batch_size, 1000); 168 } 169}