ambiguous.rs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //! Parse anything that has a pattern of < Ident, Bracket >
  2. //! ========================================================
  3. //!
  4. //! Whenever a `name {}` pattern emerges, we need to parse it into an element, a component, or a fragment.
  5. //! This feature must support:
  6. //! - Namepsaced/pathed components
  7. //! - Differentiating between built-in and custom elements
  8. use super::*;
  9. use proc_macro2::TokenStream as TokenStream2;
  10. use quote::ToTokens;
  11. use syn::{
  12. parse::{Parse, ParseStream},
  13. Error, Ident, LitStr, Result, Token,
  14. };
  15. pub enum AmbiguousElement {
  16. Element(Element),
  17. Component(Component),
  18. }
  19. impl Parse for AmbiguousElement {
  20. fn parse(input: ParseStream) -> Result<Self> {
  21. // Try to parse as an absolute path and immediately defer to the componetn
  22. if input.peek(Token![::]) {
  23. return input
  24. .parse::<Component>()
  25. .map(|c| AmbiguousElement::Component(c));
  26. }
  27. // If not an absolute path, then parse the ident and check if it's a valid tag
  28. if let Ok(pat) = input.fork().parse::<syn::Path>() {
  29. if pat.segments.len() > 1 {
  30. return input
  31. .parse::<Component>()
  32. .map(|c| AmbiguousElement::Component(c));
  33. }
  34. }
  35. if let Ok(name) = input.fork().parse::<Ident>() {
  36. let name_str = name.to_string();
  37. match is_valid_tag(&name_str) {
  38. true => input
  39. .parse::<Element>()
  40. .map(|c| AmbiguousElement::Element(c)),
  41. false => {
  42. let first_char = name_str.chars().next().unwrap();
  43. if first_char.is_ascii_uppercase() {
  44. input
  45. .parse::<Component>()
  46. .map(|c| AmbiguousElement::Component(c))
  47. } else {
  48. let name = input.parse::<Ident>().unwrap();
  49. Err(Error::new(
  50. name.span(),
  51. "Components must be uppercased, perhaps you mispelled a html tag",
  52. ))
  53. }
  54. }
  55. }
  56. } else {
  57. if input.peek(LitStr) {
  58. panic!("it's actually a litstr");
  59. }
  60. Err(Error::new(input.span(), "Not a valid Html tag"))
  61. }
  62. }
  63. }
  64. impl ToTokens for AmbiguousElement {
  65. fn to_tokens(&self, tokens: &mut TokenStream2) {
  66. match self {
  67. AmbiguousElement::Element(el) => el.to_tokens(tokens),
  68. AmbiguousElement::Component(comp) => comp.to_tokens(tokens),
  69. }
  70. }
  71. }