Skip to content

Commit

Permalink
verify supported protocol in parsing step
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Aug 7, 2023
1 parent 877ebf3 commit 269acc9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions crates/rune-macros/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,43 @@ impl TypeProtocol {
module.associated_function(rune::runtime::Protocol::ADD, |this: Self, other: Self| this + other)?;
},
"STRING_DISPLAY" => quote_spanned! {self.protocol.span()=>
module.associated_function(rune::runtime::Protocol::STRING_DISPLAY, |this: &Self, buf: &mut String| {
module.associated_function(rune::runtime::Protocol::STRING_DISPLAY, |this: &Self, buf: &mut ::std::string::String| {
use ::core::fmt::Write as _;
::core::write!(buf, "{this}")
})?;
},
"STRING_DEBUG" => quote_spanned! {self.protocol.span()=>
module.associated_function(rune::runtime::Protocol::STRING_DEBUG, |this: &Self, buf: &mut String| {
module.associated_function(rune::runtime::Protocol::STRING_DEBUG, |this: &Self, buf: &mut ::std::string::String| {
use ::core::fmt::Write as _;
::core::write!(buf, "{this:?}")
})?;
},
_ => syn::Error::new_spanned(
&self.protocol,
format!("Rune protocol `{}` cannot be derived", self.protocol),
)
.to_compile_error(),
_ => unreachable!("`parse()` ensures only supported protocols")
}
}
}

impl Parse for TypeProtocol {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(Self {
let it = Self {
protocol: input.parse()?,
handler: if input.parse::<Token![=]>().is_ok() {
Some(input.parse()?)
} else {
None
},
})
};

if it.handler.is_some()
|| ["ADD", "STRING_DISPLAY", "STRING_DEBUG"].contains(&it.protocol.to_string().as_str())
{
Ok(it)
} else {
Err(syn::Error::new_spanned(
&it.protocol,
format!("Rune protocol `{}` cannot be derived", it.protocol),
))
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rune-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub fn to_value(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
.into()
}

#[proc_macro_derive(Any, attributes(rune))]
#[proc_macro_derive(Any, attributes(rune, rune_derive, rune_functions))]
pub fn any(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let derive = syn::parse_macro_input!(input as any::Derive);
derive.expand().unwrap_or_else(to_compile_errors).into()
Expand Down

0 comments on commit 269acc9

Please sign in to comment.