reifydb_value/config/
decimal.rs1use super::Config;
5use crate::value::decimal::Decimal;
6
7impl Config {
8 pub fn decimal(&self, key: &str) -> Option<Decimal> {
9 self.opt(key)
10 }
11
12 pub fn require_decimal(&self, key: &str) -> Decimal {
13 self.opt(key).unwrap_or_else(|| self.missing(key, "a decimal"))
14 }
15
16 pub fn decimal_or(&self, key: &str, default: Decimal) -> Decimal {
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, decimal::Decimal};
25
26 #[test]
27 fn casts_decimal_values() {
28 let d = Decimal::from_i64(50);
29 let cfg = config(vec![("d", Value::Decimal(d.clone()))]);
30 assert_eq!(cfg.decimal("d"), Some(d));
31 }
32
33 #[test]
34 fn fixed_width_number_does_not_satisfy_decimal() {
35 let cfg = config(vec![("f", Value::float8(50.0)), ("i", Value::Int8(50))]);
36 assert_eq!(cfg.decimal("f"), None, "a float is a distinct variant from a decimal");
37 assert_eq!(cfg.decimal("i"), None, "a fixed-width integer is not a decimal");
38 }
39
40 #[test]
41 fn or_and_require_behavior() {
42 let d = Decimal::from_i64(50);
43 let default = Decimal::from_i64(0);
44 let cfg = config(vec![("present", Value::Decimal(d.clone()))]);
45 assert_eq!(cfg.decimal_or("present", default.clone()), d);
46 assert_eq!(cfg.decimal_or("absent", default.clone()), default);
47 assert_eq!(cfg.require_decimal("present"), d);
48 }
49
50 #[test]
51 #[should_panic(expected = "is missing or not a decimal")]
52 fn require_panics_when_missing() {
53 let cfg = config(vec![]);
54 cfg.require_decimal("k");
55 }
56}