use proc_macro2::TokenStream;
use quote::quote;
use super::Check;
use super::body::{Item, Walk};
use crate::walk::path;
use crate::{Error, Field, Kind, Root, Segment};
pub(super) fn names_self(ty: &str, owner: &str) -> bool {
let ty = ty.trim();
ty == owner || ty == "Self"
}
fn resolved(ty: &str, owner: &str, cyclic: &[(String, bool)]) -> Option<bool> {
let ty = ty.trim();
if let Some((_, value)) = cyclic.iter().find(|(n, _)| n == ty) {
return Some(*value);
}
names_self(ty, owner).then_some(false)
}
pub(super) fn unbounded_recursion(owner: &str, field: &str) -> String {
format!(
"`{owner}`: `{field}` is a nested `{owner}` that is always present, so the wire never ends. \
Put it behind an empty collection, a flag, or a switch arm"
)
}
pub(super) fn open_ended_expr(
segments: &[Segment],
owner: &str,
cyclic: &[(String, bool)],
root: &Root,
) -> TokenStream {
let forward = |ty: &str, of: TokenStream| {
if let Some(value) = resolved(ty, owner, cyclic) {
return quote!(#value);
}
let p = path(ty);
quote!(<#p as #root::#of>::OPEN_ENDED)
};
match segments.last() {
Some(Segment::Switch { choice, window: None, .. }) => forward(choice, quote!(Choice)),
Some(
Segment::Value { kind: Kind::Msg { ty, .. }, .. }
| Segment::Opt { field: Field { kind: Kind::Msg { ty, .. }, .. }, .. }
| Segment::Placed { kind: Kind::Msg { ty, .. }, .. },
) => forward(ty, quote!(Message)),
Some(seg) if seg.open_ended() => quote!(true),
_ => quote!(false),
}
}
pub(super) fn bounded_check(
owner: &str,
field: &str,
ty: &str,
what: &str,
root: &Root,
) -> TokenStream {
let p = path(ty);
let of = if what == "switch" { quote!(Choice) } else { quote!(Message) };
let msg = format!(
"`{owner}`: a field follows `{field}`, but {what} `{ty}` runs to the end of the body. \
Put `{field}` last, size every arm as `[u8; N]`, or close the catalogue"
);
quote! {
const _: () = ::core::assert!(!<#p as #root::#of>::OPEN_ENDED, #msg);
}
}
fn sized_check(owner: &str, field: &str, ty: &str, boxed: bool) -> Result<(), Error> {
if boxed || !names_self(ty, owner) {
return Ok(());
}
Err(Error::Refused(format!(
"`{owner}`: `{field}` contains a `{owner}` directly, so the type has infinite size. \
Use `Box<{owner}>`, or `Vec<{owner}>` for a sequence"
)))
}
impl Walk<'_> {
pub(super) fn held_message(
&self,
item: &mut Item<'_>,
field: &str,
kind: &Kind,
last: bool,
) -> Result<(), Error> {
let Kind::Msg { ty, boxed, .. } = kind else { return Ok(()) };
sized_check(self.owner, field, ty, *boxed)?;
if !last {
item.checks.push(Check {
field: String::from(field),
tokens: bounded_check(self.owner, field, ty, "nested message", self.root),
});
}
Ok(())
}
}