diff --git a/tests/deser_test b/tests/deser_test index 7d8c7dc..773ad0b 100644 --- a/tests/deser_test +++ b/tests/deser_test @@ -67,93 +67,96 @@ fn cbor_bytes_sz(bytes: Vec, sz: cbor_event::StringLenSz) -> Vec { buf.finalize() } -fn print_cbor_types(obj_name: &str, vec: Vec) { - use cbor_event::Type; - let mut raw = cbor_event::de::Deserializer::from(vec); - let mut lens = Vec::new(); - let consume_elem = |lens: &mut Vec| { - if let Some(len) = lens.last_mut() { - if let cbor_event::LenSz::Len(n, _) = len { - *n -= 1; +#[cfg(test)] +mod tests { + fn print_cbor_types(obj_name: &str, vec: Vec) { + use cbor_event::Type; + let mut raw = cbor_event::de::Deserializer::from(vec); + let mut lens = Vec::new(); + let consume_elem = |lens: &mut Vec| { + if let Some(len) = lens.last_mut() { + if let cbor_event::LenSz::Len(n, _) = len { + *n -= 1; + } } - } - }; - let reduce_depth = |lens: &mut Vec| { - while let Some(cbor_event::LenSz::Len(0, _)) = lens.last() { - lens.pop(); - println!("{}}}", "\t".repeat(lens.len())); - } - }; - println!("{} = {{", obj_name); - loop { - print!("{}", "\t".repeat(lens.len())); - match raw.cbor_type() { - Err(_) => break, - Ok(Type::UnsignedInteger) => { - let (x, sz) = raw.unsigned_integer_sz().unwrap(); - println!("UINT({}, {:?})", x, sz); - consume_elem(&mut lens); - reduce_depth(&mut lens); - }, - Ok(Type::NegativeInteger) => { - let (x, sz) = raw.negative_integer_sz().unwrap(); - println!("NINT({}, {:?})", x, sz); - consume_elem(&mut lens); - reduce_depth(&mut lens); - }, - Ok(Type::Bytes) => { - let (x, sz) = raw.bytes_sz().unwrap(); - println!("BYTES({:?}, {:?})", x, sz); - consume_elem(&mut lens); - reduce_depth(&mut lens); - }, - Ok(Type::Text) => { - let (x, sz) = raw.text_sz().unwrap(); - println!("TEXT(\"{}\", {:?})", x, sz); - consume_elem(&mut lens); - reduce_depth(&mut lens); - }, - Ok(Type::Array) => { - let len = raw.array_sz().unwrap(); - println!("ARRAY({:?}) {{", len); - consume_elem(&mut lens); - lens.push(len); - if let cbor_event::LenSz::Len(0, _sz) = len { + }; + let reduce_depth = |lens: &mut Vec| { + while let Some(cbor_event::LenSz::Len(0, _)) = lens.last() { + lens.pop(); + println!("{}}}", "\t".repeat(lens.len())); + } + }; + println!("{} = {{", obj_name); + loop { + print!("{}", "\t".repeat(lens.len())); + match raw.cbor_type() { + Err(_) => break, + Ok(Type::UnsignedInteger) => { + let (x, sz) = raw.unsigned_integer_sz().unwrap(); + println!("UINT({}, {:?})", x, sz); + consume_elem(&mut lens); reduce_depth(&mut lens); - } - }, - Ok(Type::Map) => { - let len = raw.map_sz().unwrap(); - println!("MAP({:?}) {{", len); - consume_elem(&mut lens); - lens.push(match len { - cbor_event::LenSz::Len(n, sz) => cbor_event::LenSz::Len(2 * n, sz), - cbor_event::LenSz::Indefinite => cbor_event::LenSz::Indefinite, - }); - if let cbor_event::LenSz::Len(0, _sz) = len { + }, + Ok(Type::NegativeInteger) => { + let (x, sz) = raw.negative_integer_sz().unwrap(); + println!("NINT({}, {:?})", x, sz); + consume_elem(&mut lens); reduce_depth(&mut lens); - } - }, - Ok(Type::Tag) => { - let (tag, sz) = raw.tag_sz().unwrap(); - println!("TAG({}, {:?})", tag, sz); - }, - Ok(Type::Special) => { - let special = raw.special().unwrap(); - println!("SPECIAL({:?})", special); - if special == cbor_event::Special::Break { - if let Some(cbor_event::LenSz::Indefinite) = lens.last() { - lens.pop(); + }, + Ok(Type::Bytes) => { + let (x, sz) = raw.bytes_sz().unwrap(); + println!("BYTES({:?}, {:?})", x, sz); + consume_elem(&mut lens); + reduce_depth(&mut lens); + }, + Ok(Type::Text) => { + let (x, sz) = raw.text_sz().unwrap(); + println!("TEXT(\"{}\", {:?})", x, sz); + consume_elem(&mut lens); + reduce_depth(&mut lens); + }, + Ok(Type::Array) => { + let len = raw.array_sz().unwrap(); + println!("ARRAY({:?}) {{", len); + consume_elem(&mut lens); + lens.push(len); + if let cbor_event::LenSz::Len(0, _sz) = len { reduce_depth(&mut lens); - } else { - panic!("unexpected break"); } - } else { + }, + Ok(Type::Map) => { + let len = raw.map_sz().unwrap(); + println!("MAP({:?}) {{", len); consume_elem(&mut lens); - reduce_depth(&mut lens); - } - }, + lens.push(match len { + cbor_event::LenSz::Len(n, sz) => cbor_event::LenSz::Len(2 * n, sz), + cbor_event::LenSz::Indefinite => cbor_event::LenSz::Indefinite, + }); + if let cbor_event::LenSz::Len(0, _sz) = len { + reduce_depth(&mut lens); + } + }, + Ok(Type::Tag) => { + let (tag, sz) = raw.tag_sz().unwrap(); + println!("TAG({}, {:?})", tag, sz); + }, + Ok(Type::Special) => { + let special = raw.special().unwrap(); + println!("SPECIAL({:?})", special); + if special == cbor_event::Special::Break { + if let Some(cbor_event::LenSz::Indefinite) = lens.last() { + lens.pop(); + reduce_depth(&mut lens); + } else { + panic!("unexpected break"); + } + } else { + consume_elem(&mut lens); + reduce_depth(&mut lens); + } + }, + } } + println!("}}"); } - println!("}}"); }