reifydb_value/config/
i128.rs1use super::Config;
5
6impl Config {
7 pub fn i128(&self, key: &str) -> Option<i128> {
8 self.opt_coerce(key)
9 }
10
11 pub fn require_i128(&self, key: &str) -> i128 {
12 self.opt_coerce(key).unwrap_or_else(|| self.missing(key, "an integer"))
13 }
14
15 pub fn i128_or(&self, key: &str, default: i128) -> i128 {
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_width_that_fits() {
27 let cfg = config(vec![
28 ("a", Value::Int16(-170141183460469231731687303715884105728)),
29 ("b", Value::Uint8(u64::MAX)),
30 ("c", Value::Int4(-3)),
31 ]);
32 assert_eq!(cfg.i128("a"), Some(i128::MIN), "Int16 round-trips through i128");
33 assert_eq!(cfg.i128("b"), Some(u64::MAX as i128), "u64::MAX fits in i128");
34 assert_eq!(cfg.i128("c"), Some(-3));
35 }
36
37 #[test]
38 fn rejects_uint16_above_i128_max() {
39 let cfg = config(vec![("u", Value::Uint16(i128::MAX as u128 + 1))]);
40 assert_eq!(cfg.i128("u"), None, "Uint16 above i128::MAX is range-checked and rejected");
41 }
42
43 #[test]
44 fn rejects_non_integer() {
45 let cfg = config(vec![("f", Value::float8(1.0)), ("b", Value::Boolean(true))]);
46 assert_eq!(cfg.i128("f"), None);
47 assert_eq!(cfg.i128("b"), None);
48 }
49
50 #[test]
51 fn or_and_require_behavior() {
52 let cfg = config(vec![("present", Value::Int16(-9))]);
53 assert_eq!(cfg.i128_or("present", 1), -9);
54 assert_eq!(cfg.i128_or("absent", 1), 1);
55 assert_eq!(cfg.require_i128("present"), -9);
56 }
57
58 #[test]
59 #[should_panic(expected = "is missing or not an integer")]
60 fn require_panics_when_missing() {
61 let cfg = config(vec![]);
62 cfg.require_i128("k");
63 }
64}