rsxtemplate.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. use crate::{rsx::RsxBody, util::is_valid_svg_tag};
  2. use {
  3. proc_macro::TokenStream,
  4. proc_macro2::{Span, TokenStream as TokenStream2},
  5. quote::{quote, ToTokens, TokenStreamExt},
  6. syn::{
  7. ext::IdentExt,
  8. parse::{Parse, ParseStream},
  9. token, Error, Expr, ExprClosure, Ident, LitBool, LitStr, Path, Result, Token,
  10. },
  11. };
  12. // ==============================================
  13. // Parse any stream coming from the html! macro
  14. // ==============================================
  15. pub struct RsxTemplate {
  16. inner: RsxBody,
  17. }
  18. impl Parse for RsxTemplate {
  19. fn parse(s: ParseStream) -> Result<Self> {
  20. if s.peek(LitStr) {
  21. use std::str::FromStr;
  22. let lit = s.parse::<LitStr>()?;
  23. let g = lit.span();
  24. let mut value = lit.value();
  25. if value.ends_with('\n') {
  26. value.pop();
  27. if value.ends_with('\r') {
  28. value.pop();
  29. }
  30. }
  31. let lit = LitStr::new(&value, lit.span());
  32. // panic!("{:#?}", lit);
  33. match lit.parse::<crate::rsx::RsxBody>() {
  34. Ok(r) => Ok(Self { inner: r }),
  35. Err(e) => Err(e),
  36. }
  37. } else {
  38. panic!("Not a str lit")
  39. }
  40. // let t = s.parse::<LitStr>()?;
  41. // let new_stream = TokenStream::from(t.to_s)
  42. // let cx: Ident = s.parse()?;
  43. // s.parse::<Token![,]>()?;
  44. // if elements are in an array, return a bumpalo::collections::Vec rather than a Node.
  45. // let kind = if s.peek(token::Bracket) {
  46. // let nodes_toks;
  47. // syn::bracketed!(nodes_toks in s);
  48. // let mut nodes: Vec<MaybeExpr<Node>> = vec![nodes_toks.parse()?];
  49. // while nodes_toks.peek(Token![,]) {
  50. // nodes_toks.parse::<Token![,]>()?;
  51. // nodes.push(nodes_toks.parse()?);
  52. // }
  53. // NodeOrList::List(NodeList(nodes))
  54. // } else {
  55. // NodeOrList::Node(s.parse()?)
  56. // };
  57. // Ok(HtmlRender { kind })
  58. }
  59. }
  60. impl ToTokens for RsxTemplate {
  61. fn to_tokens(&self, out_tokens: &mut TokenStream2) {
  62. self.inner.to_tokens(out_tokens);
  63. // let new_toks = ToToksCtx::new(&self.kind).to_token_stream();
  64. // // create a lazy tree that accepts a bump allocator
  65. // let final_tokens = quote! {
  66. // dioxus::prelude::LazyNodes::new(move |cx| {
  67. // let bump = &cx.bump();
  68. // #new_toks
  69. // })
  70. // };
  71. // final_tokens.to_tokens(out_tokens);
  72. }
  73. }