Skip to content

Commit

Permalink
Define print_cbor_types only in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonIT committed Mar 13, 2023
1 parent 78a4a4b commit 439ef67
Showing 1 changed file with 84 additions and 81 deletions.
165 changes: 84 additions & 81 deletions tests/deser_test
Original file line number Diff line number Diff line change
Expand Up @@ -67,93 +67,96 @@ fn cbor_bytes_sz(bytes: Vec<u8>, sz: cbor_event::StringLenSz) -> Vec<u8> {
buf.finalize()
}

fn print_cbor_types(obj_name: &str, vec: Vec<u8>) {
use cbor_event::Type;
let mut raw = cbor_event::de::Deserializer::from(vec);
let mut lens = Vec::new();
let consume_elem = |lens: &mut Vec<cbor_event::LenSz>| {
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<u8>) {
use cbor_event::Type;
let mut raw = cbor_event::de::Deserializer::from(vec);
let mut lens = Vec::new();
let consume_elem = |lens: &mut Vec<cbor_event::LenSz>| {
if let Some(len) = lens.last_mut() {
if let cbor_event::LenSz::Len(n, _) = len {
*n -= 1;
}
}
}
};
let reduce_depth = |lens: &mut Vec<cbor_event::LenSz>| {
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<cbor_event::LenSz>| {
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!("}}");
}

0 comments on commit 439ef67

Please sign in to comment.