@@ -36,30 +36,48 @@ pub struct BatteryPool {
3636impl BatteryPool {
3737 /// Creates a new `BatteryPool` instance with the given component IDs,
3838 /// client and logical meter handles.
39- pub ( crate ) fn new (
39+ pub ( crate ) fn try_new (
4040 component_ids : Option < BTreeSet < u64 > > ,
4141 client : MicrogridClientHandle ,
4242 logical_meter : LogicalMeterHandle ,
43- ) -> Self {
44- Self {
43+ ) -> Result < Self , Error > {
44+ let this = Self {
4545 component_ids,
4646 client,
4747 logical_meter,
4848 snapshot_tx : None ,
4949 bounds_tx : None ,
50+ } ;
51+ if let Some ( ids) = & this. component_ids {
52+ if ids. is_empty ( ) {
53+ let e = "component_ids cannot be an empty set" . to_string ( ) ;
54+ tracing:: error!( "{e}" ) ;
55+ return Err ( Error :: invalid_component ( e) ) ;
56+ }
57+ // Validate that all provided IDs correspond to batteries in the graph.
58+ if !ids. is_subset ( & this. get_all_battery_ids ( ) ) {
59+ let e = format ! ( "All component_ids {:?} must be batteries." , ids) ;
60+ tracing:: error!( "{e}" ) ;
61+ return Err ( Error :: invalid_component ( e) ) ;
62+ }
5063 }
64+ Ok ( this)
65+ }
66+
67+ fn get_all_battery_ids ( & self ) -> BTreeSet < u64 > {
68+ self . logical_meter
69+ . graph ( )
70+ . components ( )
71+ . filter ( |c| c. category ( ) == ElectricalComponentCategory :: Battery )
72+ . map ( |c| c. id )
73+ . collect ( )
5174 }
5275
5376 pub ( crate ) fn get_battery_ids ( & self ) -> BTreeSet < u64 > {
5477 if let Some ( ids) = & self . component_ids {
5578 ids. clone ( )
5679 } else {
57- self . logical_meter
58- . graph ( )
59- . components ( )
60- . filter ( |c| c. category ( ) == ElectricalComponentCategory :: Battery )
61- . map ( |c| c. id )
62- . collect ( )
80+ self . get_all_battery_ids ( )
6381 }
6482 }
6583
0 commit comments