Skip to content

Commit f240b97

Browse files
committed
Amalgamate get_id_by_str with get_id
1 parent 94c80ad commit f240b97

12 files changed

Lines changed: 19 additions & 39 deletions

File tree

src/id.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,39 +75,21 @@ pub(crate) use define_id_getter;
7575

7676
/// A data structure containing a set of IDs
7777
pub trait IDCollection<ID: IDLike> {
78-
/// Get the ID from the collection by its string representation.
79-
///
80-
/// # Arguments
81-
///
82-
/// * `id` - The string representation of the ID
83-
///
84-
/// # Returns
85-
///
86-
/// A copy of the ID in `self`, or an error if not found.
87-
fn get_id_by_str(&self, id: &str) -> Result<ID>;
88-
8978
/// Check if the ID is in the collection, returning a copy of it if found.
9079
///
9180
/// # Arguments
9281
///
93-
/// * `id` - The ID to check
82+
/// * `id` - The ID to check (can be string or ID type)
9483
///
9584
/// # Returns
9685
///
9786
/// A copy of the ID in `self`, or an error if not found.
98-
fn get_id(&self, id: &ID) -> Result<ID>;
87+
fn get_id<T: Borrow<str> + Display + ?Sized>(&self, id: &T) -> Result<ID>;
9988
}
10089

10190
macro_rules! define_id_methods {
10291
() => {
103-
fn get_id_by_str(&self, id: &str) -> Result<ID> {
104-
let found = self
105-
.get(id)
106-
.with_context(|| format!("Unknown ID {id} found"))?;
107-
Ok(found.clone())
108-
}
109-
110-
fn get_id(&self, id: &ID) -> Result<ID> {
92+
fn get_id<T: Borrow<str> + Display + ?Sized>(&self, id: &T) -> Result<ID> {
11193
let found = self
11294
.get(id.borrow())
11395
.with_context(|| format!("Unknown ID {id} found"))?;

src/input/agent/cost_limit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ where
6161
let years = parse_year_str(&agent_cost_limits_raw.years, milestone_years)?;
6262

6363
// Get agent ID
64-
let agent_id = agent_ids.get_id_by_str(&agent_cost_limits_raw.agent_id)?;
64+
let agent_id = agent_ids.get_id(&agent_cost_limits_raw.agent_id)?;
6565

6666
// Get or create entry in the map
6767
let entry = map.entry(agent_id.clone()).or_default();

src/input/agent/search_space.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl AgentSearchSpaceRaw {
5252
let search_space = Rc::new(parse_search_space_str(&self.search_space, process_ids)?);
5353

5454
// Get commodity
55-
let commodity_id = commodity_ids.get_id_by_str(&self.commodity_id)?;
55+
let commodity_id = commodity_ids.get_id(&self.commodity_id)?;
5656

5757
// Check that the year is a valid milestone year
5858
let year = parse_year_str(&self.years, milestone_years)?;
@@ -86,7 +86,7 @@ fn parse_search_space_str(
8686
} else {
8787
search_space
8888
.split(';')
89-
.map(|id| process_ids.get_id_by_str(id.trim()))
89+
.map(|id| process_ids.get_id(id.trim()))
9090
.try_collect()
9191
}
9292
}

src/input/asset.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ where
6969
I: Iterator<Item = AssetRaw>,
7070
{
7171
iter.map(|asset| -> Result<_> {
72-
let agent_id = agent_ids.get_id_by_str(&asset.agent_id)?;
72+
let agent_id = agent_ids.get_id(&asset.agent_id)?;
7373
let process = processes
7474
.get(asset.process_id.as_str())
7575
.with_context(|| format!("Invalid process ID: {}", &asset.process_id))?;
76-
let region_id = region_ids.get_id_by_str(&asset.region_id)?;
76+
let region_id = region_ids.get_id(&asset.region_id)?;
7777

7878
Asset::new(
7979
agent_id,

src/input/commodity/cost.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ where
7878
let mut commodity_regions: HashMap<CommodityID, HashSet<RegionID>> = HashMap::new();
7979

8080
for cost in iter {
81-
let commodity_id = commodity_ids.get_id_by_str(&cost.commodity_id)?;
81+
let commodity_id = commodity_ids.get_id(&cost.commodity_id)?;
8282
let regions = parse_region_str(&cost.regions, region_ids)?;
8383
let years = parse_year_str(&cost.years, milestone_years)?;
8484
let ts_selection = time_slice_info.get_selection(&cost.time_slice)?;

src/input/commodity/demand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ where
110110
let mut map = AnnualDemandMap::new();
111111
for demand in iter {
112112
let commodity_id = svd_commodity_ids
113-
.get_id_by_str(&demand.commodity_id)
113+
.get_id(&demand.commodity_id)
114114
.with_context(|| {
115115
format!(
116116
"Can only provide demand data for SVD commodities. Found entry for '{}'",
117117
demand.commodity_id
118118
)
119119
})?;
120-
let region_id = region_ids.get_id_by_str(&demand.region_id)?;
120+
let region_id = region_ids.get_id(&demand.region_id)?;
121121

122122
ensure!(
123123
milestone_years.binary_search(&demand.year).is_ok(),

src/input/commodity/demand_slicing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ where
6464

6565
for slice in iter {
6666
let commodity_id = svd_commodity_ids
67-
.get_id_by_str(&slice.commodity_id)
67+
.get_id(&slice.commodity_id)
6868
.with_context(|| {
6969
format!(
7070
"Can only provide demand slice data for SVD commodities. Found entry for '{}'",
7171
slice.commodity_id
7272
)
7373
})?;
74-
let region_id = region_ids.get_id_by_str(&slice.region_id)?;
74+
let region_id = region_ids.get_id(&slice.region_id)?;
7575

7676
// We need to know how many time slices are covered by the current demand slice entry and
7777
// how long they are relative to one another so that we can divide up the demand for this

src/input/process/availability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ where
109109
record.validate()?;
110110

111111
// Get process
112-
let id = process_ids.get_id_by_str(&record.process_id)?;
112+
let id = process_ids.get_id(&record.process_id)?;
113113
let process = processes
114114
.get(&id)
115115
.with_context(|| format!("Process {id} not found"))?;

src/input/process/flow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ where
8282
record.validate()?;
8383

8484
// Get process
85-
let id = process_ids.get_id_by_str(&record.process_id)?;
85+
let id = process_ids.get_id(&record.process_id)?;
8686
let process = processes
8787
.get(&id)
8888
.with_context(|| format!("Process {id} not found"))?;

src/input/process/parameter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ where
117117
let mut map: HashMap<ProcessID, ProcessParameterMap> = HashMap::new();
118118
for param_raw in iter {
119119
// Get process
120-
let id = process_ids.get_id_by_str(&param_raw.process_id)?;
120+
let id = process_ids.get_id(&param_raw.process_id)?;
121121
let process = processes
122122
.get(&id)
123123
.with_context(|| format!("Process {id} not found"))?;

0 commit comments

Comments
 (0)