Replace failure with anyhow.
This commit is contained in:
parent
2e956be490
commit
e7b68e6465
6 changed files with 41 additions and 110 deletions
105
Cargo.lock
generated
105
Cargo.lock
generated
|
@ -31,6 +31,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800"
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.66"
|
||||
|
@ -65,29 +71,6 @@ version = "1.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ada4c783bb7e7443c14e0480f429ae2cc99da95065aeab7ee1b81ada0419404f"
|
||||
dependencies = [
|
||||
"autocfg 0.1.4",
|
||||
"backtrace-sys",
|
||||
"cfg-if 0.1.9",
|
||||
"libc",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "backtrace-sys"
|
||||
version = "0.1.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.13.1"
|
||||
|
@ -164,12 +147,6 @@ version = "1.0.79"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
|
@ -295,7 +272,7 @@ version = "0.5.7"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf2b3e8478797446514c91ef04bafcb59faba183e621ad488df88983cc14128c"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
|
@ -305,7 +282,7 @@ version = "0.8.15"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -477,28 +454,6 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "failure"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"failure_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "failure_derive"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"synstructure",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flume"
|
||||
version = "0.10.14"
|
||||
|
@ -632,7 +587,7 @@ version = "0.2.8"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
"libc",
|
||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||
|
@ -803,12 +758,12 @@ checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6"
|
|||
name = "jeethru"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bson",
|
||||
"bus",
|
||||
"chrono",
|
||||
"clap",
|
||||
"config",
|
||||
"failure",
|
||||
"log",
|
||||
"loggerv",
|
||||
"mongodb",
|
||||
|
@ -888,7 +843,7 @@ version = "0.4.17"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1100,7 +1055,7 @@ version = "0.9.7"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall 0.2.16",
|
||||
"smallvec",
|
||||
|
@ -1378,16 +1333,10 @@ version = "0.18.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
"ordered-multimap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7f4dccf6f4891ebcc0c39f9b6eb1a83b9bf5d747cb439ec6fba4f3b977038af"
|
||||
|
||||
[[package]]
|
||||
name = "rustc_version"
|
||||
version = "0.2.3"
|
||||
|
@ -1613,7 +1562,7 @@ version = "0.10.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f5058ada175748e33390e40e872bd0fe59a19f265d0158daa551c5a88a76009c"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
"cpufeatures",
|
||||
"digest",
|
||||
]
|
||||
|
@ -1624,7 +1573,7 @@ version = "0.10.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
"cpufeatures",
|
||||
"digest",
|
||||
]
|
||||
|
@ -1708,18 +1657,6 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "synstructure"
|
||||
version = "0.12.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "take_mut"
|
||||
version = "0.2.2"
|
||||
|
@ -1891,7 +1828,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "9c31f240f59877c3d4bb3b3ea0ec5a6a0cff07323580ff8c7a605cd7d08b255d"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
"data-encoding",
|
||||
"enum-as-inner",
|
||||
"futures-channel",
|
||||
|
@ -1915,7 +1852,7 @@ version = "0.21.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e4ba72c2ea84515690c9fcef4c6c660bb9df3036ed1051686de84605b74fd558"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
"futures-util",
|
||||
"ipconfig",
|
||||
"lazy_static",
|
||||
|
@ -1979,12 +1916,6 @@ version = "0.1.5"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.7.1"
|
||||
|
@ -2036,7 +1967,7 @@ version = "0.2.84"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
|
|
|
@ -10,7 +10,8 @@ clap = { version = "~4.1.8", features = ["derive", "cargo"] }
|
|||
config = "~0.13"
|
||||
log = "0.4.17"
|
||||
loggerv = "0.7.2"
|
||||
failure = "~0.1.8"
|
||||
# failure = "~0.1.8"
|
||||
anyhow = "1.0.69"
|
||||
bus = "~2.3.0"
|
||||
rand = "0.8.5"
|
||||
num-traits = "0.2.15"
|
||||
|
|
|
@ -6,7 +6,7 @@ use std::collections::HashMap;
|
|||
use rumqttc;
|
||||
use bson;
|
||||
use mongodb;
|
||||
use failure::{self,ResultExt};
|
||||
use anyhow::{self,Context};
|
||||
|
||||
use crate::sensordata::{SensorData,SensorDatum};
|
||||
use crate::params;
|
||||
|
@ -31,7 +31,7 @@ pub enum MessageType {
|
|||
}
|
||||
|
||||
impl DeviceType {
|
||||
pub fn process_bytes(&self, bytes: Vec<u8>) -> Result<Vec<SensorDatum>, failure::Error> {
|
||||
pub fn process_bytes(&self, bytes: Vec<u8>) -> anyhow::Result<Vec<SensorDatum>> {
|
||||
match self {
|
||||
//TempHumBattV1
|
||||
//- 2bytes|LE|temp*100
|
||||
|
@ -150,7 +150,7 @@ pub fn process_gw_incoming(msg: &str,
|
|||
node_list: &HashMap<String,DeviceDesc>,
|
||||
opts: ¶ms::AuxOptions,
|
||||
mut mq: Option<&mut rumqttc::Client>,
|
||||
mdb: Option<&mongodb::sync::Client>) -> Result<(), failure::Error> {
|
||||
mdb: Option<&mongodb::sync::Client>) -> anyhow::Result<()> {
|
||||
debug!("Processing incoming packet: {}.", msg);
|
||||
|
||||
// publish raw incoming
|
||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -1,7 +1,7 @@
|
|||
// crate jeethru
|
||||
// all jeethru functionality is in this crate except parsing/setting up program parameters
|
||||
|
||||
#[macro_use] extern crate failure;
|
||||
#[macro_use] extern crate anyhow;
|
||||
#[macro_use] extern crate log;
|
||||
|
||||
extern crate bus;
|
||||
|
@ -23,7 +23,7 @@ use std::sync;
|
|||
use std::time;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use failure::ResultExt;
|
||||
use anyhow::Context;
|
||||
|
||||
pub mod params;
|
||||
pub mod decode_jee;
|
||||
|
@ -87,7 +87,7 @@ fn handle_output_serve(listen_serve: net::TcpListener,
|
|||
// process one output client
|
||||
fn process_output_client(stream: net::TcpStream,
|
||||
bus_outhandle: &sync::Arc<sync::Mutex<bus::Bus<String>>>,
|
||||
gw_stream: &net::TcpStream) -> Result<(), failure::Error> {
|
||||
gw_stream: &net::TcpStream) -> anyhow::Result<()> {
|
||||
let rx = bus_outhandle.lock()
|
||||
.map_err(|e| format_err!("Couldn't lock the bus: {}", e.to_string()) )?
|
||||
.add_rx(); // can't use .context, weird error type
|
||||
|
@ -111,7 +111,7 @@ fn process_output_client(stream: net::TcpStream,
|
|||
|
||||
|
||||
pub fn processor(params: params::Params, node_list: HashMap<String,decode_jee::DeviceDesc>)
|
||||
-> Result<(), failure::Error> {
|
||||
-> anyhow::Result<()> {
|
||||
|
||||
// mqtt setup and connect
|
||||
let mut mqtt_client_options = rumqttc::MqttOptions::new(format!("jee-to-mqtt-rust-{:x}",rand::random::<u16>()),
|
||||
|
@ -131,7 +131,7 @@ pub fn processor(params: params::Params, node_list: HashMap<String,decode_jee::D
|
|||
|
||||
// thread: mqtt incoming (required for event loop even if we don't receive anything I think)
|
||||
let _mqtt_incoming_handler = thread::Builder::new().name("mqtt_incoming_handler".to_string()).spawn(move ||
|
||||
-> Result<(), failure::Error> {
|
||||
-> anyhow::Result<()> {
|
||||
for (i, notification) in mqtt_connection.unwrap().iter().enumerate() { // unwrap is 'guaranteed' here
|
||||
debug!("MQTT incoming notification = {}: {:?}", i, notification);
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ pub fn processor(params: params::Params, node_list: HashMap<String,decode_jee::D
|
|||
// thread: consume gateway|ser2net input
|
||||
let aux_opts_clone = params.auxopts.clone();
|
||||
let input_gw_handler = thread::Builder::new().name("input_handler".to_string()).spawn(move ||
|
||||
-> Result<(), failure::Error> {
|
||||
-> anyhow::Result<()> {
|
||||
for line in input_gw_reader.lines() {
|
||||
let line = line.context("TCP input stream error")?;
|
||||
bus_inhandle.lock()
|
||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -2,7 +2,7 @@
|
|||
//#![allow(unused_imports)]
|
||||
|
||||
#[macro_use] extern crate clap;
|
||||
#[macro_use] extern crate failure;
|
||||
#[macro_use] extern crate anyhow;
|
||||
#[macro_use] extern crate log;
|
||||
extern crate loggerv;
|
||||
extern crate config;
|
||||
|
@ -16,7 +16,7 @@ use std::str;
|
|||
use std::collections::HashMap;
|
||||
use std::process;
|
||||
|
||||
use failure::ResultExt;
|
||||
use anyhow::Context;
|
||||
|
||||
use num_traits::cast;
|
||||
|
||||
|
@ -30,7 +30,7 @@ use jeethru::{decode_jee,params};
|
|||
//
|
||||
// get parameter value (numerical)
|
||||
fn get_param<T>(settings: &config::Config, param: &str, cli_val: Option<T>)
|
||||
-> Result<T, failure::Error>
|
||||
-> anyhow::Result<T>
|
||||
where T: str::FromStr+cast::NumCast {
|
||||
if let Some(val) = cli_val {
|
||||
Ok(val)
|
||||
|
@ -115,7 +115,7 @@ struct CliArgs {
|
|||
|
||||
|
||||
// main function
|
||||
fn run(cli_args: CliArgs) -> Result<(), failure::Error> {
|
||||
fn run(cli_args: CliArgs) -> anyhow::Result<()> {
|
||||
|
||||
// settings
|
||||
let settings = config::Config::builder()
|
||||
|
@ -186,11 +186,10 @@ fn main() {
|
|||
// run run() and unwrap any returned errors
|
||||
process::exit(match run(cli_args) {
|
||||
Ok(_) => 0,
|
||||
Err(ref e) => {
|
||||
error!("fatal: {}; caused by:", e);
|
||||
for c in e.iter_causes() {
|
||||
error!(" {:?}", c);
|
||||
}
|
||||
Err(err) => {
|
||||
// error!("fatal: {}; caused by:", err);
|
||||
// err.chain().skip(1).for_each(|cause| error!(" -> {}", cause));
|
||||
error!("fatal: {:#}", err);
|
||||
1
|
||||
}
|
||||
});
|
||||
|
|
|
@ -9,7 +9,7 @@ use rumqttc;
|
|||
use bson;
|
||||
use mongodb;
|
||||
use chrono;
|
||||
use failure::ResultExt;
|
||||
use anyhow::{self,Context};
|
||||
|
||||
// sensor data structures: individual sensor reading
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
|
||||
|
@ -59,7 +59,7 @@ pub struct SensorData<'a> {
|
|||
|
||||
// publish a data packet to mqtt
|
||||
impl<'a> SensorData<'a> {
|
||||
pub fn publish_to_mqtt( &self, mq: &mut rumqttc::Client, prefix: &str ) -> Result<(), failure::Error> {
|
||||
pub fn publish_to_mqtt( &self, mq: &mut rumqttc::Client, prefix: &str ) -> anyhow::Result<()> {
|
||||
trace!("Publishing decoded packet to MQTT broker.");
|
||||
for datum in &self.data {
|
||||
mq.publish(
|
||||
|
@ -74,7 +74,7 @@ impl<'a> SensorData<'a> {
|
|||
}
|
||||
|
||||
// save a data packet to mongodb
|
||||
pub fn save_to_mongodb( &self, mdb: &mongodb::sync::Client ) -> Result<(), failure::Error> {
|
||||
pub fn save_to_mongodb( &self, mdb: &mongodb::sync::Client ) -> anyhow::Result<()> {
|
||||
trace!("Saving decoded packet to MongoDB.");
|
||||
let mc = mdb.database("sensors").collection::<mongodb::bson::Document>(
|
||||
&self.name.trim_matches('/').replace("/","_"));
|
||||
|
@ -91,7 +91,7 @@ impl<'a> SensorData<'a> {
|
|||
}
|
||||
|
||||
// save data to text files
|
||||
pub fn save_to_csv( &self, prefix: &str ) -> Result<(), failure::Error> {
|
||||
pub fn save_to_csv( &self, prefix: &str ) -> anyhow::Result<()> {
|
||||
for datum in &self.data {
|
||||
// one file for each datum...
|
||||
let filename = path::Path::new(prefix)
|
||||
|
|
Loading…
Reference in a new issue