reifydb_value/config/
identity.rs1use super::Config;
5use crate::value::identity::IdentityId;
6
7impl Config {
8 pub fn identity_id(&self, key: &str) -> Option<IdentityId> {
9 self.opt(key)
10 }
11
12 pub fn require_identity_id(&self, key: &str) -> IdentityId {
13 self.opt(key).unwrap_or_else(|| self.missing(key, "an identity id"))
14 }
15
16 pub fn identity_id_or(&self, key: &str, default: IdentityId) -> IdentityId {
17 self.opt(key).unwrap_or(default)
18 }
19}
20
21#[cfg(test)]
22mod tests {
23 use super::super::testutil::config;
24 use crate::value::{Value, identity::IdentityId};
25
26 #[test]
27 fn casts_identity_id_values() {
28 let id = IdentityId::root();
29 let cfg = config(vec![("id", Value::IdentityId(id))]);
30 assert_eq!(cfg.identity_id("id"), Some(id));
31 }
32
33 #[test]
34 fn rejects_other_values() {
35 let cfg = config(vec![("n", Value::Uint8(1)), ("s", Value::utf8("root"))]);
36 assert_eq!(cfg.identity_id("n"), None, "an integer is not an identity id");
37 assert_eq!(cfg.identity_id("s"), None, "a string is not an identity id");
38 }
39
40 #[test]
41 fn or_and_require_behavior() {
42 let id = IdentityId::root();
43 let default = IdentityId::anonymous();
44 let cfg = config(vec![("present", Value::IdentityId(id))]);
45 assert_eq!(cfg.identity_id_or("present", default), id);
46 assert_eq!(cfg.identity_id_or("absent", default), default);
47 assert_eq!(cfg.require_identity_id("present"), id);
48 }
49
50 #[test]
51 #[should_panic(expected = "is missing or not an identity id")]
52 fn require_panics_when_missing() {
53 let cfg = config(vec![]);
54 cfg.require_identity_id("k");
55 }
56}