hotreload_pattern.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #![allow(unused)]
  2. use dioxus_rsx::{
  3. hot_reload::{diff_rsx, template_location, ChangedRsx, DiffResult},
  4. CallBody, HotReloadingContext,
  5. };
  6. use quote::quote;
  7. use syn::{parse::Parse, spanned::Spanned, File};
  8. #[derive(Debug)]
  9. struct Mock;
  10. impl HotReloadingContext for Mock {
  11. fn map_attribute(
  12. element_name_rust: &str,
  13. attribute_name_rust: &str,
  14. ) -> Option<(&'static str, Option<&'static str>)> {
  15. match element_name_rust {
  16. "svg" => match attribute_name_rust {
  17. "width" => Some(("width", Some("style"))),
  18. "height" => Some(("height", Some("style"))),
  19. _ => None,
  20. },
  21. _ => None,
  22. }
  23. }
  24. fn map_element(element_name_rust: &str) -> Option<(&'static str, Option<&'static str>)> {
  25. match element_name_rust {
  26. "svg" => Some(("svg", Some("svg"))),
  27. _ => None,
  28. }
  29. }
  30. }
  31. #[test]
  32. fn testing_for_pattern() {
  33. let old = quote! {
  34. div {
  35. for item in vec![1, 2, 3] {
  36. div { "123" }
  37. div { "asasddasdasd" }
  38. }
  39. }
  40. };
  41. let new = quote! {
  42. div {
  43. for item in vec![1, 2, 3] {
  44. div { "asasddasdasd" }
  45. }
  46. }
  47. };
  48. let old: CallBody = syn::parse2(old).unwrap();
  49. let new: CallBody = syn::parse2(new).unwrap();
  50. let updated = old.update_template::<Mock>(Some(new), "testing");
  51. // currently, modifying a for loop is not hot reloadable
  52. // We want to change this...
  53. assert!(updated.is_none());
  54. // let updated = old.update_template::<Mock>(Some(new), "testing").unwrap();
  55. // let old = include_str!(concat!("./valid/for_.old.rsx"));
  56. // let new = include_str!(concat!("./valid/for_.new.rsx"));
  57. // let (old, new) = load_files(old, new);
  58. // let DiffResult::RsxChanged { rsx_calls } = diff_rsx(&new, &old) else {
  59. // panic!("Expected a rsx call to be changed")
  60. // };
  61. // for calls in rsx_calls {
  62. // let ChangedRsx { old, new } = calls;
  63. // let old_start = old.span().start();
  64. // let old_call_body = syn::parse2::<CallBody>(old.tokens).unwrap();
  65. // let new_call_body = syn::parse2::<CallBody>(new).unwrap();
  66. // let leaked_location = Box::leak(template_location(old_start, file).into_boxed_str());
  67. // let hotreloadable_template =
  68. // new_call_body.update_template::<Ctx>(Some(old_call_body), leaked_location);
  69. // dbg!(hotreloadable_template);
  70. // }
  71. // dbg!(rsx_calls);
  72. }