use anyhow
This commit is contained in:
38
src/main.rs
38
src/main.rs
@@ -10,23 +10,9 @@ use std::time::SystemTime;
|
||||
use std::{thread, time::Duration};
|
||||
use sysinfo::{System, SystemExt};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use std::{env, fs, path::PathBuf, process::Command};
|
||||
|
||||
trait ErrorToString {
|
||||
type Output;
|
||||
fn str_err(self) -> std::result::Result<Self::Output, String>;
|
||||
}
|
||||
|
||||
impl<T, E> ErrorToString for std::result::Result<T, E>
|
||||
where
|
||||
E: std::error::Error,
|
||||
{
|
||||
type Output = T;
|
||||
fn str_err(self) -> std::result::Result<Self::Output, String> {
|
||||
self.map_err(|e| e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Time with hours (24) and minutes (60)
|
||||
#[derive(Deserialize, Default, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Debug)]
|
||||
#[serde(try_from = "String")]
|
||||
@@ -47,21 +33,21 @@ impl<Tz: TimeZone> From<DateTime<Tz>> for TimeHM {
|
||||
}
|
||||
|
||||
impl FromStr for TimeHM {
|
||||
type Err = String;
|
||||
type Err = anyhow::Error;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
fn from_str(s: &str) -> Result<Self> {
|
||||
if let Some((str_hour, str_minute)) = s.split(':').collect_tuple() {
|
||||
let hour: u8 = str_hour.parse().str_err()?;
|
||||
let minute: u8 = str_minute.parse().str_err()?;
|
||||
let hour: u8 = str_hour.parse()?;
|
||||
let minute: u8 = str_minute.parse()?;
|
||||
Ok(TimeHM { hour, minute })
|
||||
} else {
|
||||
Err("invalid time, must be hh:mm".into())
|
||||
Err(anyhow!("invalid time, must be hh:mm"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<String> for TimeHM {
|
||||
type Error = String;
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
value.parse()
|
||||
@@ -104,13 +90,13 @@ fn default_true() -> bool {
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn read<P: AsRef<Path>>(path: P) -> Result<Self, String> {
|
||||
let data = fs::read_to_string(path).map_err(|e| format!("can't read config: {:?}", e))?;
|
||||
toml::from_str(&data).map_err(|e| format!("can't parse config: {:?}", e))
|
||||
fn read<P: AsRef<Path>>(path: P) -> Result<Self> {
|
||||
let data = fs::read_to_string(path).map_err(|e| anyhow!("can't read config: {:?}", e))?;
|
||||
toml::from_str(&data).map_err(|e| anyhow!("can't parse config: {:?}", e))
|
||||
}
|
||||
fn file_location() -> Result<PathBuf, String> {
|
||||
fn file_location() -> Result<PathBuf> {
|
||||
let res = env::current_exe()
|
||||
.map_err(|e| format!("can't get current exe path: {:?}", e))?
|
||||
.map_err(|e| anyhow!("can't get current exe path: {:?}", e))?
|
||||
.with_extension("toml");
|
||||
Ok(res)
|
||||
}
|
||||
|
Reference in New Issue
Block a user