1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use std::collections::HashMap; use std::cell::RefCell; use std::fmt; pub enum Data { Null, String(String), Bool(bool), Vec(Vec<Data>), Map(HashMap<String, Data>), Fun(RefCell<Box<FnMut(String) -> String + Send>>), } impl PartialEq for Data { #[inline] fn eq(&self, other: &Data) -> bool { match (self, other) { (&Data::Null, &Data::Null) => true, (&Data::String(ref v0), &Data::String(ref v1)) => v0 == v1, (&Data::Bool(ref v0), &Data::Bool(ref v1)) => v0 == v1, (&Data::Vec(ref v0), &Data::Vec(ref v1)) => v0 == v1, (&Data::Map(ref v0), &Data::Map(ref v1)) => v0 == v1, (&Data::Fun(_), &Data::Fun(_)) => bug!("Cannot compare closures"), (_, _) => false, } } } impl fmt::Debug for Data { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Data::Null => write!(f, "Null"), Data::String(ref v) => write!(f, "StrVal({})", v), Data::Bool(v) => write!(f, "Bool({:?})", v), Data::Vec(ref v) => write!(f, "VecVal({:?})", v), Data::Map(ref v) => write!(f, "Map({:?})", v), Data::Fun(_) => write!(f, "Fun(...)"), } } }