diff --git a/pywr-schema/src/parameters/core.rs b/pywr-schema/src/parameters/core.rs index e65c4ff8..e6aac1ce 100644 --- a/pywr-schema/src/parameters/core.rs +++ b/pywr-schema/src/parameters/core.rs @@ -260,17 +260,7 @@ impl TryFromV1Parameter for MaxParameter { /// # Examples /// /// ```json -/// { -/// "type": "Division", -/// "numerator": { -/// "type": "MonthlyProfile", -/// "values": [1, 4, 5, 9, 1, 5, 10, 8, 11, 9, 11 ,12] -/// }, -/// "denominator": { -/// "type": "Constant", -/// "value": 0.3 -/// } -/// } +#[doc = include_str!("doc_examples/division.json")] /// ``` #[derive(serde::Deserialize, serde::Serialize, Debug, Clone)] pub struct DivisionParameter { @@ -330,14 +320,7 @@ impl TryFromV1Parameter for DivisionParameter { /// # Examples /// /// ```json -/// { -/// "type": "Min", -/// "parameter": { -/// "type": "MonthlyProfile", -/// "values": [1, 4, 5, 9, 1, 5, 10, 8, 11, 9, 11 ,12] -/// }, -/// "threshold": 2 -/// } +#[doc = include_str!("doc_examples/min.json")] /// ``` #[derive(serde::Deserialize, serde::Serialize, Debug, Clone)] pub struct MinParameter { @@ -442,14 +425,7 @@ impl TryFromV1Parameter for NegativeParameter { /// # Examples /// /// ```json -/// { -/// "type": "NegativeMax", -/// "metric": { -/// "type": "MonthlyProfile", -/// "values": [-1, -4, 5, 9, 1, 5, 10, 8, 11, 9, 11 ,12] -/// }, -/// "threshold": 2 -/// } +#[doc = include_str!("doc_examples/negative_max.json")] /// ``` /// In January this parameter returns 2, in February 4. /// @@ -508,14 +484,7 @@ impl TryFromV1Parameter for NegativeMaxParameter { /// # Examples /// /// ```json -/// { -/// "type": "NegativeMin", -/// "metric": { -/// "type": "MonthlyProfile", -/// "values": [-1, -4, 5, 9, 1, 5, 10, 8, 11, 9, 11 ,12] -/// }, -/// "threshold": 2 -/// } +#[doc = include_str!("doc_examples/negative_min.json")] /// ``` /// In January this parameter returns 1, in February 2. /// diff --git a/pywr-schema/src/parameters/doc_examples/division.json b/pywr-schema/src/parameters/doc_examples/division.json new file mode 100644 index 00000000..7d6acc12 --- /dev/null +++ b/pywr-schema/src/parameters/doc_examples/division.json @@ -0,0 +1,29 @@ +{ + "name": "my-division", + "type": "Division", + "numerator": { + "type": "InlineParameter", + "definition": { + "name": "my-monthly-profile", + "type": "MonthlyProfile", + "values": [ + 1, + 4, + 5, + 9, + 1, + 5, + 10, + 8, + 11, + 9, + 11, + 12 + ] + } + }, + "denominator": { + "type": "Constant", + "value": 0.3 + } +} diff --git a/pywr-schema/src/parameters/doc_examples/min.json b/pywr-schema/src/parameters/doc_examples/min.json new file mode 100644 index 00000000..7c439f50 --- /dev/null +++ b/pywr-schema/src/parameters/doc_examples/min.json @@ -0,0 +1,26 @@ +{ + "name": "my-min", + "type": "Min", + "parameter": { + "type": "InlineParameter", + "definition": { + "name": "my-monthly-profile", + "type": "MonthlyProfile", + "values": [ + 1, + 4, + 5, + 9, + 1, + 5, + 10, + 8, + 11, + 9, + 11, + 12 + ] + } + }, + "threshold": 2 +} diff --git a/pywr-schema/src/parameters/doc_examples/negative_max.json b/pywr-schema/src/parameters/doc_examples/negative_max.json new file mode 100644 index 00000000..3d81a1fd --- /dev/null +++ b/pywr-schema/src/parameters/doc_examples/negative_max.json @@ -0,0 +1,26 @@ +{ + "name": "my-negative-max", + "type": "NegativeMax", + "metric": { + "type": "InlineParameter", + "definition": { + "name": "my-monthly-profile", + "type": "MonthlyProfile", + "values": [ + -1, + -4, + 5, + 9, + 1, + 5, + 10, + 8, + 11, + 9, + 11, + 12 + ] + } + }, + "threshold": 2 +} diff --git a/pywr-schema/src/parameters/doc_examples/negative_min.json b/pywr-schema/src/parameters/doc_examples/negative_min.json new file mode 100644 index 00000000..22c61bb1 --- /dev/null +++ b/pywr-schema/src/parameters/doc_examples/negative_min.json @@ -0,0 +1,26 @@ +{ + "name": "my-negative-min", + "type": "NegativeMin", + "metric": { + "type": "InlineParameter", + "definition": { + "name": "my-monthly-profile", + "type": "MonthlyProfile", + "values": [ + -1, + -4, + 5, + 9, + 1, + 5, + 10, + 8, + 11, + 9, + 11, + 12 + ] + } + }, + "threshold": 2 +} diff --git a/pywr-schema/src/parameters/mod.rs b/pywr-schema/src/parameters/mod.rs index ff1d9e27..65b59fdd 100644 --- a/pywr-schema/src/parameters/mod.rs +++ b/pywr-schema/src/parameters/mod.rs @@ -744,7 +744,7 @@ mod tests { use std::fs; use std::path::PathBuf; - /// Test all of the documentation examples successfully deserialize. + /// Test all the documentation examples successfully deserialize. #[test] fn test_doc_examples() { let mut doc_examples = PathBuf::from(env!("CARGO_MANIFEST_DIR")); @@ -753,8 +753,9 @@ mod tests { for entry in fs::read_dir(doc_examples).unwrap() { let p = entry.unwrap().path(); if p.is_file() { - let data = fs::read_to_string(p).unwrap(); - let _: Parameter = serde_json::from_str(&data).unwrap(); + let data = fs::read_to_string(&p).unwrap_or_else(|_| panic!("Failed to read file: {:?}", p)); + let _: Parameter = + serde_json::from_str(&data).unwrap_or_else(|_| panic!("Failed to deserialize: {:?}", p)); } } }