Skip to content

Commit

Permalink
✨ Add group iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurlm committed Oct 7, 2024
1 parent 111c9be commit 29ed450
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
24 changes: 24 additions & 0 deletions quickfix-msg-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I>,
current_index: usize,
}}
impl<'a, T, I> Iterator for GroupIterator<'a, T, I> {{
type Item = I;
fn next(&mut self) -> Option<Self::Item> {{
self.current_index += 1;
(self.clone_group_func)(self.parent, self.current_index)
}}
}}
"#
))
}
Expand Down Expand Up @@ -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,
}}
}}
"#
));
}
Expand Down
53 changes: 53 additions & 0 deletions quickfix-msg40/tests/test_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit 29ed450

Please sign in to comment.