Skip to main content

hara_native/lang/protocol/
ilookup.rs

1use super::IFind;
2use hara_protocol_macros::hara_protocol;
3
4#[hara_protocol(
5    namespace = "std.protocol.ilookup",
6    name = "ILookup",
7    parents = ["IFind"]
8)]
9pub trait ILookup<K, V>: IFind<K, Output = (K, V)>
10where
11    K: Clone,
12    V: Clone,
13{
14    type Keys: Iterator<Item = K>;
15    type Values: Iterator<Item = V>;
16    fn keys(&self) -> Self::Keys;
17    fn vals(&self) -> Self::Values;
18    #[hara_method(
19        value = "lookup",
20        arity = -1,
21        variadic = true,
22        min_arity = 2,
23        max_arity = 3,
24        whole_wasm
25    )]
26    fn lookup(&self, key: &K) -> Option<V> {
27        self.find(key).map(|(_, value)| value)
28    }
29    fn lookup_or(&self, key: &K, not_found: V) -> V {
30        self.lookup(key).unwrap_or(not_found)
31    }
32}