Skip to content

Commit 61e4a5d

Browse files
committed
Simpler method
1 parent 1e9fd2d commit 61e4a5d

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

src/asset.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ use crate::region::RegionID;
66
use crate::time_slice::TimeSliceID;
77
use anyhow::{ensure, Context, Result};
88
use indexmap::IndexMap;
9+
use serde::{Deserialize, Serialize};
910
use std::hash::{Hash, Hasher};
1011
use std::ops::{Deref, RangeInclusive};
1112
use std::rc::Rc;
1213

1314
/// A unique identifier for an asset
14-
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
15+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
1516
pub struct AssetID(u32);
1617

1718
/// An asset controlled by an agent.
1819
#[derive(Clone, Debug, PartialEq)]
1920
pub struct Asset {
2021
/// A unique identifier for the asset
21-
id: Option<AssetID>,
22+
pub id: Option<AssetID>,
2223
/// A unique identifier for the agent
2324
pub agent_id: AgentID,
2425
/// The [`Process`] that this asset corresponds to
@@ -34,11 +35,6 @@ pub struct Asset {
3435
}
3536

3637
impl Asset {
37-
/// Get the asset's ID as a u32
38-
pub fn get_id(&self) -> Option<u32> {
39-
self.id.map(|AssetID(id)| id)
40-
}
41-
4238
/// Create a new [`Asset`].
4339
///
4440
/// The `id` field is initially set to `None`, but is changed to a unique value when the asset

src/output.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The module responsible for writing output data to disk.
22
use crate::agent::AgentID;
3-
use crate::asset::{Asset, AssetRef};
3+
use crate::asset::{Asset, AssetID, AssetRef};
44
use crate::commodity::CommodityID;
55
use crate::process::ProcessID;
66
use crate::region::RegionID;
@@ -70,7 +70,7 @@ pub fn create_output_directory(output_dir: &Path) -> Result<()> {
7070
#[derive(Serialize, Deserialize, Debug, PartialEq)]
7171
struct AssetRow {
7272
milestone_year: u32,
73-
asset_id: u32,
73+
asset_id: AssetID,
7474
process_id: ProcessID,
7575
region_id: RegionID,
7676
agent_id: AgentID,
@@ -82,7 +82,7 @@ impl AssetRow {
8282
fn new(milestone_year: u32, asset: &Asset) -> Self {
8383
Self {
8484
milestone_year,
85-
asset_id: asset.get_id().unwrap(),
85+
asset_id: asset.id.unwrap(),
8686
process_id: asset.process.id.clone(),
8787
region_id: asset.region_id.clone(),
8888
agent_id: asset.agent_id.clone(),
@@ -95,7 +95,7 @@ impl AssetRow {
9595
#[derive(Serialize, Deserialize, Debug, PartialEq)]
9696
struct CommodityFlowRow {
9797
milestone_year: u32,
98-
asset_id: u32,
98+
asset_id: AssetID,
9999
commodity_id: CommodityID,
100100
time_slice: TimeSliceID,
101101
flow: f64,
@@ -115,7 +115,7 @@ struct CommodityPriceRow {
115115
#[derive(Serialize, Deserialize, Debug, PartialEq)]
116116
struct CapacityDualsRow {
117117
milestone_year: u32,
118-
asset_id: u32,
118+
asset_id: AssetID,
119119
time_slice: TimeSliceID,
120120
value: f64,
121121
}
@@ -183,7 +183,7 @@ impl DebugDataWriter {
183183
for (asset, time_slice, value) in iter {
184184
let row = CapacityDualsRow {
185185
milestone_year,
186-
asset_id: asset.get_id().unwrap(),
186+
asset_id: asset.id.unwrap(),
187187
time_slice: time_slice.clone(),
188188
value,
189189
};
@@ -278,7 +278,7 @@ impl DataWriter {
278278
for ((asset, commodity_id, time_slice), flow) in flow_map {
279279
let row = CommodityFlowRow {
280280
milestone_year,
281-
asset_id: asset.get_id().unwrap(),
281+
asset_id: asset.id.unwrap(),
282282
commodity_id: commodity_id.clone(),
283283
time_slice: time_slice.clone(),
284284
flow: *flow,
@@ -381,7 +381,7 @@ mod tests {
381381
// Read back and compare
382382
let expected = CommodityFlowRow {
383383
milestone_year,
384-
asset_id: asset.get_id().unwrap(),
384+
asset_id: asset.id.unwrap(),
385385
commodity_id,
386386
time_slice,
387387
flow: 42.0,
@@ -486,7 +486,7 @@ mod tests {
486486
// Read back and compare
487487
let expected = CapacityDualsRow {
488488
milestone_year,
489-
asset_id: asset.get_id().unwrap(),
489+
asset_id: asset.id.unwrap(),
490490
time_slice,
491491
value,
492492
};

0 commit comments

Comments
 (0)