Decode a MessagePack-encoded value as f64. If the value is a map (typed KV entry), extracts the first numeric field.
(bytes: &[u8])
| 440 | /// |
| 441 | /// If the value is a map (typed KV entry), extracts the first numeric field. |
| 442 | fn decode_msgpack_f64(bytes: &[u8]) -> Result<f64, AtomicError> { |
| 443 | if let Ok(v) = zerompk::from_msgpack::<f64>(bytes) { |
| 444 | return Ok(v); |
| 445 | } |
| 446 | // Accept integer values promoted to float. |
| 447 | if let Ok(v) = zerompk::from_msgpack::<i64>(bytes) { |
| 448 | return Ok(v as f64); |
| 449 | } |
| 450 | if let Ok(v) = zerompk::from_msgpack::<u64>(bytes) { |
| 451 | return Ok(v as f64); |
| 452 | } |
| 453 | // If value is a map, find the first numeric field. |
| 454 | if let Ok(nodedb_types::Value::Object(map)) = nodedb_types::value_from_msgpack(bytes) { |
| 455 | for (k, v) in &map { |
| 456 | if k == "key" { |
| 457 | continue; |
| 458 | } |
| 459 | match v { |
| 460 | nodedb_types::Value::Float(f) => return Ok(*f), |
| 461 | nodedb_types::Value::Integer(i) => return Ok(*i as f64), |
| 462 | _ => {} |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | Err(AtomicError::TypeMismatch { |
| 467 | detail: "value is not numeric".into(), |
| 468 | }) |
| 469 | } |
| 470 | |
| 471 | #[cfg(test)] |
| 472 | mod tests { |
no test coverage detected