Guest User

Untitled

a guest
Jun 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #![feature(try_from)]
  2. #![feature(never_type)]
  3.  
  4. use ::std::collections::BTreeMap;
  5. use ::std::convert::TryFrom;
  6.  
  7. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
  8. enum CborValue {
  9. Text(String),
  10. Map(BTreeMap<CborValue, CborValue>),
  11. Null,
  12. }
  13.  
  14. impl TryFrom<CborValue> for String {
  15. type Error = ();
  16.  
  17. fn try_from(cbor: CborValue) -> Result<String, ()> {
  18. if let CborValue::Text(string) = cbor {
  19. Ok(string)
  20. } else {
  21. Err(())
  22. }
  23. }
  24. }
  25.  
  26. fn foo(cbor: CborValue) -> Result<BTreeMap<String, CborValue>, ()> {
  27. match cbor {
  28. CborValue::Map(map) => {
  29. map.into_iter()
  30. .map(|(k, v)| Ok((String::try_from(k)?, v)))
  31. .collect()
  32. },
  33. value => return Err(()),
  34. }
  35. }
  36.  
  37. fn main() {}
Add Comment
Please, Sign In to add comment