use std::cell::RefCell;
use dcbor::{prelude::*, walk::WalkElement};
fn main() {
println!("=== Testing stop flag behavior ===\n");
let mut inner_map = Map::new();
inner_map.insert("inner_key", "inner_value");
let mut map = Map::new();
map.insert("first", "stop_here"); map.insert("second", inner_map); map.insert("third", vec![1, 2, 3]); let cbor = CBOR::from(map);
println!("CBOR structure: {}", cbor.diagnostic_flat());
println!("\n=== Test 1: Stop on single element ===");
let visited = RefCell::new(Vec::new());
cbor.walk((), &|element, depth, edge, state| {
let indent = " ".repeat(depth);
let description = match element {
WalkElement::Single(cbor) => {
format!("Single: {}", cbor.diagnostic_flat())
}
WalkElement::KeyValue { key, value } => format!(
"KV: {} => {}",
key.diagnostic_flat(),
value.diagnostic_flat()
),
};
visited
.borrow_mut()
.push(format!("{}[{:?}] {}", indent, edge, description));
let should_stop = if let Some(single) = element.as_single() {
matches!(single.as_case(), CBORCase::Text(s) if s == "stop_here")
} else {
false
};
if should_stop {
println!("{}🛑 STOPPING at: {}", indent, description);
} else {
println!("{}✅ Visited: {}", indent, description);
}
(state, should_stop)
});
println!("\nAll visits in order:");
for visit in visited.borrow().iter() {
println!(" {}", visit);
}
println!("\n=== Test 2: Stop on key-value pair ===");
let visited2 = RefCell::new(Vec::new());
cbor.walk((), &|element, depth, edge, state| {
let indent = " ".repeat(depth);
let description = match element {
WalkElement::Single(cbor) => {
format!("Single: {}", cbor.diagnostic_flat())
}
WalkElement::KeyValue { key, value } => format!(
"KV: {} => {}",
key.diagnostic_flat(),
value.diagnostic_flat()
),
};
visited2
.borrow_mut()
.push(format!("{}[{:?}] {}", indent, edge, description));
let should_stop = if let Some((_key, value)) = element.as_key_value() {
matches!(value.as_case(), CBORCase::Text(s) if s == "stop_here")
} else {
false
};
if should_stop {
println!("{}🛑 STOPPING at: {}", indent, description);
} else {
println!("{}✅ Visited: {}", indent, description);
}
(state, should_stop)
});
println!("\nAll visits in order:");
for visit in visited2.borrow().iter() {
println!(" {}", visit);
}
}