diff --git a/src/api.rs b/src/api.rs
index 40d50a2..9b6b387 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -7,8 +7,6 @@ use serde::Deserialize;
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use std::thread;
-use std::thread::sleep;
-use std::thread::Thread;
use std::time::{Duration, Instant};
pub trait Timestamped {
diff --git a/src/config.rs b/src/config.rs
index 9743f23..59a8be1 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -17,7 +17,7 @@ pub struct Config {
impl Default for Config {
fn default() -> Config {
Config {
- api_key: "E3GrOiQAnY61BP623XXzt9Fo87A1IQrS1FFzD57P".to_owned(),
+ api_key: "".to_owned(),
tags_url: "https://platform.yepzon.com/tags".to_owned(),
states_url: "https://platform.yepzon.com/tags/{tag}/states".to_owned(),
@@ -26,7 +26,7 @@ impl Default for Config {
timestamp_format: "%Y-%m-%dT%H:%M:%S%.fZ".to_owned(),
between_format: "%d.%m.%Y %H:%M:%S".to_owned(),
- throttle: 10,
+ throttle: 9,
}
}
}
diff --git a/src/errors.rs b/src/errors.rs
index bb42763..e63d946 100644
--- a/src/errors.rs
+++ b/src/errors.rs
@@ -1,4 +1,5 @@
use super::api::ErrorModel;
+use std::fmt::{Display, Formatter};
use std::io;
#[derive(Debug)]
@@ -9,7 +10,7 @@ pub enum GenericError {
ChronoParseError(chrono::ParseError),
IOError(io::Error),
FromUTF8Error(std::string::FromUtf8Error),
- OutOfLocations,
+ MessagedError(String, Box),
}
impl From for GenericError {
@@ -47,3 +48,31 @@ impl From for GenericError {
GenericError::FromUTF8Error(error)
}
}
+
+impl Display for GenericError {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
+ let err = match self {
+ GenericError::ChronoParseError(_) => "Chrono parse error".to_owned(),
+ GenericError::FromUTF8Error(_) => "UTF8 error".to_owned(),
+ GenericError::IOError(e) => format!("IO error: {}", e),
+ GenericError::MinreqError(_) => "Minreq error".to_owned(),
+ GenericError::TomlError(_) => "Toml error".to_owned(),
+ GenericError::YepzonServerError(_) => "Yepzon server error".to_owned(),
+ GenericError::MessagedError(msg, err) => format!("{}\n {}", msg, err),
+ };
+ write!(f, "{}", err)
+ }
+}
+
+pub trait MessagedError {
+ fn with_msg>(self, text: T) -> Result;
+}
+
+impl> MessagedError for Result {
+ fn with_msg>(self, text: T) -> Result {
+ match self {
+ Ok(ok) => Ok(ok),
+ Err(e) => Err(GenericError::MessagedError(text.into(), Box::new(e.into()))),
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index a54f4a9..2f6da32 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,7 +7,7 @@ use api::API;
use chrono::NaiveDateTime;
use cmd::*;
use config::Config;
-use errors::GenericError;
+use errors::{GenericError, MessagedError};
use std::fs::File;
use std::io::prelude::*;
use std::sync::mpsc::TryRecvError;
@@ -15,19 +15,21 @@ use std::sync::mpsc::TryRecvError;
fn main() {
let env: EnvOpt = argh::from_env();
if let Err(e) = from_env(env) {
- eprintln!("Error: {:?}", e);
+ eprintln!("Critical Error: {}", e);
std::process::exit(1);
}
}
fn from_env(env: EnvOpt) -> Result<(), GenericError> {
match env.subcommand {
- Subcommand::Run(opt) => {
- let mut file = File::open("config.toml")?;
+ Subcommand::Run(_) => {
+ let mut file = File::open("config.toml").with_msg("Could not find config.toml")?;
let mut string = String::new();
- file.read_to_string(&mut string)?;
+ file.read_to_string(&mut string)
+ .with_msg("config.toml is not valid UTF-8")?;
- let config: Config = toml::from_str(&string)?;
+ let config: Config =
+ toml::from_str(&string).with_msg("config.toml is not a valid config file")?;
run(
&config,
@@ -36,11 +38,12 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> {
)?;
Ok(())
}
- Subcommand::Init(opt) => {
+ Subcommand::Init(_) => {
let config = Config::default();
let mut file = File::create("config.toml").unwrap();
- file.write_all(&toml::to_vec(&config).unwrap())?;
+ file.write_all(&toml::to_vec(&config).unwrap())
+ .with_msg("Could not write config.toml, make sure you have correct permissions.")?;
Ok(())
}
}
@@ -75,7 +78,10 @@ fn run(config: &Config, from: Option, to: Option) -> Result<(),
);
match loc {
Ok(mut loc) => locations.append(&mut loc),
- Err(e) => println!("Error: {}", e.message.unwrap_or(String::new())),
+ Err(e) => println!(
+ "Error fetching location data: {}",
+ e.message.unwrap_or(String::new())
+ ),
}
}
Err(e) => eprintln!("{:?}", e),