-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from oramasearch/feat/adds-utils
feat: adds utils
- Loading branch information
Showing
5 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ members = [ | |
"tanstack_example", | ||
"code_parser", | ||
"vector_index", | ||
"utils" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "utils" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[[bin]] | ||
name = "test" | ||
path = "src/bin/test.rs" | ||
|
||
[dependencies] | ||
serde_json = "1.0.132" | ||
regex = "1.11.1" | ||
anyhow = "1.0.93" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use anyhow::Result; | ||
use regex::Regex; | ||
use serde_json::Value; | ||
|
||
pub fn parse_json_safely(input_str: &str) -> Result<Value> { | ||
if let Ok(parsed) = serde_json::from_str(input_str.trim()) { | ||
return Ok(parsed); | ||
} | ||
|
||
let trimmed_input = input_str.trim(); | ||
|
||
let json_regex = Regex::new(r"```(?:json)?\s*([\s\S]*?)\s*```|(\{[\s\S]*\}|\[[\s\S]*\])") | ||
.expect("Failed to compile regex"); | ||
|
||
if let Some(captures) = json_regex.captures(trimmed_input) { | ||
if let Some(json_string) = captures.get(1).or_else(|| captures.get(2)) { | ||
if let Ok(parsed) = serde_json::from_str(json_string.as_str().trim()) { | ||
return Ok(parsed); | ||
} | ||
} | ||
} | ||
|
||
anyhow::bail!("Failed to parse JSON") | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use serde_json::json; | ||
|
||
#[test] | ||
fn test_parse_valid_json() { | ||
let input = r#"{"key": "value"}"#; | ||
let result = parse_json_safely(input); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), json!({"key": "value"})); | ||
} | ||
|
||
#[test] | ||
fn test_parse_json_in_code_block() { | ||
let input = r#"```json | ||
{ | ||
"name": "John", | ||
"age": 30 | ||
} | ||
```"#; | ||
let result = parse_json_safely(input); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), json!({"name": "John", "age": 30})); | ||
} | ||
|
||
#[test] | ||
fn test_parse_json_without_language_specifier() { | ||
let input = r#"``` | ||
{ | ||
"status": "ok" | ||
} | ||
```"#; | ||
let result = parse_json_safely(input); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), json!({"status": "ok"})); | ||
} | ||
|
||
#[test] | ||
fn test_parse_partial_json() { | ||
let input = r#" | ||
Some text before | ||
{ | ||
"foo": "bar" | ||
} | ||
Some text after | ||
"#; | ||
let result = parse_json_safely(input); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), json!({"foo": "bar"})); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_json_returns_error() { | ||
let input = r#"Invalid JSON string"#; | ||
let result = parse_json_safely(input); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_incomplete_json_structure() { | ||
let input = r#" | ||
{ | ||
"incomplete": true, | ||
"#; | ||
let result = parse_json_safely(input); | ||
assert!(result.is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_json_in_plain_text_with_extra_text() { | ||
let input = r#" | ||
Here is some text and then: | ||
{ | ||
"key": "value" | ||
} | ||
Followed by more text. | ||
"#; | ||
let result = parse_json_safely(input); | ||
assert!(result.is_ok()); | ||
assert_eq!(result.unwrap(), json!({"key": "value"})); | ||
} | ||
} |