Skip to main content

reifydb_value/config/
uint.rs

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