use postcard_schema::Schema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Schema, Clone, Copy, Debug)]
pub enum Level {
Error,
Warn,
Info,
Debug,
Trace,
}
#[derive(Serialize, Schema, Clone)]
pub struct ErgotFmtTx<'a> {
pub level: Level,
pub inner: &'a core::fmt::Arguments<'a>,
}
#[derive(Serialize, Deserialize, Schema)]
pub struct ErgotFmtRx<'a> {
pub level: Level,
pub inner: &'a str,
}
#[cfg(feature = "std")]
#[derive(Serialize, Deserialize, Schema, Clone)]
pub struct ErgotFmtRxOwned {
pub level: Level,
pub inner: String,
}
#[macro_export]
macro_rules! fmt {
($fmt:expr) => {
&::core::format_args!($fmt)
};
($fmt:expr, $($toks: tt)*) => {
&::core::format_args!($fmt, $($toks)*)
};
}
#[cfg(test)]
mod test {
use super::*;
use crate::{
traits::Topic,
well_known::{ErgotFmtRxTopic, ErgotFmtTxTopic},
};
#[test]
fn fmt_punning_works() {
assert_eq!(ErgotFmtTxTopic::TOPIC_KEY, ErgotFmtRxTopic::TOPIC_KEY);
#[cfg(feature = "std")]
assert_eq!(
crate::well_known::ErgotFmtRxOwnedTopic::TOPIC_KEY,
ErgotFmtRxTopic::TOPIC_KEY
);
let x = 10;
let y = "world";
let res = postcard::to_vec::<_, 128>(&ErgotFmtTx {
level: Level::Warn,
inner: &format_args!("hello {}, {}", x, y),
})
.unwrap();
let res = postcard::from_bytes::<ErgotFmtRx<'_>>(&res).unwrap();
assert_eq!(res.inner, "hello 10, world");
}
}