1
0
Fork 0

Start custom error type

refactor
Bo Jeanes 2022-09-04 16:51:38 +10:00
parent f87fcec1cf
commit b6943ce9a9
3 changed files with 26 additions and 20 deletions

20
Cargo.lock generated
View File

@ -732,13 +732,13 @@ dependencies = [
"serde",
"serde_json",
"serialport",
"thiserror",
"tokio",
"tokio-modbus",
"tokio-serial",
"tokio_modbus-winets",
"tracing",
"tracing-subscriber",
"uuid",
]
[[package]]
@ -1348,18 +1348,18 @@ checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
[[package]]
name = "thiserror"
version = "1.0.32"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5f6586b7f764adc0231f4c79be7b920e766bb2f3e51b3661cdb263828f19994"
checksum = "3d0a539a918745651435ac7db7a18761589a94cd7e94cd56999f828bf73c8a57"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.32"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12bafc5b54507e0149cdf1b145a5d80ab80a90bcd9275df43d4fff68460f6c21"
checksum = "c251e90f708e16c49a16f4917dc2131e75222b72edfa9cb7f7c58ae56aae0c09"
dependencies = [
"proc-macro2",
"quote",
@ -1637,16 +1637,6 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "uuid"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd6469f4314d5f1ffec476e05f17cc9a78bc7a27a6a857842170bdf8d6f98d2f"
dependencies = [
"getrandom",
"serde",
]
[[package]]
name = "valuable"
version = "0.1.0"

View File

@ -20,13 +20,13 @@ rust_decimal = { version = "1.26.1", features = ["serde-arbitrary-precision", "s
serde = { version = "1.0.139", features = ["serde_derive"] }
serde_json = "1.0.82"
serialport = { version = "4.2.0", features = ["serde"] }
thiserror = "1.0.33"
tokio = { version = "1.20.0", features = ["rt", "rt-multi-thread", "time"] }
tokio-modbus = "0.5.3"
tokio-serial = "5.4.3"
tokio_modbus-winets = { version = "0.1.0", path = "../tokio_modbus-winets" }
tracing = "0.1.36"
tracing-subscriber = "0.3.15"
uuid = { version = "1.1.2", features = ["v4", "serde"] }
[dev-dependencies]
pretty_assertions = "1.2.1"

View File

@ -6,10 +6,24 @@ use tokio::{sync::mpsc, sync::oneshot, time::MissedTickBehavior};
use tokio_modbus::prelude::*;
use tracing::{debug, error, info};
use thiserror::Error;
use clap::Parser;
mod modbus;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error(transparent)]
IOError(#[from] std::io::Error),
#[error("Unknown")]
Unknown,
}
type Result<T> = std::result::Result<T, Error>;
#[derive(Parser)]
struct Cli {
mqtt_host: String,
@ -39,7 +53,7 @@ enum MainStatus {
}
#[tokio::main(worker_threads = 3)]
async fn main() {
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let args = Cli::parse();
@ -71,6 +85,7 @@ async fn main() {
registry_handle.await.unwrap();
dispatcher_handle.await.unwrap();
Ok(())
}
#[derive(Debug)]
@ -228,7 +243,7 @@ enum ModbusCommand {
Write(u16, Vec<u16>, ModbusResponse),
}
type ModbusResponse = oneshot::Sender<Result<Vec<u16>, std::io::Error>>;
type ModbusResponse = oneshot::Sender<Result<Vec<u16>>>;
#[tracing::instrument(level = "debug")]
async fn handle_connect(
@ -297,7 +312,7 @@ async fn handle_connect(
}
};
responder.send(response.await).unwrap();
responder.send(response.await.map_err(Into::into)).unwrap();
}
ModbusCommand::Write(address, data, responder) => {
responder
@ -309,7 +324,8 @@ async fn handle_connect(
address,
&data[..],
)
.await,
.await
.map_err(Into::into),
)
.unwrap();
}