format code

This commit is contained in:
Armaël Guéneau 2024-04-10 18:47:09 +02:00
parent 085755eebc
commit ec1b70d399

View file

@ -3,8 +3,8 @@ use eyre::{OptionExt, WrapErr};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::time::{Duration, SystemTime};
use std::fmt; use std::fmt;
use std::time::{Duration, SystemTime};
const RESTIC_ALARM_BUCKET: &str = "restic-alarm-state"; const RESTIC_ALARM_BUCKET: &str = "restic-alarm-state";
const RESTIC_ALARM_STATE_FILE: &str = "state.toml"; const RESTIC_ALARM_STATE_FILE: &str = "state.toml";
@ -56,19 +56,19 @@ struct AlertStatus {
impl fmt::Display for AlertStatus { impl fmt::Display for AlertStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let days = |d: Duration| { let days = |d: Duration| d.as_secs() / (3600 * 24);
d.as_secs() / (3600 * 24) let hours = |d: Duration| (d.as_secs() / 3600) % 24;
}; write!(
let hours = |d: Duration| { f,
(d.as_secs() / 3600) % 24 "{}: inactivity: {} days, {} hours, last_alert: {}",
}; (if self.alert { "ALERT" } else { "no alert" }),
write!(f, "{}: inactivity: {} days, {} hours, last_alert: {}", days(self.inactivity),
(if self.alert { "ALERT" } else { "no alert" }), hours(self.inactivity),
days(self.inactivity), hours(self.inactivity), match self.last_alert {
match self.last_alert { None => "none".to_owned(),
None => "none".to_owned(), Some(d) => format!("{} days, {} hours", days(d), hours(d)),
Some(d) => format!("{} days, {} hours", days(d), hours(d)) }
}) )
} }
} }
@ -221,7 +221,11 @@ async fn repo_last_snapshot(client: &s3::Client, repo: &str) -> eyre::Result<Opt
// (e.g. if it fails to parse). // (e.g. if it fails to parse).
// So the error must not be propagated to the toplevel, which would abort the // So the error must not be propagated to the toplevel, which would abort the
// alert for remaining repositories; it should instead just be reported/logged. // alert for remaining repositories; it should instead just be reported/logged.
async fn check_repo(client: &s3::Client, state: &mut State, repo: &str) -> eyre::Result<Option<AlertStatus>> { async fn check_repo(
client: &s3::Client,
state: &mut State,
repo: &str,
) -> eyre::Result<Option<AlertStatus>> {
let config = read_repo_config(client, repo).await?; let config = read_repo_config(client, repo).await?;
if let Some(inactivity) = repo_last_snapshot(client, repo).await? { if let Some(inactivity) = repo_last_snapshot(client, repo).await? {
let last_alert = state.last_alert(repo); let last_alert = state.last_alert(repo);
@ -232,7 +236,11 @@ async fn check_repo(client: &s3::Client, state: &mut State, repo: &str) -> eyre:
state.update_last_alert(repo); state.update_last_alert(repo);
write_state(client, state).await?; write_state(client, state).await?;
} }
Ok(Some(AlertStatus { alert, inactivity, last_alert })) Ok(Some(AlertStatus {
alert,
inactivity,
last_alert,
}))
} else { } else {
Ok(None) Ok(None)
} }
@ -258,10 +266,10 @@ async fn main() -> eyre::Result<()> {
match check_repo(&client, &mut state, &repo).await { match check_repo(&client, &mut state, &repo).await {
Ok(None) => { Ok(None) => {
println!("{}: no snapshot, skipping", &repo) println!("{}: no snapshot, skipping", &repo)
}, }
Ok(Some(status)) => { Ok(Some(status)) => {
println!("{}: {}", &repo, status); println!("{}: {}", &repo, status);
}, }
Err(err) => { Err(err) => {
// is this the best way to log the error? // is this the best way to log the error?
println!("Error ({}): {:?}", &repo, err) println!("Error ({}): {:?}", &repo, err)