Skip to main content

hara_native/lang/protocol/
ideps.rs

1use hara_protocol_macros::hara_protocol;
2
3#[hara_protocol(namespace = "std.protocol.ideps", name = "IDeps")]
4pub trait IDeps<K, E> {
5    type Entries: Iterator<Item = K>;
6    type Keys: Iterator<Item = K>;
7
8    #[hara_method(value = "dep-get", arity = 2)]
9    fn dep_get(&self, key: &K) -> Option<E>;
10    #[hara_method(value = "dep-entries", arity = 2)]
11    fn dep_entries(&self, key: &K) -> Self::Entries;
12    #[hara_method(value = "dep-keys", arity = 1)]
13    fn dep_keys(&self) -> Self::Keys;
14}
15
16#[cfg(test)]
17mod tests {
18    use super::IDeps;
19
20    struct Fixture;
21
22    impl IDeps<&'static str, &'static str> for Fixture {
23        type Entries = std::vec::IntoIter<&'static str>;
24        type Keys = std::array::IntoIter<&'static str, 2>;
25
26        fn dep_get(&self, key: &&'static str) -> Option<&'static str> {
27            (*key == "a").then_some("A")
28        }
29
30        fn dep_entries(&self, key: &&'static str) -> Self::Entries {
31            if *key == "a" {
32                vec!["b"].into_iter()
33            } else {
34                vec![].into_iter()
35            }
36        }
37
38        fn dep_keys(&self) -> Self::Keys {
39            ["a", "b"].into_iter()
40        }
41    }
42
43    #[test]
44    fn exposes_dependency_values_entries_and_keys() {
45        let fixture = Fixture;
46        assert_eq!(fixture.dep_get(&"a"), Some("A"));
47        assert_eq!(fixture.dep_entries(&"a").collect::<Vec<_>>(), vec!["b"]);
48        assert_eq!(fixture.dep_keys().collect::<Vec<_>>(), vec!["a", "b"]);
49    }
50}