|
| 1 | +use crate::custom_error::MapErrToString; |
| 2 | + |
| 3 | +pub fn extract_json_object<T: for<'de> serde::Deserialize<'de>>(text: &str) -> Result<T, String> { |
| 4 | + let start = text.find('{').ok_or_else(|| "No opening brace '{' found".to_string())?; |
| 5 | + let end = text.rfind('}').ok_or_else(|| "No closing brace '}' found".to_string())?; |
| 6 | + |
| 7 | + if end <= start { |
| 8 | + return Err("Closing brace appears before opening brace".to_string()); |
| 9 | + } |
| 10 | + |
| 11 | + serde_json::from_str(&text[start..=end]).map_err_to_string() |
| 12 | +} |
| 13 | + |
| 14 | +#[cfg(test)] |
| 15 | +mod tests { |
| 16 | + use super::*; |
| 17 | + use serde_json::json; |
| 18 | + use serde::Deserialize; |
| 19 | + use indexmap::IndexMap; |
| 20 | + |
| 21 | + #[test] |
| 22 | + fn test_extract_json_clean_input() { |
| 23 | + let input = r#"{"key": "value", "number": 42}"#; |
| 24 | + let result: Result<serde_json::Value, _> = extract_json_object(input); |
| 25 | + assert!(result.is_ok()); |
| 26 | + assert_eq!(result.unwrap(), json!({"key": "value", "number": 42})); |
| 27 | + } |
| 28 | + |
| 29 | + #[test] |
| 30 | + fn test_extract_json_with_text_before_after() { |
| 31 | + let input = "Some text before\n {\"key\": \"value\",\n \"number\": 42}\n\n and text after"; |
| 32 | + let result: Result<serde_json::Value, _> = extract_json_object(input); |
| 33 | + assert!(result.is_ok()); |
| 34 | + assert_eq!(result.unwrap(), json!({"key": "value", "number": 42})); |
| 35 | + } |
| 36 | + |
| 37 | + #[test] |
| 38 | + fn test_extract_json_nested() { |
| 39 | + let input = r#"LLM response: {"FOUND": {"file1.rs": "symbol1,symbol2"}, "SIMILAR": {"file2.rs": "symbol3"}}"#; |
| 40 | + let result: Result<IndexMap<String, IndexMap<String, String>>, _> = extract_json_object(input); |
| 41 | + assert!(result.is_ok()); |
| 42 | + |
| 43 | + let map = result.unwrap(); |
| 44 | + assert_eq!(map.len(), 2); |
| 45 | + assert_eq!(map.get("FOUND").unwrap().get("file1.rs").unwrap(), "symbol1,symbol2"); |
| 46 | + assert_eq!(map.get("SIMILAR").unwrap().get("file2.rs").unwrap(), "symbol3"); |
| 47 | + } |
| 48 | + |
| 49 | + #[derive(Deserialize, Debug, PartialEq)] |
| 50 | + struct FollowUpResponse { |
| 51 | + pub follow_ups: Vec<String>, |
| 52 | + pub topic_changed: bool, |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn test_follow_up_response_format() { |
| 57 | + let input = r#" |
| 58 | + Here are the follow up questions: |
| 59 | + ```json |
| 60 | + { |
| 61 | + "follow_ups": ["How?", "More examples", "Thank you"], |
| 62 | + "topic_changed": false |
| 63 | + } |
| 64 | + ``` |
| 65 | + "#; |
| 66 | + |
| 67 | + let result: Result<FollowUpResponse, _> = extract_json_object(input); |
| 68 | + |
| 69 | + assert!(result.is_ok()); |
| 70 | + let response = result.unwrap(); |
| 71 | + |
| 72 | + assert_eq!(response.follow_ups, vec!["How?", "More examples", "Thank you"]); |
| 73 | + assert_eq!(response.topic_changed, false); |
| 74 | + } |
| 75 | +} |
0 commit comments