diff --git a/src/people.rs b/src/people.rs index 5862b1cd..c4f4d205 100644 --- a/src/people.rs +++ b/src/people.rs @@ -688,6 +688,7 @@ pub trait ContextPeopleExt { fn get_person_id(&self, person_id: usize) -> PersonId; fn index_property(&mut self, property: T); fn query_people(&self, q: T) -> Vec; + fn query_people_count(&self, q: T) -> usize; fn match_person(&self, person_id: PersonId, q: T) -> bool; } @@ -863,6 +864,18 @@ impl ContextPeopleExt for Context { accumulator.people } + fn query_people_count(&self, q: T) -> usize { + // Special case the situation where nobody exists. + if self.get_data_container(PeoplePlugin).is_none() { + return 0 + } + + T::setup(self); + let mut accumulator = PeopleAccumulatorCount::default(); + self.query_people_internal(&mut accumulator, q.get_query()); + accumulator.count + } + fn match_person(&self, person_id: PersonId, q: T) -> bool { T::setup(self); // This cannot fail because someone must have been made by now. @@ -1490,6 +1503,23 @@ mod test { assert_eq!(people.len(), 0); } + #[test] + fn query_people_count() { + let mut context = Context::new(); + let _ = context + .add_person((RiskCategoryType, RiskCategory::High)) + .unwrap(); + + assert_eq!(context.query_people_count((RiskCategoryType, RiskCategory::High)), 1); + } + + #[test] + fn query_people_count_empty() { + let context = Context::new(); + + assert_eq!(context.query_people_count((RiskCategoryType, RiskCategory::High)), 0); + } + #[test] fn query_people_macro_index_first() { let mut context = Context::new();