SHARE
TWEET

Untitled

a guest Jul 18th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use serde::{Deserialize, Deserializer};
  2. use std::collections::HashMap;
  3. use std::fmt;
  4.  
  5. /// Just try to serialize a `Foo`.
  6. fn main() {
  7.     let s = "features = ['Jerry', 'Jerry']\n\n[aliases]\nfoo='Jerry'\n";
  8.     let _: GoodHelper<'_> = dbg!(toml::from_str(s).unwrap());
  9.     let _: BadHelper<'_> = dbg!(toml::from_str(s).unwrap());
  10.     let _: Foo = dbg!(toml::from_str(s).unwrap());
  11. }
  12.  
  13. /// A container of `Jerry`. This can be created via `GoodHelper`, but not via
  14. /// `BadHelper`, for some reason.
  15. #[derive(Debug, Default, Deserialize)]
  16. #[serde(from = "BadHelper")] // <-- change this to GoodHelper
  17. struct Foo {
  18.     everything: Vec<Jerry>,
  19. }
  20.  
  21. /// A `Foo` can be created from the helper by just collecting `Jerry`.
  22. impl From<GoodHelper<'_>> for Foo {
  23.     fn from(helper: GoodHelper<'_>) -> Self {
  24.         let mut res = Foo::default();
  25.         res.everything.extend(helper.aliases.values());
  26.         res
  27.     }
  28. }
  29.  
  30. /// A `Foo` can be created from the helper by just collecting `Jerry`.
  31. impl From<BadHelper<'_>> for Foo {
  32.     fn from(helper: BadHelper<'_>) -> Self {
  33.         let mut res = Foo::default();
  34.         res.everything.extend(helper.aliases.values().cloned());
  35.         res
  36.     }
  37. }
  38.  
  39. /// Creating `Foo` via this helper works.
  40. #[derive(Debug, Deserialize)]
  41. #[serde(rename = "foo")]
  42. struct GoodHelper<'a> {
  43.     #[serde(borrow)]
  44.     aliases: HashMap<&'a str, Jerry>,
  45. }
  46.  
  47. /// Creating `Foo` via this helper doesn't work.
  48. /// Creating the helper itself *does* work, however.
  49. #[derive(Debug, Deserialize)]
  50. #[serde(rename = "foo")]
  51. struct BadHelper<'a> {
  52.     #[serde(borrow)]
  53.     aliases: HashMap<&'a str, &'static Jerry>,
  54. }
  55.  
  56. /// Jerry.
  57. #[derive(Debug, Clone, Copy)]
  58. struct Jerry;
  59.  
  60. /// You can deserialize `Jerry` …
  61. impl<'de> Deserialize<'de> for Jerry {
  62.     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  63.     where
  64.         D: Deserializer<'de>,
  65.     {
  66.         use serde::de::Error;
  67.         use serde::de::Unexpected;
  68.         struct Visitor;
  69.         impl<'de> serde::de::Visitor<'de> for Visitor {
  70.             type Value = Jerry;
  71.             fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  72.                 f.write_str("Jerry")
  73.             }
  74.             fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
  75.             where
  76.                 E: Error,
  77.             {
  78.                 match v {
  79.                     "Jerry" => Ok(Jerry),
  80.                     _ => Err(Error::invalid_value(Unexpected::Str(v), &self)),
  81.                 }
  82.             }
  83.         }
  84.         deserializer.deserialize_str(Visitor)
  85.     }
  86. }
  87.  
  88. /// … but you can also deserialize `&'stati Jerry`.
  89. impl<'de> Deserialize<'de> for &'static Jerry {
  90.     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  91.     where
  92.         D: Deserializer<'de>,
  93.     {
  94.         use serde::de::Error;
  95.         use serde::de::Unexpected;
  96.         struct Visitor;
  97.         impl<'de> serde::de::Visitor<'de> for Visitor {
  98.             type Value = &'static Jerry;
  99.             fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  100.                 f.write_str("Jerry")
  101.             }
  102.             fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
  103.             where
  104.                 E: Error,
  105.             {
  106.                 match v {
  107.                     "Jerry" => Ok(&Jerry),
  108.                     _ => Err(Error::invalid_value(Unexpected::Str(v), &self)),
  109.                 }
  110.             }
  111.         }
  112.         deserializer.deserialize_str(Visitor)
  113.     }
  114. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top