diff --git a/src/dp_tools.rs b/src/dp_tools.rs index 123c77b..96b641c 100644 --- a/src/dp_tools.rs +++ b/src/dp_tools.rs @@ -2,12 +2,15 @@ use std::collections::HashMap; use serde_json::Value; +const PLAY_AND_GET_DIGITS_APP: &str = "play_and_get_digits"; +const PLAYBACK_APP: &str = "playback"; + use crate::{EslConnection, EslError, Event}; impl EslConnection { /// plays file in call during outbound mode pub async fn playback(&self, file_path: &str) -> Result { - self.execute("playback", file_path).await + self.execute(PLAYBACK_APP, file_path).await } #[allow(clippy::too_many_arguments)] @@ -23,11 +26,10 @@ impl EslConnection { invalid_file: &str, ) -> Result { let variable_name = uuid::Uuid::new_v4().to_string(); - let app_name = "play_and_get_digits"; let app_args = format!( "{min} {max} {tries} {timeout} {terminators} {file} {invalid_file} {variable_name}", ); - let data = self.execute(app_name, &app_args).await?; + let data = self.execute(PLAY_AND_GET_DIGITS_APP, &app_args).await?; let body = data.body.as_ref().unwrap(); let body = parse_json_body(body).unwrap(); let result = body.get(&format!("variable_{}", variable_name)); diff --git a/src/io.rs b/src/io.rs index a0eb2f3..1451e54 100644 --- a/src/io.rs +++ b/src/io.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use bytes::Buf; use serde_json::Value; use tokio_util::codec::{Decoder, Encoder}; -use tracing::trace; +use tracing::{trace, warn}; use crate::{event::Event, EslError}; @@ -40,10 +40,15 @@ fn parse_header(src: &[u8]) -> Result, std::io::Error> { let a = data.split('\n'); let mut hash = HashMap::new(); for line in a { - let mut key_value = line.split(':'); - let key = key_value.next().unwrap().trim().to_string(); - let val = key_value.next().unwrap().trim().to_string(); - hash.insert(key, serde_json::json!(val)); + let parts: Vec<&str> = line.split(':').collect(); + if parts.len() == 2 { + // SAFETY: Index access is safe beacue we have checked the length + let key = parts[0].trim(); + let val = parts[1].trim(); + hash.insert(key.to_string(), serde_json::json!(val.to_string())); + } else { + warn!("Invalid formatting while parsing header"); + } } trace!("returning hashmap : {:?}", hash); Ok(hash) @@ -55,10 +60,10 @@ impl Decoder for EslCodec { fn decode(&mut self, src: &mut bytes::BytesMut) -> Result, Self::Error> { trace!("decode"); let header_end = get_header_end(src); - if header_end.is_none() { - return Ok(None); - } - let header_end = header_end.unwrap(); + let header_end = match header_end { + Some(he) => he, + None => return Ok(None), + }; let headers = parse_header(&src[..(header_end - 1)])?; trace!("parsed headers are : {:?}", headers); let body_start = header_end + 1; diff --git a/src/lib.rs b/src/lib.rs index afedaa3..d5b0646 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -#![deny(missing_docs, rust_2018_idioms)] +#![deny(missing_docs)] //! Esl Create for interacting with freeswitch //!