spin_factors/runtime_config/
toml.rs1use std::{cell::RefCell, collections::HashSet};
4
5pub trait GetTomlValue {
7 fn get(&self, key: &str) -> Option<&toml::Value>;
8}
9
10impl GetTomlValue for toml::Table {
11 fn get(&self, key: &str) -> Option<&toml::Value> {
12 self.get(key)
13 }
14}
15
16#[derive(Debug, Clone)]
17pub struct TomlKeyTracker<'a> {
19 unused_keys: RefCell<HashSet<&'a str>>,
20 table: &'a toml::Table,
21}
22
23impl<'a> TomlKeyTracker<'a> {
24 pub fn new(table: &'a toml::Table) -> Self {
25 Self {
26 unused_keys: RefCell::new(table.keys().map(String::as_str).collect()),
27 table,
28 }
29 }
30
31 pub fn validate_all_keys_used(&self) -> crate::Result<()> {
32 if !self.unused_keys.borrow().is_empty() {
33 return Err(crate::Error::RuntimeConfigUnusedKeys {
34 keys: self
35 .unused_keys
36 .borrow()
37 .iter()
38 .map(|s| (*s).to_owned())
39 .collect(),
40 });
41 }
42 Ok(())
43 }
44}
45
46impl GetTomlValue for TomlKeyTracker<'_> {
47 fn get(&self, key: &str) -> Option<&toml::Value> {
48 self.unused_keys.borrow_mut().remove(key);
49 self.table.get(key)
50 }
51}
52
53impl AsRef<toml::Table> for TomlKeyTracker<'_> {
54 fn as_ref(&self) -> &toml::Table {
55 self.table
56 }
57}