Skip to main content

reifydb_value/config/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::collections::BTreeMap;
5
6use crate::value::{
7	Value,
8	try_from::{TryFromValue, TryFromValueCoerce},
9};
10
11pub mod bool;
12pub mod date;
13pub mod datetime;
14pub mod decimal;
15pub mod dictionary;
16pub mod duration;
17pub mod f32;
18pub mod f64;
19pub mod i128;
20pub mod i16;
21pub mod i32;
22pub mod i64;
23pub mod i8;
24pub mod identity;
25pub mod int;
26pub mod string;
27pub mod time;
28pub mod u128;
29pub mod u16;
30pub mod u32;
31pub mod u64;
32pub mod u8;
33pub mod uint;
34pub mod usize;
35pub mod window;
36
37#[derive(Debug, Clone)]
38pub struct Config {
39	name: String,
40	values: BTreeMap<String, Value>,
41}
42
43impl Config {
44	pub fn new(name: impl Into<String>, values: BTreeMap<String, Value>) -> Self {
45		Self {
46			name: name.into(),
47			values,
48		}
49	}
50
51	pub fn name(&self) -> &str {
52		&self.name
53	}
54
55	pub fn get(&self, key: &str) -> Option<&Value> {
56		self.values.get(key)
57	}
58
59	pub fn contains(&self, key: &str) -> bool {
60		self.values.contains_key(key)
61	}
62
63	pub fn iter(&self) -> impl Iterator<Item = (&String, &Value)> {
64		self.values.iter()
65	}
66
67	fn opt<T: TryFromValue>(&self, key: &str) -> Option<T> {
68		self.values.get(key).and_then(T::from_value)
69	}
70
71	fn opt_coerce<T: TryFromValueCoerce>(&self, key: &str) -> Option<T> {
72		self.values.get(key).and_then(T::from_value_coerce)
73	}
74
75	fn missing(&self, key: &str, expected: &str) -> ! {
76		panic!("{}: required config '{}' is missing or not {}", self.name, key, expected)
77	}
78}
79
80#[cfg(test)]
81pub(super) mod testutil {
82	use std::collections::BTreeMap;
83
84	use super::Config;
85	use crate::value::Value;
86
87	pub fn config(pairs: Vec<(&str, Value)>) -> Config {
88		let values: BTreeMap<String, Value> = pairs.into_iter().map(|(k, v)| (k.to_string(), v)).collect();
89		Config::new("test_op", values)
90	}
91}