Skip to content

Commit

Permalink
removed some of the unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
KaranGauswami committed Sep 3, 2023
1 parent 55a6b3c commit a39ba4c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/dp_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event, EslError> {
self.execute("playback", file_path).await
self.execute(PLAYBACK_APP, file_path).await
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -23,11 +26,10 @@ impl EslConnection {
invalid_file: &str,
) -> Result<String, EslError> {
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));
Expand Down
23 changes: 14 additions & 9 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -40,10 +40,15 @@ fn parse_header(src: &[u8]) -> Result<HashMap<String, Value>, 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)
Expand All @@ -55,10 +60,10 @@ impl Decoder for EslCodec {
fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, 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;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(missing_docs, rust_2018_idioms)]
#![deny(missing_docs)]

//! Esl Create for interacting with freeswitch
//!
Expand Down

0 comments on commit a39ba4c

Please sign in to comment.