use std::sync::LazyLock;
use reliar_core::{ContentType, Message, Serializer};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub(crate) struct OrderCreated {
pub order_id: u64,
}
impl Message for OrderCreated {
const TYPE: &'static str = "orders.created";
const VERSION: u16 = 1;
}
#[derive(Clone, Debug, Default)]
pub(crate) struct TestVndSerializer;
#[derive(Debug)]
pub(crate) struct TestVndSerializerError(String);
impl std::fmt::Display for TestVndSerializerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "test serializer error: {}", self.0)
}
}
impl std::error::Error for TestVndSerializerError {}
impl Serializer for TestVndSerializer {
type Error = TestVndSerializerError;
fn content_type(&self) -> &ContentType {
static CONTENT_TYPE: LazyLock<ContentType> =
LazyLock::new(|| ContentType::parse("application/vnd.reliar-test+json").unwrap());
&CONTENT_TYPE
}
fn serialize<T: Message>(&self, body: &T) -> Result<bytes::Bytes, Self::Error> {
serde_json::to_vec(body)
.map(bytes::Bytes::from)
.map_err(|err| TestVndSerializerError(err.to_string()))
}
fn deserialize<T: Message>(&self, bytes: &[u8]) -> Result<T, Self::Error> {
serde_json::from_slice(bytes).map_err(|err| TestVndSerializerError(err.to_string()))
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct AlwaysFailingSerializer;
#[derive(Debug)]
pub(crate) struct AlwaysFailingSerializerError;
impl std::fmt::Display for AlwaysFailingSerializerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "this serializer always fails, by design")
}
}
impl std::error::Error for AlwaysFailingSerializerError {}
impl Serializer for AlwaysFailingSerializer {
type Error = AlwaysFailingSerializerError;
fn content_type(&self) -> &ContentType {
&ContentType::JSON
}
fn serialize<T: Message>(&self, _body: &T) -> Result<bytes::Bytes, Self::Error> {
Err(AlwaysFailingSerializerError)
}
fn deserialize<T: Message>(&self, _bytes: &[u8]) -> Result<T, Self::Error> {
Err(AlwaysFailingSerializerError)
}
}