use crate::converters::json::{json_to_document, json_to_dx};
use crate::llm::types::DxDocument;
pub fn toml_to_dx(toml_str: &str) -> Result<String, String> {
let value: toml::Value =
toml::from_str(toml_str).map_err(|e| format!("TOML parse error: {}", e))?;
let json_str =
serde_json::to_string(&value).map_err(|e| format!("JSON conversion error: {}", e))?;
json_to_dx(&json_str)
}
pub fn toml_to_document(toml_str: &str) -> Result<DxDocument, String> {
let value: toml::Value =
toml::from_str(toml_str).map_err(|e| format!("TOML parse error: {}", e))?;
let json_str =
serde_json::to_string(&value).map_err(|e| format!("JSON conversion error: {}", e))?;
json_to_document(&json_str)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_toml_to_dx() {
let toml = r#"
name = "test"
version = "1.0.0"
items = ["a", "b", "c"]
"#;
let dx = toml_to_dx(toml).unwrap();
assert!(dx.contains("n:test"));
}
#[test]
fn test_toml_to_document() {
let doc = toml_to_document(
r#"
name = "bun"
[install]
cache = true
"#,
)
.unwrap();
assert_eq!(doc.get_path("name").unwrap().as_str(), Some("bun"));
assert_eq!(doc.get_path("install.cache").unwrap().as_bool(), Some(true));
}
}