|
| 1 | +use toml_edit::{visit::*, Document, Item, Value}; |
| 2 | + |
| 3 | +#[derive(Default)] |
| 4 | +pub(crate) struct DeepKeysCollector<'doc> { |
| 5 | + current_path: Vec<&'doc str>, |
| 6 | + pub keys: Vec<String>, |
| 7 | +} |
| 8 | + |
| 9 | +impl DeepKeysCollector<'_> { |
| 10 | + pub fn get_keys(toml_string: String) -> Vec<String> { |
| 11 | + let document: Document = toml_string.parse().unwrap(); |
| 12 | + let mut visitor = DeepKeysCollector::default(); |
| 13 | + visitor.visit_document(&document); |
| 14 | + |
| 15 | + visitor.keys.sort(); |
| 16 | + visitor.keys |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl<'doc> Visit<'doc> for DeepKeysCollector<'doc> { |
| 21 | + fn visit_table_like_kv(&mut self, key: &'doc str, node: &'doc Item) { |
| 22 | + self.current_path.push(key); |
| 23 | + self.visit_item(node); |
| 24 | + self.current_path.pop(); |
| 25 | + } |
| 26 | + |
| 27 | + fn visit_value(&mut self, node: &'doc Value) { |
| 28 | + match node { |
| 29 | + Value::InlineTable(_table) => {} |
| 30 | + _ => { |
| 31 | + self.keys.push(self.current_path.join(".")); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + match node { |
| 36 | + Value::String(s) => self.visit_string(s), |
| 37 | + Value::Integer(i) => self.visit_integer(i), |
| 38 | + Value::Float(f) => self.visit_float(f), |
| 39 | + Value::Boolean(b) => self.visit_boolean(b), |
| 40 | + Value::Datetime(dt) => self.visit_datetime(dt), |
| 41 | + Value::Array(array) => self.visit_array(array), |
| 42 | + Value::InlineTable(table) => self.visit_inline_table(table), |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod tests { |
| 49 | + use toml_edit::Document; |
| 50 | + |
| 51 | + use crate::settings::Settings; |
| 52 | + |
| 53 | + use super::*; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn test_basic() { |
| 57 | + let input = r#" |
| 58 | +laputa = "sky-castle" |
| 59 | +the-force = { value = "surrounds-you" } |
| 60 | +"#; |
| 61 | + |
| 62 | + let document: Document = input.parse().unwrap(); |
| 63 | + let mut visitor = DeepKeysCollector::default(); |
| 64 | + visitor.visit_document(&document); |
| 65 | + |
| 66 | + assert_eq!(visitor.current_path, Vec::<&str>::new()); |
| 67 | + assert_eq!(visitor.keys, vec!["laputa", "the-force.value"]); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_default_config() { |
| 72 | + let input = toml::to_string_pretty(&Settings::new().unwrap()).unwrap(); |
| 73 | + |
| 74 | + let document: Document = input.parse().unwrap(); |
| 75 | + let mut visitor = DeepKeysCollector::default(); |
| 76 | + visitor.visit_document(&document); |
| 77 | + |
| 78 | + assert_eq!(visitor.current_path, Vec::<&str>::new()); |
| 79 | + visitor.keys.sort(); |
| 80 | + assert_eq!( |
| 81 | + visitor.keys, |
| 82 | + vec![ |
| 83 | + "allow_amend", |
| 84 | + "model_provider", |
| 85 | + "openai.api_key", |
| 86 | + "openai.model", |
| 87 | + "output.lang", |
| 88 | + "prompt.commit_summary", |
| 89 | + "prompt.commit_title", |
| 90 | + "prompt.file_diff", |
| 91 | + "prompt.translation", |
| 92 | + ] |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_get_keys() { |
| 98 | + let input = toml::to_string_pretty(&Settings::new().unwrap()).unwrap(); |
| 99 | + |
| 100 | + assert_eq!( |
| 101 | + DeepKeysCollector::get_keys(input), |
| 102 | + vec![ |
| 103 | + "allow_amend", |
| 104 | + "model_provider", |
| 105 | + "openai.api_key", |
| 106 | + "openai.model", |
| 107 | + "output.lang", |
| 108 | + "prompt.commit_summary", |
| 109 | + "prompt.commit_title", |
| 110 | + "prompt.file_diff", |
| 111 | + "prompt.translation", |
| 112 | + ] |
| 113 | + ); |
| 114 | + } |
| 115 | +} |
0 commit comments