use crate::{DefaultStr, FromStaticStr, types::value::Data};
#[doc(hidden)]
pub trait AtprotoMacroLiteral {
fn into_atproto_data(self) -> Data;
}
impl AtprotoMacroLiteral for &'static str {
fn into_atproto_data(self) -> Data {
Data::String(crate::types::string::AtprotoStr::new(
DefaultStr::from_static(self),
))
}
}
macro_rules! impl_atproto_macro_integer_literal {
($($ty:ty),* $(,)?) => {
$(
impl AtprotoMacroLiteral for $ty {
fn into_atproto_data(self) -> Data {
Data::Integer(i64::from(self))
}
}
)*
};
}
macro_rules! impl_atproto_macro_checked_integer_literal {
($($ty:ty),* $(,)?) => {
$(
impl AtprotoMacroLiteral for $ty {
fn into_atproto_data(self) -> Data {
Data::Integer(self.try_into().expect("integer literal exceeds the AT Protocol i64 range"))
}
}
)*
};
}
impl_atproto_macro_integer_literal!(i8, i16, i32, u8, u16, u32);
impl_atproto_macro_checked_integer_literal!(i64, i128, isize, u64, u128, usize);
#[macro_export(local_inner_macros)]
macro_rules! atproto {
($($atproto:tt)+) => {
atproto_internal!($($atproto)+)
};
}
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! atproto_internal {
(@array [$($elems:expr,)*]) => {
atproto_internal_vec![$($elems,)*]
};
(@array [$($elems:expr),*]) => {
atproto_internal_vec![$($elems),*]
};
(@array [$($elems:expr,)*] null $($rest:tt)*) => {
atproto_internal!(@array [$($elems,)* atproto_internal!(null)] $($rest)*)
};
(@array [$($elems:expr,)*] true $($rest:tt)*) => {
atproto_internal!(@array [$($elems,)* atproto_internal!(true)] $($rest)*)
};
(@array [$($elems:expr,)*] false $($rest:tt)*) => {
atproto_internal!(@array [$($elems,)* atproto_internal!(false)] $($rest)*)
};
(@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
atproto_internal!(@array [$($elems,)* atproto_internal!([$($array)*])] $($rest)*)
};
(@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
atproto_internal!(@array [$($elems,)* atproto_internal!({$($map)*})] $($rest)*)
};
(@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
atproto_internal!(@array [$($elems,)* atproto_internal!($next),] $($rest)*)
};
(@array [$($elems:expr,)*] $last:expr) => {
atproto_internal!(@array [$($elems,)* atproto_internal!($last)])
};
(@array [$($elems:expr),*] , $($rest:tt)*) => {
atproto_internal!(@array [$($elems,)*] $($rest)*)
};
(@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
atproto_unexpected!($unexpected)
};
(@object $object:ident () () ()) => {};
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
let _ = $object.insert(atproto_internal_key!($($key)+), $value);
atproto_internal!(@object $object () ($($rest)*) ($($rest)*));
};
(@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
atproto_unexpected!($unexpected);
};
(@object $object:ident [$($key:tt)+] ($value:expr)) => {
let _ = $object.insert(atproto_internal_key!($($key)+), $value);
};
(@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
atproto_internal!(@object $object [$($key)+] (atproto_internal!(null)) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
atproto_internal!(@object $object [$($key)+] (atproto_internal!(true)) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
atproto_internal!(@object $object [$($key)+] (atproto_internal!(false)) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
atproto_internal!(@object $object [$($key)+] (atproto_internal!([$($array)*])) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
atproto_internal!(@object $object [$($key)+] (atproto_internal!({$($map)*})) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
atproto_internal!(@object $object [$($key)+] (atproto_internal!($value)) , $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
atproto_internal!(@object $object [$($key)+] (atproto_internal!($value)));
};
(@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
atproto_internal!();
};
(@object $object:ident ($($key:tt)+) () $copy:tt) => {
atproto_internal!();
};
(@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
atproto_unexpected!($colon);
};
(@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
atproto_unexpected!($comma);
};
(@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
atproto_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
};
(@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
atproto_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
};
(null) => {
$crate::types::value::Data::<$crate::DefaultStr>::Null
};
(true) => {
$crate::types::value::Data::<$crate::DefaultStr>::Boolean(true)
};
(false) => {
$crate::types::value::Data::<$crate::DefaultStr>::Boolean(false)
};
([]) => {
$crate::types::value::Data::<$crate::DefaultStr>::Array($crate::types::value::Array(atproto_internal_vec![]))
};
([ $($tt:tt)+ ]) => {
$crate::types::value::Data::<$crate::DefaultStr>::Array($crate::types::value::Array(atproto_internal!(@array [] $($tt)+)))
};
({}) => {
$crate::types::value::Data::<$crate::DefaultStr>::Object($crate::types::value::Object(::std::collections::BTreeMap::new()))
};
({ $($tt:tt)+ }) => {
$crate::types::value::Data::<$crate::DefaultStr>::Object($crate::types::value::Object({
let mut object = ::std::collections::BTreeMap::new();
atproto_internal!(@object object () ($($tt)+) ($($tt)+));
object
}))
};
($literal:literal) => {
$crate::macros::AtprotoMacroLiteral::into_atproto_data($literal)
};
($other:expr) => {
{
$crate::types::value::Data::<$crate::DefaultStr>::from($other)
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! atproto_internal_vec {
($($content:tt)*) => {
::std::vec![$($content)*]
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! atproto_internal_key {
($key:literal) => {
<$crate::DefaultStr as $crate::FromStaticStr>::from_static($key)
};
($key:expr) => {
($key).into()
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! atproto_unexpected {
() => {};
}
#[cfg(test)]
mod tests {
use crate::{DefaultStr, types::value::Data};
const LONG_KEY: &str = "a-static-key-that-is-longer-than-inline-capacity";
const LONG_VALUE: &str = "a static string value that is longer than inline capacity";
#[test]
fn string_literals_use_static_default_backing() {
let value = atproto!({
"a-static-key-that-is-longer-than-inline-capacity":
"a static string value that is longer than inline capacity"
});
let Data::Object(object) = value else {
panic!("expected object");
};
let (key, value) = object.0.iter().next().expect("object has one field");
assert_eq!(key.as_str(), LONG_KEY);
assert!(!key.is_heap_allocated());
let Data::String(string) = value else {
panic!("expected string value");
};
assert_eq!(string.as_str(), LONG_VALUE);
if let crate::types::string::AtprotoStr::String(backing) = string {
assert!(!backing.is_heap_allocated());
} else {
panic!("test value should not be inferred as a richer atproto string type");
}
}
#[test]
fn macro_result_defaults_to_default_backing_without_context() {
let value = atproto!(["hello", 200, true, null]);
let _: Data<DefaultStr> = value;
}
}