Skip to content

Commit

Permalink
Merge pull request #29 from jnioche/5
Browse files Browse the repository at this point in the history
Prevent CLI to panic when the pipe is broken, fixes #5
  • Loading branch information
jnioche authored Oct 12, 2024
2 parents 9de7240 + f3caa0f commit 4ed6cf8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- prevent start date to be before first data available ([issue #15](https://github.com/jnioche/carbonintensity-api/issues/15))
- Prevent CLI to panic when the pipe is broken ([issue #5](https://github.com/jnioche/carbonintensity-api/issues/5))

### Removed

Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::process;
use carbonintensity::{get_intensities, get_intensity, ApiError, Target};
use chrono::NaiveDateTime;
use clap::Parser;
use std::io::Write;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -41,20 +42,28 @@ async fn main() {
}
}

#[allow(clippy::explicit_write)]
fn handle_results(result: Result<Vec<(NaiveDateTime, i32)>, ApiError>) {
if let Ok(results) = result {
for (time, value) in results {
println!("{}, {}", time, value);
writeln!(std::io::stdout(), "{}, {}", time, value).unwrap_or_default();
}
} else {
eprintln!("{}", result.unwrap_err());
process::exit(1);
}
}

#[allow(clippy::explicit_write)]
fn handle_result(result: Result<i32, ApiError>, target: &Target) {
if result.is_ok() {
println!("Carbon intensity for {}: {:?}", target, result.unwrap());
writeln!(
std::io::stdout(),
"Carbon intensity for {}: {:?}",
target,
result.unwrap()
)
.unwrap_or_default();
} else {
eprintln!("{}", result.unwrap_err());
process::exit(1);
Expand Down

0 comments on commit 4ed6cf8

Please sign in to comment.