Parse a YAML value string into a serde_json::Value.
(s: &str)
| 1521 | |
| 1522 | /// Parse a YAML value string into a serde_json::Value. |
| 1523 | fn parse_yaml_value(s: &str) -> serde_json::Value { |
| 1524 | // Quoted values remain strings even when their contents look like another |
| 1525 | // scalar type. Decode JSON-style double-quoted escapes losslessly. |
| 1526 | if s.len() >= 2 && s.starts_with('"') && s.ends_with('"') { |
| 1527 | return serde_json::from_str::<String>(s) |
| 1528 | .map(serde_json::Value::String) |
| 1529 | .unwrap_or_else(|_| serde_json::Value::String(s[1..s.len() - 1].to_string())); |
| 1530 | } |
| 1531 | if s.len() >= 2 && s.starts_with('\'') && s.ends_with('\'') { |
| 1532 | return serde_json::Value::String(s[1..s.len() - 1].replace("''", "'")); |
| 1533 | } |
| 1534 | |
| 1535 | // Try as integer |
| 1536 | if let Ok(n) = s.parse::<i64>() { |
| 1537 | return serde_json::Value::Number(n.into()); |
| 1538 | } |
| 1539 | // Try as float |
| 1540 | if let Ok(n) = s.parse::<f64>() { |
| 1541 | if let Some(n) = serde_json::Number::from_f64(n) { |
| 1542 | return serde_json::Value::Number(n); |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | // Try as boolean / null |
| 1547 | match s { |
| 1548 | "true" => return serde_json::Value::Bool(true), |
| 1549 | "false" => return serde_json::Value::Bool(false), |
| 1550 | "null" => return serde_json::Value::Null, |
| 1551 | _ => {} |
| 1552 | } |
| 1553 | |
| 1554 | // Materialized arrays/objects use JSON syntax, which is also valid YAML. |
| 1555 | // Parse it first so quoted commas and nested values remain lossless. |
| 1556 | if (s.starts_with('[') && s.ends_with(']')) || (s.starts_with('{') && s.ends_with('}')) { |
| 1557 | if let Ok(value) = serde_json::from_str::<serde_json::Value>(s) { |
| 1558 | return value; |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | // Also accept simple hand-authored YAML arrays such as [a, b, c]. |
| 1563 | if s.starts_with('[') && s.ends_with(']') { |
| 1564 | let inner = s[1..s.len() - 1].trim(); |
| 1565 | if inner.is_empty() { |
| 1566 | return serde_json::Value::Array(Vec::new()); |
| 1567 | } |
| 1568 | let items: Vec<serde_json::Value> = inner |
| 1569 | .split(',') |
| 1570 | .map(|item| parse_yaml_value(item.trim())) |
| 1571 | .collect(); |
| 1572 | return serde_json::Value::Array(items); |
| 1573 | } |
| 1574 | |
| 1575 | // Default: string |
| 1576 | serde_json::Value::String(s.to_string()) |
| 1577 | } |
| 1578 | |
| 1579 | /// Infer the vault entry type from its path. |
| 1580 | fn infer_entry_type(path: &str) -> VaultEntryType { |
no test coverage detected