Parse a YAML value string into a serde_json::Value.
(s: &str)
| 1207 | |
| 1208 | /// Parse a YAML value string into a serde_json::Value. |
| 1209 | fn parse_yaml_value(s: &str) -> serde_json::Value { |
| 1210 | // Quoted values remain strings even when their contents look like another |
| 1211 | // scalar type. Decode JSON-style double-quoted escapes losslessly. |
| 1212 | if s.len() >= 2 && s.starts_with('"') && s.ends_with('"') { |
| 1213 | return serde_json::from_str::<String>(s) |
| 1214 | .map(serde_json::Value::String) |
| 1215 | .unwrap_or_else(|_| serde_json::Value::String(s[1..s.len() - 1].to_string())); |
| 1216 | } |
| 1217 | if s.len() >= 2 && s.starts_with('\'') && s.ends_with('\'') { |
| 1218 | return serde_json::Value::String(s[1..s.len() - 1].replace("''", "'")); |
| 1219 | } |
| 1220 | |
| 1221 | // Try as integer |
| 1222 | if let Ok(n) = s.parse::<i64>() { |
| 1223 | return serde_json::Value::Number(n.into()); |
| 1224 | } |
| 1225 | // Try as float |
| 1226 | if let Ok(n) = s.parse::<f64>() { |
| 1227 | if let Some(n) = serde_json::Number::from_f64(n) { |
| 1228 | return serde_json::Value::Number(n); |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | // Try as boolean / null |
| 1233 | match s { |
| 1234 | "true" => return serde_json::Value::Bool(true), |
| 1235 | "false" => return serde_json::Value::Bool(false), |
| 1236 | "null" => return serde_json::Value::Null, |
| 1237 | _ => {} |
| 1238 | } |
| 1239 | |
| 1240 | // Materialized arrays/objects use JSON syntax, which is also valid YAML. |
| 1241 | // Parse it first so quoted commas and nested values remain lossless. |
| 1242 | if (s.starts_with('[') && s.ends_with(']')) || (s.starts_with('{') && s.ends_with('}')) { |
| 1243 | if let Ok(value) = serde_json::from_str::<serde_json::Value>(s) { |
| 1244 | return value; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | // Also accept simple hand-authored YAML arrays such as [a, b, c]. |
| 1249 | if s.starts_with('[') && s.ends_with(']') { |
| 1250 | let inner = s[1..s.len() - 1].trim(); |
| 1251 | if inner.is_empty() { |
| 1252 | return serde_json::Value::Array(Vec::new()); |
| 1253 | } |
| 1254 | let items: Vec<serde_json::Value> = inner |
| 1255 | .split(',') |
| 1256 | .map(|item| parse_yaml_value(item.trim())) |
| 1257 | .collect(); |
| 1258 | return serde_json::Value::Array(items); |
| 1259 | } |
| 1260 | |
| 1261 | // Default: string |
| 1262 | serde_json::Value::String(s.to_string()) |
| 1263 | } |
| 1264 | |
| 1265 | /// Infer the vault entry type from its path. |
| 1266 | fn infer_entry_type(path: &str) -> VaultEntryType { |
no test coverage detected