Skip to content

Commit

Permalink
Add FromStr impls for MessageStatus, StatusCodeClass
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-jplatte committed Dec 16, 2024
1 parent 74e5206 commit 9f3833d
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion rust/src/model_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Self, Self::Err> {
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<Self, Self::Err> {
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),
}
}
}

0 comments on commit 9f3833d

Please sign in to comment.