From 17b54fbe3aad24b9925fa52b2ce60d458a9c2716 Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Thu, 16 Jun 2022 01:39:16 +0200 Subject: [PATCH] remove all unwraps :) --- Cargo.toml | 1 + src/command.rs | 2 -- src/main.rs | 11 +++++++---- src/modem.rs | 3 +++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f582a29..3596908 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ opt-level = "z" pio = ["esp-idf-sys/pio"] [dependencies] +anyhow = "1.0.57" embedded-hal = "0.2.7" esp-idf-hal = "0.37.4" esp-idf-sys = { version = "0.31.5", features = ["binstart"] } diff --git a/src/command.rs b/src/command.rs index 440cf8b..6de5fa6 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,5 +1,3 @@ -#[allow(dead_code)] - use std::time::Duration; pub struct Command { diff --git a/src/main.rs b/src/main.rs index f00db07..36f1e2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,12 @@ mod modem; #[allow(dead_code)] mod command; +use anyhow; use esp_idf_hal::prelude::*; use esp_idf_hal::peripherals::Peripherals; use esp_idf_hal::serial; -fn main() { +fn main() -> anyhow::Result<()> { esp_idf_sys::link_patches(); let dp = Peripherals::take().unwrap(); @@ -36,15 +37,17 @@ fn main() { let modem_rst = dp.pins.gpio5.into_output().unwrap(); let modem_power = dp.pins.gpio23.into_output().unwrap(); - modem::init(modem_pwrkey, modem_rst, modem_power).unwrap(); + modem::init(modem_pwrkey, modem_rst, modem_power)?; let (tx, rx) = serial.split(); let mut mdm = modem::Modem::new(tx, rx); - let _ = mdm.modem_info().unwrap(); + let _ = mdm.modem_info()?; let _ = mdm.connect_to_gprs_ap( config::A1_GPRS_AP.apn, config::A1_GPRS_AP.username, config::A1_GPRS_AP.password, - ).unwrap(); + )?; + + Ok(()) } diff --git a/src/modem.rs b/src/modem.rs index d0f31f7..dd75eb2 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -2,6 +2,7 @@ use crate::command::Command; use std::iter::FromIterator; use std::thread; +use std::error::Error; use std::time::{Duration, Instant}; use embedded_hal::serial::{Read, Write}; @@ -21,6 +22,8 @@ pub enum ModemError { SetupError(String), } +impl Error for ModemError {} + impl std::fmt::Display for ModemError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{:?}", self)