use uor_foundation::enforcement::{Binding, BindingEntry, BindingsTable};
static BINDINGS: &[Binding] = &[
Binding {
name_index: 0,
type_index: 0,
value_index: 0,
surface: "x",
content_address: 0x0000_0000_0000_0001,
},
Binding {
name_index: 1,
type_index: 0,
value_index: 1,
surface: "y",
content_address: 0x0000_0000_0000_00ff,
},
Binding {
name_index: 2,
type_index: 0,
value_index: 2,
surface: "z",
content_address: 0x0000_0000_0000_ffff,
},
];
static ENTRIES: &[BindingEntry] = &[
BINDINGS[0].to_binding_entry(),
BINDINGS[1].to_binding_entry(),
BINDINGS[2].to_binding_entry(),
];
const TABLE: BindingsTable = match BindingsTable::try_new(ENTRIES) {
Ok(t) => t,
Err(_) => panic!("bindings are not in strict-ascending ContentAddress order"),
};
fn main() {
println!("Static BindingsTable with {} entries:", TABLE.entries.len());
for entry in TABLE.entries {
println!(
" address={:?} bytes={:?}",
entry.address,
core::str::from_utf8(entry.bytes).expect("surface is utf8")
);
}
let lookup_addr = BINDINGS[1].to_binding_entry().address;
let found = TABLE
.entries
.binary_search_by_key(&lookup_addr.as_u128(), |e| e.address.as_u128())
.map(|idx| &TABLE.entries[idx]);
match found {
Ok(entry) => println!(
"Lookup found: {:?}",
core::str::from_utf8(entry.bytes).unwrap_or("?")
),
Err(_) => println!("Lookup miss"),
}
}