From 6cde64a87a4d19798455664ccbc4233a4b931378 Mon Sep 17 00:00:00 2001 From: Franklin Date: Tue, 20 Sep 2022 00:24:55 -0400 Subject: [PATCH] fromstr or unexpectedstatus code and unspecified error --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/enums/error.rs | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ec2d98..fbd65ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,7 +183,7 @@ dependencies = [ [[package]] name = "actix-web-utils" -version = "0.2.16" +version = "0.2.17" dependencies = [ "actix-web", "log", diff --git a/Cargo.toml b/Cargo.toml index b120a5b..d95bafb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web-utils" -version = "0.2.16" +version = "0.2.17" edition = "2021" authors = ["Franklin E. Blanco"] description = "Just some useful addons for actix web." diff --git a/src/enums/error.rs b/src/enums/error.rs index 62aae36..2b3be88 100644 --- a/src/enums/error.rs +++ b/src/enums/error.rs @@ -1,4 +1,4 @@ -use std::fmt::{self}; +use std::{fmt::{self}, str::FromStr}; use crate::dtos::message::MessageResource; @@ -36,4 +36,39 @@ impl fmt::Display for Error{ } } } +impl FromStr for Error { + type Err = Error; + + fn from_str(string: &str) -> Result { + let error_name_option = string.get(14..24); + let error_name_whole = match error_name_option { + Some(error_name_whole) => error_name_whole, + None => return Err(Error::Unspecified), + }; + if error_name_whole.starts_with("Unspecified") { + return Err(Self::Unspecified) + } else if error_name_whole.starts_with("UnexpectedStatusCode") { + let expected_str_index = string.find("Expected: ").unwrap() + 10; + let actual_str_index = string.find("Actual: ").unwrap() + 8; + let expected_status_code = string.get(expected_str_index..expected_str_index+3).unwrap(); + let actual_status_code = string.get(actual_str_index..actual_str_index+3).unwrap(); + + let message_resources_string = string.get(string.find("receivedMessageResources").unwrap() + 26..string.len() - 1).unwrap(); + let message_resources: Vec = serde_json::from_str(message_resources_string).unwrap(); + return Err(Self::UnexpectedStatusCode(expected_status_code.parse().unwrap(), actual_status_code.parse().unwrap(), message_resources)); + } else if error_name_whole.starts_with("Client") { + + } else if error_name_whole.starts_with("Network") { + + } else if error_name_whole.starts_with("Serialization") { + + } else if error_name_whole.starts_with("Database") { + + } else if error_name_whole.starts_with("Compute") { + + } + + Ok(Error::Unspecified) + } +} impl std::error::Error for Error {} \ No newline at end of file