Parse a YAML value string into a serde_json::Value.
(s: &str)
| 1443 | |
| 1444 | /// Parse a YAML value string into a serde_json::Value. |
| 1445 | fn parse_yaml_value(s: &str) -> serde_json::Value { |
| 1446 | // Quoted values remain strings even when their contents look like another |
| 1447 | // scalar type. Decode JSON-style double-quoted escapes losslessly. |
| 1448 | if s.len() >= 2 && s.starts_with('"') && s.ends_with('"') { |
| 1449 | return serde_json::from_str::<String>(s) |
| 1450 | .map(serde_json::Value::String) |
| 1451 | .unwrap_or_else(|_| serde_json::Value::String(s[1..s.len() - 1].to_string())); |
| 1452 | } |
| 1453 | if s.len() >= 2 && s.starts_with('\'') && s.ends_with('\'') { |
| 1454 | return serde_json::Value::String(s[1..s.len() - 1].replace("''", "'")); |
| 1455 | } |
| 1456 | |
| 1457 | // Try as integer |
| 1458 | if let Ok(n) = s.parse::<i64>() { |
| 1459 | return serde_json::Value::Number(n.into()); |
| 1460 | } |
| 1461 | // Try as float |
| 1462 | if let Ok(n) = s.parse::<f64>() { |
| 1463 | if let Some(n) = serde_json::Number::from_f64(n) { |
| 1464 | return serde_json::Value::Number(n); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | // Try as boolean / null |
| 1469 | match s { |
| 1470 | "true" => return serde_json::Value::Bool(true), |
| 1471 | "false" => return serde_json::Value::Bool(false), |
| 1472 | "null" => return serde_json::Value::Null, |
| 1473 | _ => {} |
| 1474 | } |
| 1475 | |
| 1476 | // Materialized arrays/objects use JSON syntax, which is also valid YAML. |
| 1477 | // Parse it first so quoted commas and nested values remain lossless. |
| 1478 | if (s.starts_with('[') && s.ends_with(']')) || (s.starts_with('{') && s.ends_with('}')) { |
| 1479 | if let Ok(value) = serde_json::from_str::<serde_json::Value>(s) { |
| 1480 | return value; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | // Also accept simple hand-authored YAML arrays such as [a, b, c]. |
| 1485 | if s.starts_with('[') && s.ends_with(']') { |
| 1486 | let inner = s[1..s.len() - 1].trim(); |
| 1487 | if inner.is_empty() { |
| 1488 | return serde_json::Value::Array(Vec::new()); |
| 1489 | } |
| 1490 | let items: Vec<serde_json::Value> = inner |
| 1491 | .split(',') |
| 1492 | .map(|item| parse_yaml_value(item.trim())) |
| 1493 | .collect(); |
| 1494 | return serde_json::Value::Array(items); |
| 1495 | } |
| 1496 | |
| 1497 | // Default: string |
| 1498 | serde_json::Value::String(s.to_string()) |
| 1499 | } |
| 1500 | |
| 1501 | /// Infer the vault entry type from its path. |
| 1502 | fn infer_entry_type(path: &str) -> VaultEntryType { |
no test coverage detected