Skip to main content

reifydb_value/config/
int.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use super::Config;
5use crate::value::int::Int;
6
7impl Config {
8	pub fn int(&self, key: &str) -> Option<Int> {
9		self.opt(key)
10	}
11
12	pub fn require_int(&self, key: &str) -> Int {
13		self.opt(key).unwrap_or_else(|| self.missing(key, "an integer"))
14	}
15
16	pub fn int_or(&self, key: &str, default: Int) -> Int {
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, int::Int};
25
26	#[test]
27	fn casts_bignum_int_values() {
28		let n = Int::from_i64(42);
29		let cfg = config(vec![("n", Value::Int(n.clone()))]);
30		assert_eq!(cfg.int("n"), Some(n));
31	}
32
33	#[test]
34	fn fixed_width_int_does_not_satisfy_bignum() {
35		let cfg = config(vec![("fixed", Value::Int8(42))]);
36		assert_eq!(
37			cfg.int("fixed"),
38			None,
39			"a fixed-width Int8 is a distinct variant from the arbitrary-precision Int"
40		);
41	}
42
43	#[test]
44	fn or_and_require_behavior() {
45		let n = Int::from_i64(42);
46		let default = Int::from_i64(0);
47		let cfg = config(vec![("present", Value::Int(n.clone()))]);
48		assert_eq!(cfg.int_or("present", default.clone()), n);
49		assert_eq!(cfg.int_or("absent", default.clone()), default);
50		assert_eq!(cfg.require_int("present"), n);
51	}
52
53	#[test]
54	#[should_panic(expected = "is missing or not an integer")]
55	fn require_panics_when_missing() {
56		let cfg = config(vec![]);
57		cfg.require_int("k");
58	}
59}