Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(try_from)]
- #![feature(never_type)]
- use ::std::collections::BTreeMap;
- use ::std::convert::TryFrom;
- #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
- enum CborValue {
- Text(String),
- Map(BTreeMap<CborValue, CborValue>),
- Null,
- }
- impl TryFrom<CborValue> for String {
- type Error = ();
- fn try_from(cbor: CborValue) -> Result<String, ()> {
- if let CborValue::Text(string) = cbor {
- Ok(string)
- } else {
- Err(())
- }
- }
- }
- fn foo(cbor: CborValue) -> Result<BTreeMap<String, CborValue>, ()> {
- match cbor {
- CborValue::Map(map) => {
- map.into_iter()
- .map(|(k, v)| Ok((String::try_from(k)?, v)))
- .collect()
- },
- value => return Err(()),
- }
- }
- fn main() {}
Add Comment
Please, Sign In to add comment