deserialize_props.rs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. use serde::de::DeserializeOwned;
  2. use super::u16_from_char;
  3. #[allow(unused)]
  4. pub(crate) fn serde_from_string<T: DeserializeOwned>(string: &str) -> Option<T> {
  5. let decompressed = string
  6. .chars()
  7. .flat_map(|c| {
  8. let u = u16_from_char(c);
  9. let u1 = (u >> 8) as u8;
  10. let u2 = (u & 0xFF) as u8;
  11. [u1, u2].into_iter()
  12. })
  13. .collect::<Vec<u8>>();
  14. let (decompressed, _) = yazi::decompress(&decompressed, yazi::Format::Zlib).unwrap();
  15. postcard::from_bytes(&decompressed).ok()
  16. }
  17. #[cfg(not(feature = "ssr"))]
  18. /// Get the props from the document. This is only available in the browser.
  19. pub fn get_props_from_document<T: DeserializeOwned>() -> Option<T> {
  20. #[cfg(not(target_arch = "wasm32"))]
  21. {
  22. None
  23. }
  24. #[cfg(target_arch = "wasm32")]
  25. {
  26. let attribute = web_sys::window()?
  27. .document()?
  28. .get_element_by_id("dioxus-storage")?
  29. .get_attribute("data-serialized")?;
  30. serde_from_string(&attribute)
  31. }
  32. }