diff --git a/quickfix-msg-gen/src/lib.rs b/quickfix-msg-gen/src/lib.rs index dc54353..7fc7a69 100644 --- a/quickfix-msg-gen/src/lib.rs +++ b/quickfix-msg-gen/src/lib.rs @@ -94,6 +94,21 @@ fn generate_root(output: &mut String, begin_string: &str) { impl std::error::Error for FixParseError {{}} + pub struct GroupIterator<'a, T, I> {{ + parent: &'a T, + clone_group_func: fn(&'a T, usize) -> Option, + current_index: usize, + }} + + impl<'a, T, I> Iterator for GroupIterator<'a, T, I> {{ + type Item = I; + + fn next(&mut self) -> Option {{ + self.current_index += 1; + (self.clone_group_func)(self.parent, self.current_index) + }} + }} + "# )) } @@ -725,6 +740,15 @@ fn generate_group_reader(output: &mut String, struct_name: &str, group: &Message .map(|raw_group| {group_type} {{ inner: raw_group }}) }} + #[inline(always)] + pub fn iter_{fun_name_suffix}(&self) -> GroupIterator<'_, Self, {group_type}> {{ + GroupIterator {{ + parent: self, + clone_group_func: |parent, idx| parent.clone_group_{fun_name_suffix}(idx), + current_index: 0, + }} + }} + "# )); } diff --git a/quickfix-msg40/tests/test_usage.rs b/quickfix-msg40/tests/test_usage.rs index b67980e..aaea415 100644 --- a/quickfix-msg40/tests/test_usage.rs +++ b/quickfix-msg40/tests/test_usage.rs @@ -161,3 +161,56 @@ fn test_build_list_status() -> Result<(), QuickFixError> { Ok(()) } + +#[test] +fn test_group_iterator() -> Result<(), QuickFixError> { + let mut obj = ListStatus::try_new("My list".to_string(), 0, 0)?; + + obj.add_no_orders(list_status::NoOrders::try_new( + "Order:10000".to_string(), + 100, + 50, + 18.5, + )?)?; + obj.add_no_orders(list_status::NoOrders::try_new( + "Order:10001".to_string(), + 89, + 75, + 987.4, + )?)?; + obj.add_no_orders(list_status::NoOrders::try_new( + "Order:10018".to_string(), + 5, + 79, + 5.6, + )?)?; + + // Check we have inserted something + assert_eq!(obj.get_no_orders_count(), 3); + + // Iterate over group + + let output: Vec<_> = obj + .iter_no_orders() + .map(|no_order| { + ( + no_order.get_cl_ord_id(), + no_order.get_cum_qty(), + no_order.get_cxl_qty(), + no_order.get_avg_px(), + ) + }) + .collect(); + + // Check what we have collected + assert_eq!( + output, + vec![ + ("Order:10000".to_string(), 100, 50, 18.5), + ("Order:10001".to_string(), 89, 75, 987.4), + ("Order:10018".to_string(), 5, 79, 5.6) + ] + ); + + Ok(()) +}