Skip to content

Commit

Permalink
Improve outputs for date ranges, fixes #2
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Nioche <[email protected]>
  • Loading branch information
jnioche committed Nov 21, 2023
1 parent 5583843 commit 0bc2146
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 17 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub async fn get_intensities_region(
regionid: u8,
start: &str,
end: &Option<&str>,
) -> Result<RegionData, ApiError> {
) -> Result<Vec<(NaiveDateTime, i32)>, ApiError> {
if regionid < 1 || regionid > 17 {
return Err(ApiError::Error(
"Invalid regiondid - should be between 1-17".to_string(),
Expand All @@ -181,7 +181,8 @@ pub async fn get_intensities_region(
let path = "regional/intensity/";
let url = format!("{BASE_URL}{path}{start}/{ed}/regionid/{regionid}");

get_intensities(&url).await
let region_data = get_intensities(&url).await?;
to_tuple(region_data)
}

/// ISO8601 format YYYY-MM-DDThh:mmZ
Expand All @@ -192,7 +193,7 @@ pub async fn get_intensities_postcode(
postcode: &str,
start: &str,
end: &Option<&str>,
) -> Result<RegionData, ApiError> {
) -> Result<Vec<(NaiveDateTime, i32)>, ApiError> {
if postcode.len() < 2 || postcode.len() > 4 {
return Err(ApiError::Error("Invalid postcode".to_string()));
}
Expand All @@ -201,8 +202,20 @@ pub async fn get_intensities_postcode(

let path = "regional/intensity/";
let url = format!("{BASE_URL}{path}{start}/{ed}/postcode/{postcode}");
let region_data = get_intensities(&url).await?;
to_tuple(region_data)
}

get_intensities(&url).await
/// converts the values from JSON into a simpler
/// representation Vec<DateTime, float>
fn to_tuple(data: RegionData) -> Result<Vec<(NaiveDateTime, i32)>, ApiError> {
let mut values: Vec<(NaiveDateTime, i32)> = Vec::new();
for d in data.data {
let start_date = parse(&d.from).expect("Unparsable date");
let intensity = d.intensity.forecast;
values.push((start_date, intensity));
}
Ok(values)
}

pub async fn get_intensities(url: &str) -> Result<RegionData, ApiError> {
Expand Down
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use std::process;

use carbonintensity::{
get_intensities_postcode, get_intensities_region, get_intensity_postcode, get_intensity_region,
ApiError, RegionData,
ApiError,
};
use chrono::NaiveDateTime;
use clap::{Parser, Subcommand};

#[derive(Parser)]
Expand Down Expand Up @@ -61,9 +62,11 @@ async fn main() {
}
}

fn handle_results(result: Result<RegionData, ApiError>) {
fn handle_results(result: Result<Vec<(NaiveDateTime, i32)>, ApiError>) {
if result.is_ok() {
println!("{:?}", result.unwrap());
for t in result.unwrap() {
println!("{}, {}", t.0, t.1);
}
} else {
eprintln!("{}", result.unwrap_err());
process::exit(1);
Expand Down

0 comments on commit 0bc2146

Please sign in to comment.