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