File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11//! Code for handling IDs
22use anyhow:: { Context , Result } ;
3- use indexmap:: IndexSet ;
3+ use indexmap:: { IndexMap , IndexSet } ;
44use std:: borrow:: Borrow ;
55use std:: collections:: HashSet ;
66use std:: fmt:: Display ;
@@ -105,3 +105,12 @@ impl<ID: IDLike> IDCollection<ID> for HashSet<ID> {
105105impl < ID : IDLike > IDCollection < ID > for IndexSet < ID > {
106106 define_id_methods ! ( ) ;
107107}
108+
109+ impl < ID : IDLike , V > IDCollection < ID > for IndexMap < ID , V > {
110+ fn get_id < T : Borrow < str > + Display + ?Sized > ( & self , id : & T ) -> Result < & ID > {
111+ let ( found, _) = self
112+ . get_key_value ( id. borrow ( ) )
113+ . with_context ( || format ! ( "Unknown ID {id} found" ) ) ?;
114+ Ok ( found)
115+ }
116+ }
Original file line number Diff line number Diff line change 22use super :: super :: * ;
33use crate :: agent:: { AgentCommodityPortionsMap , AgentID , AgentMap } ;
44use crate :: commodity:: { CommodityID , CommodityMap , CommodityType } ;
5+ use crate :: id:: IDCollection ;
56use crate :: region:: RegionID ;
67use crate :: year:: parse_year_str;
78use anyhow:: { ensure, Context , Result } ;
6667 for agent_commodity_portion_raw in iter {
6768 // Get agent ID
6869 let agent_id_raw = agent_commodity_portion_raw. agent_id . as_str ( ) ;
69- let ( id, _agent) = agents
70- . get_key_value ( agent_id_raw)
71- . with_context ( || format ! ( "Invalid agent ID {agent_id_raw}" ) ) ?;
70+ let id = agents. get_id ( agent_id_raw) ?;
7271
7372 // Get/create entry for agent
7473 let entry = agent_commodity_portions
7776
7877 // Insert portion for the commodity/year(s)
7978 let commodity_id_raw = agent_commodity_portion_raw. commodity_id . as_str ( ) ;
80- let ( commodity_id, _commodity) = commodities
81- . get_key_value ( commodity_id_raw)
82- . with_context ( || format ! ( "Invalid commodity ID {commodity_id_raw}" ) ) ?;
79+ let commodity_id = commodities. get_id ( commodity_id_raw) ?;
8380 let years = parse_year_str ( & agent_commodity_portion_raw. years , milestone_years) ?;
8481 for year in years {
8582 try_insert (
Original file line number Diff line number Diff line change @@ -57,9 +57,7 @@ impl AgentSearchSpaceRaw {
5757 // Check that the year is a valid milestone year
5858 let year = parse_year_str ( & self . years , milestone_years) ?;
5959
60- let ( agent_id, _) = agents
61- . get_key_value ( self . agent_id . as_str ( ) )
62- . context ( "Invalid agent ID" ) ?;
60+ let agent_id = agents. get_id ( & self . agent_id ) ?;
6361
6462 Ok ( AgentSearchSpace {
6563 agent_id : agent_id. clone ( ) ,
You can’t perform that action at this time.
0 commit comments