Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent CLI to panic when the pipe is broken, fixes #5 #29

Merged
merged 5 commits into from
Oct 12, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
jnioche marked this conversation as resolved.
Show resolved Hide resolved
} else {
eprintln!("{}", result.unwrap_err());
process::exit(1);
Expand Down
Loading