use serde_json::Value;
use std::fs;
use std::path::Path;
use quick_xml::de::from_str as xml_from_str;
pub fn yaml_load_once(yaml_file: &str, clm_root: &str) -> crate::error::Result<String> {
let path = Path::new(yaml_file);
if !path.exists() {
return Err(crate::error::ClmError::Evaluation(format!("File not found: {}", yaml_file)));
}
let content_hash = crate::fs_utils::compute_stripped_file_hash(path)
.map_err(|e| crate::error::ClmError::Evaluation(format!("Failed to compute file hash: {}", e)))?;
let ast_handle = format!("ast:{}", content_hash);
if let Some(cached_ast) = crate::cache::getter_mcard_cache_handle(&ast_handle)
&& let Some(content) = crate::cache::getter_mcard_cache(&cached_ast) {
return Ok(content);
}
if let Ok(Some(cached_content)) = crate::db::resolve_handle_content(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, clm_root, &ast_handle) {
if let Ok(Some(current_hash)) = crate::db::resolve_handle_hash(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, clm_root, &ast_handle) {
crate::cache::setter_mcard_cache_handle(&ast_handle, ¤t_hash);
crate::cache::setter_mcard_cache(¤t_hash, &cached_content);
}
return Ok(cached_content);
}
let json_str = parse_to_mcards(yaml_file)?;
let _ = crate::memorize::memoize_clm_state(yaml_file, &content_hash, &json_str, clm_root);
Ok(json_str)
}
pub fn parse_to_mcards(file_path: &str) -> crate::error::Result<String> {
let path = Path::new(file_path);
if !path.exists() {
return Err(crate::error::ClmError::Evaluation(format!("File not found: {}", file_path)));
}
let extension = path.extension().and_then(|s| s.to_str()).unwrap_or("");
let mut parsed: Value = match extension {
"xml" => {
let xml_content = fs::read_to_string(path)?;
xml_from_str(&xml_content)
.map_err(|e| crate::error::ClmError::Evaluation(format!("Failed to parse XML: {}", e)))?
}
"jsonl" => {
let content = fs::read_to_string(path)?;
let mut array = Vec::new();
for line in content.lines() {
let trimmed = line.trim();
if !trimmed.is_empty() {
let val: Value = serde_json::from_str(trimmed)?;
array.push(val);
}
}
Value::Array(array)
}
_ => {
let yaml_content = fs::read_to_string(path)?;
match serde_yaml::from_str(&yaml_content) {
Ok(val) => val,
Err(original_err) => {
if let Some(block_content) = extract_yaml_code_block(&yaml_content) {
serde_yaml::from_str(&block_content)?
} else {
return Err(crate::error::ClmError::YamlParse(original_err));
}
}
}
}
};
normalize_numbers(&mut parsed);
Ok(serde_json::to_string_pretty(&parsed)?)
}
fn normalize_numbers(value: &mut Value) {
match value {
Value::Number(num) => {
if let Some(f) = num.as_f64()
&& f.fract() == 0.0 {
if f >= 0.0 {
if let Some(u) = num.as_u64().or(Some(f as u64)) {
*value = Value::Number(serde_json::Number::from(u));
}
} else {
if let Some(i) = num.as_i64().or(Some(f as i64)) {
*value = Value::Number(serde_json::Number::from(i));
}
}
}
}
Value::Object(map) => {
for val in map.values_mut() {
normalize_numbers(val);
}
}
Value::Array(arr) => {
for val in arr.iter_mut() {
normalize_numbers(val);
}
}
_ => {}
}
}
pub fn evaluate_jq(json: &Value, query: &str) -> Value {
let parts: Vec<&str> = query.split("//").map(|s| s.trim()).collect();
for part in parts {
let val = evaluate_single_query(json, part);
if !val.is_null() && val != Value::String("null".to_string()) {
return val;
}
}
Value::Null
}
fn evaluate_single_query(json: &Value, query: &str) -> Value {
let mut current = json;
let trimmed = if query.starts_with('.') {
&query[1..]
} else {
query
};
if trimmed.is_empty() {
return current.clone();
}
let components = trimmed.split('.');
for comp in components {
if comp.is_empty() {
continue;
}
if comp.contains('[') && comp.ends_with(']') {
let parts: Vec<&str> = comp.split('[').collect();
if parts.len() == 2 {
let key = parts[0];
let idx_str = parts[1].trim_end_matches(']');
if let Ok(idx) = idx_str.parse::<usize>() {
let sub_val = if key.is_empty() {
current
} else if let Some(v) = current.get(key) {
v
} else {
return Value::Null;
};
if let Some(array_val) = sub_val.get(idx) {
current = array_val;
} else {
return Value::Null;
}
} else {
return Value::Null;
}
} else {
return Value::Null;
}
} else {
if let Some(v) = current.get(comp) {
current = v;
} else {
return Value::Null;
}
}
}
current.clone()
}
pub fn yaml_get_field(json_str: &str, jq_path: &str) -> String {
if json_str.is_empty() || json_str == "null" {
return "null".to_string();
}
let parsed: Value = match serde_json::from_str(json_str) {
Ok(v) => v,
Err(_) => return "null".to_string(),
};
let resolved = evaluate_jq(&parsed, jq_path);
match resolved {
Value::Null => "null".to_string(),
Value::String(s) => s,
other => other.to_string(),
}
}
pub fn yaml_get_array_length(json_str: &str, jq_path: &str) -> String {
if json_str.is_empty() || json_str == "null" {
return "0".to_string();
}
let parsed: Value = match serde_json::from_str(json_str) {
Ok(v) => v,
Err(_) => return "0".to_string(),
};
let resolved = evaluate_jq(&parsed, jq_path);
match resolved {
Value::Array(arr) => arr.len().to_string(),
_ => "0".to_string(),
}
}
pub fn yaml_get_array_item(json_str: &str, jq_path: &str, index: usize) -> String {
if json_str.is_empty() || json_str == "null" {
return "null".to_string();
}
let parsed: Value = match serde_json::from_str(json_str) {
Ok(v) => v,
Err(_) => return "null".to_string(),
};
let full_path = format!("{}[{}]", jq_path, index);
let resolved = evaluate_jq(&parsed, &full_path);
match resolved {
Value::Null => "null".to_string(),
Value::String(s) => s,
other => other.to_string(),
}
}
fn extract_yaml_code_block(content: &str) -> Option<String> {
let mut in_block = false;
let mut block_lines = Vec::new();
for line in content.lines() {
let trimmed = line.trim();
if trimmed.starts_with("```") {
if in_block {
return Some(block_lines.join("\n"));
} else {
let lang = trimmed.trim_start_matches('`').trim().to_lowercase();
if lang.is_empty() || lang == "yaml" || lang == "yml" || lang == "json" {
in_block = true;
}
}
} else if in_block {
block_lines.push(line);
}
}
None
}