serialize.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use serde::Serialize;
  2. use serde_derive::Serialize;
  3. use std::cell::RefCell;
  4. use std::collections::BTreeMap;
  5. use std::fmt::Debug;
  6. fn test<T>(value: &T, expected: &str)
  7. where
  8. T: ?Sized + Serialize + Debug,
  9. {
  10. let mut out = Vec::new();
  11. let ser = &mut serde_json::Serializer::new(&mut out);
  12. let result = serde_path_to_error::serialize(value, ser);
  13. let path = result.unwrap_err().path().to_string();
  14. assert_eq!(path, expected);
  15. }
  16. #[test]
  17. fn test_refcell_already_borrowed() {
  18. #[derive(Serialize, Debug)]
  19. struct Outer<'a> {
  20. k: Inner<'a>,
  21. }
  22. #[derive(Serialize, Debug)]
  23. struct Inner<'a> {
  24. refcell: &'a RefCell<String>,
  25. }
  26. let refcell = RefCell::new(String::new());
  27. let outer = Outer {
  28. k: Inner { refcell: &refcell },
  29. };
  30. let _borrowed = refcell.borrow_mut();
  31. test(&outer, "k.refcell");
  32. }
  33. #[test]
  34. fn test_map_nonstring_key() {
  35. let mut inner_map = BTreeMap::new();
  36. inner_map.insert(b"", 0);
  37. let mut outer_map = BTreeMap::new();
  38. outer_map.insert("k", inner_map);
  39. test(&outer_map, "k");
  40. }