reifydb_value/config/
u64.rs1use super::Config;
5
6impl Config {
7 pub fn u64(&self, key: &str) -> Option<u64> {
8 self.opt_coerce(key)
9 }
10
11 pub fn require_u64(&self, key: &str) -> u64 {
12 self.opt_coerce(key).unwrap_or_else(|| self.missing(key, "an unsigned integer"))
13 }
14
15 pub fn u64_or(&self, key: &str, default: u64) -> u64 {
16 self.opt_coerce(key).unwrap_or(default)
17 }
18}
19
20#[cfg(test)]
21mod tests {
22 use super::super::testutil::config;
23 use crate::value::Value;
24
25 #[test]
26 fn casts_every_unsigned_width() {
27 let cfg = config(vec![
28 ("a", Value::Uint1(1)),
29 ("b", Value::Uint2(2)),
30 ("c", Value::Uint4(3)),
31 ("d", Value::Uint8(4)),
32 ("e", Value::Uint16(5)),
33 ]);
34 assert_eq!(cfg.u64("a"), Some(1));
35 assert_eq!(cfg.u64("b"), Some(2));
36 assert_eq!(cfg.u64("c"), Some(3));
37 assert_eq!(cfg.u64("d"), Some(4));
38 assert_eq!(cfg.u64("e"), Some(5), "Uint16 within range coerces to u64");
39 }
40
41 #[test]
42 fn casts_non_negative_signed_width() {
43 let cfg = config(vec![
44 ("a", Value::Int1(1)),
45 ("b", Value::Int2(2)),
46 ("c", Value::Int4(3)),
47 ("d", Value::Int8(4)),
48 ("e", Value::Int16(5)),
49 ]);
50 assert_eq!(cfg.u64("a"), Some(1));
51 assert_eq!(cfg.u64("b"), Some(2));
52 assert_eq!(cfg.u64("c"), Some(3));
53 assert_eq!(cfg.u64("d"), Some(4));
54 assert_eq!(cfg.u64("e"), Some(5), "non-negative Int16 coerces to u64");
55 }
56
57 #[test]
58 fn rejects_negative_signed() {
59 let cfg = config(vec![("a", Value::Int1(-1)), ("b", Value::Int4(-3)), ("c", Value::Int16(-7))]);
60 assert_eq!(cfg.u64("a"), None, "negative does not coerce to unsigned");
61 assert_eq!(cfg.u64("b"), None, "negative does not coerce to unsigned");
62 assert_eq!(cfg.u64("c"), None, "negative does not coerce to unsigned");
63 }
64
65 #[test]
66 fn rejects_uint16_above_u64_max() {
67 let cfg = config(vec![("a", Value::Uint16(u64::MAX as u128 + 1))]);
68 assert_eq!(cfg.u64("a"), None, "Uint16 above u64::MAX is range-checked and rejected");
69 }
70
71 #[test]
72 fn rejects_non_integer_values() {
73 let cfg = config(vec![("f", Value::float8(1.0)), ("s", Value::utf8("3")), ("b", Value::Boolean(true))]);
74 assert_eq!(cfg.u64("f"), None, "floats do not coerce to u64");
75 assert_eq!(cfg.u64("s"), None, "strings are not integers");
76 assert_eq!(cfg.u64("b"), None, "booleans are not integers");
77 }
78
79 #[test]
80 fn opt_and_or_handle_absent() {
81 let cfg = config(vec![("present", Value::Uint4(9))]);
82 assert_eq!(cfg.u64("absent"), None);
83 assert_eq!(cfg.u64_or("present", 1), 9);
84 assert_eq!(cfg.u64_or("absent", 1), 1);
85 }
86
87 #[test]
88 fn require_returns_value_when_present() {
89 let cfg = config(vec![("window_duration", Value::Uint8(60))]);
90 assert_eq!(cfg.require_u64("window_duration"), 60);
91 }
92
93 #[test]
94 #[should_panic(expected = "test_op: required config 'window_duration' is missing or not an unsigned integer")]
95 fn require_panics_when_missing() {
96 let cfg = config(vec![]);
97 cfg.require_u64("window_duration");
98 }
99}