From 9f3833d13fbc70810b6bf7ecf3f9b2ecf92f4fc1 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 16 Dec 2024 16:17:56 +0100 Subject: [PATCH] Add FromStr impls for MessageStatus, StatusCodeClass --- rust/src/model_ext.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/rust/src/model_ext.rs b/rust/src/model_ext.rs index 7c258f4c5..b5e934637 100644 --- a/rust/src/model_ext.rs +++ b/rust/src/model_ext.rs @@ -4,7 +4,10 @@ use std::str::FromStr; use serde_json::json; -use crate::{api::Ordering, models::MessageIn}; +use crate::{ + api::{MessageStatus, Ordering, StatusCodeClass}, + models::MessageIn, +}; impl MessageIn { /// Create a new message with a pre-serialized payload. @@ -56,3 +59,41 @@ impl FromStr for Ordering { } } } + +#[derive(Debug, thiserror::Error)] +#[error("invalid value for messageStatus")] +pub struct MessageStatusFromStrError; + +impl FromStr for MessageStatus { + type Err = MessageStatusFromStrError; + + fn from_str(s: &str) -> Result { + match s { + "0" | "success" => Ok(Self::Success), + "1" | "pending" => Ok(Self::Pending), + "2" | "fail" => Ok(Self::Fail), + "3" | "sending" => Ok(Self::Sending), + _ => Err(MessageStatusFromStrError), + } + } +} + +#[derive(Debug, thiserror::Error)] +#[error("invalid value for statusCodeClass")] +pub struct StatusCodeClassFromStrError; + +impl FromStr for StatusCodeClass { + type Err = StatusCodeClassFromStrError; + + fn from_str(s: &str) -> Result { + match s { + "0" => Ok(Self::CodeNone), + "100" => Ok(Self::Code1xx), + "200" => Ok(Self::Code2xx), + "300" => Ok(Self::Code3xx), + "400" => Ok(Self::Code4xx), + "500" => Ok(Self::Code5xx), + _ => Err(StatusCodeClassFromStrError), + } + } +}