Skip to content

Commit

Permalink
chore: Upgrade Pywr v1.x schema to v0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jetuk committed Apr 24, 2024
1 parent be33436 commit 7cd980e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ pyo3-log = "0.9.0"
tracing = { version = "0.1", features = ["log"] }
csv = "1.1"
hdf5 = { git = "https://github.com/aldanor/hdf5-rust.git", package = "hdf5", features = ["static", "zlib"] }
pywr-v1-schema = { git = "https://github.com/pywr/pywr-schema/", tag = "v0.11.0", package = "pywr-schema" }
chrono = { version = "0.4.34" }
pywr-v1-schema = { git = "https://github.com/pywr/pywr-schema/", tag = "v0.12.0", package = "pywr-schema" }
chrono = { version = "0.4.34" }
38 changes: 16 additions & 22 deletions pywr-schema/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ pub enum DateType {
DateTime(NaiveDateTime),
}

impl From<pywr_v1_schema::model::DateType> for DateType {
fn from(v1: pywr_v1_schema::model::DateType) -> Self {
match v1 {
pywr_v1_schema::model::DateType::Date(date) => Self::Date(date),
pywr_v1_schema::model::DateType::DateTime(date_time) => Self::DateTime(date_time),
}
}
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct Timestepper {
pub start: DateType,
Expand All @@ -91,25 +100,13 @@ impl Default for Timestepper {
}
}

impl TryFrom<pywr_v1_schema::model::Timestepper> for Timestepper {
type Error = ConversionError;

fn try_from(v1: pywr_v1_schema::model::Timestepper) -> Result<Self, Self::Error> {
let start = DateType::Date(
NaiveDate::from_ymd_opt(v1.start.year(), v1.start.month() as u32, v1.start.day() as u32)
.ok_or(ConversionError::UnparseableDate(v1.start.to_string()))?,
);

let end = DateType::Date(
NaiveDate::from_ymd_opt(v1.end.year(), v1.end.month() as u32, v1.end.day() as u32)
.ok_or(ConversionError::UnparseableDate(v1.end.to_string()))?,
);

Ok(Self {
start,
end,
impl From<pywr_v1_schema::model::Timestepper> for Timestepper {
fn from(v1: pywr_v1_schema::model::Timestepper) -> Self {
Self {
start: v1.start.into(),
end: v1.end.into(),
timestep: v1.timestep.into(),
})
}
}
}

Expand Down Expand Up @@ -457,10 +454,7 @@ impl PywrModel {
Metadata::default()
});

let timestepper = v1.timestepper.try_into().unwrap_or_else(|e| {
errors.push(e);
Timestepper::default()
});
let timestepper = v1.timestepper.into();

// Extract nodes and any timeseries data from the v1 nodes
let nodes_and_ts: Vec<NodeAndTimeseries> = v1
Expand Down
2 changes: 1 addition & 1 deletion pywr-schema/src/nodes/annual_virtual_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl TryFrom<AnnualVirtualStorageNodeV1> for AnnualVirtualStorageNode {
cost,
initial_volume,
reset: AnnualReset {
day: v1.reset_day,
day: v1.reset_day as u8,
month,
use_initial_volume: v1.reset_to_initial_volume,
},
Expand Down

0 comments on commit 7cd980e

Please sign in to comment.