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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
use iref::IriBuf; use crate::{ Id, Indexed, Object }; pub enum Expanded<T: Id = IriBuf> { Null, Object(Indexed<Object<T>>), Array(Vec<Indexed<Object<T>>>) } impl<T: Id> Expanded<T> { pub fn len(&self) -> usize { match self { Expanded::Null => 0, Expanded::Object(_) => 1, Expanded::Array(ary) => ary.len() } } pub fn is_null(&self) -> bool { match self { Expanded::Null => true, _ => false } } pub fn is_list(&self) -> bool { match self { Expanded::Object(o) => o.is_list(), _ => false } } pub fn iter(&self) -> Iter<T> { match self { Expanded::Null => Iter::Null, Expanded::Object(ref o) => Iter::Object(Some(o)), Expanded::Array(ary) => Iter::Array(ary.iter()) } } } impl<T: Id> IntoIterator for Expanded<T> { type Item = Indexed<Object<T>>; type IntoIter = IntoIter<T>; fn into_iter(self) -> IntoIter<T> { match self { Expanded::Null => IntoIter::Null, Expanded::Object(o) => IntoIter::Object(Some(o)), Expanded::Array(ary) => IntoIter::Array(ary.into_iter()) } } } impl<'a, T: Id> IntoIterator for &'a Expanded<T> { type Item = &'a Indexed<Object<T>>; type IntoIter = Iter<'a, T>; fn into_iter(self) -> Iter<'a, T> { self.iter() } } pub enum Iter<'a, T: Id> { Null, Object(Option<&'a Indexed<Object<T>>>), Array(std::slice::Iter<'a, Indexed<Object<T>>>) } impl<'a, T: Id> Iterator for Iter<'a, T> { type Item = &'a Indexed<Object<T>>; fn next(&mut self) -> Option<&'a Indexed<Object<T>>> { match self { Iter::Null => None, Iter::Object(ref mut o) => { let mut result = None; std::mem::swap(o, &mut result); result }, Iter::Array(ref mut it) => { it.next() } } } } pub enum IntoIter<T: Id> { Null, Object(Option<Indexed<Object<T>>>), Array(std::vec::IntoIter<Indexed<Object<T>>>) } impl<T: Id> Iterator for IntoIter<T> { type Item = Indexed<Object<T>>; fn next(&mut self) -> Option<Indexed<Object<T>>> { match self { IntoIter::Null => None, IntoIter::Object(ref mut o) => { let mut result = None; std::mem::swap(o, &mut result); result }, IntoIter::Array(ref mut it) => { it.next() } } } } impl<T: Id> From<Indexed<Object<T>>> for Expanded<T> { fn from(obj: Indexed<Object<T>>) -> Expanded<T> { Expanded::Object(obj) } } impl<T: Id> From<Vec<Indexed<Object<T>>>> for Expanded<T> { fn from(list: Vec<Indexed<Object<T>>>) -> Expanded<T> { Expanded::Array(list) } }