Decode a MessagePack-encoded value as i64. If the value is a map (typed KV entry), extracts the first numeric field.
(bytes: &[u8])
| 403 | /// |
| 404 | /// If the value is a map (typed KV entry), extracts the first numeric field. |
| 405 | fn decode_msgpack_i64(bytes: &[u8]) -> Result<i64, AtomicError> { |
| 406 | // Try i64 first, then u64 (MessagePack encodes small positive as u64). |
| 407 | if let Ok(v) = zerompk::from_msgpack::<i64>(bytes) { |
| 408 | return Ok(v); |
| 409 | } |
| 410 | if let Ok(v) = zerompk::from_msgpack::<u64>(bytes) { |
| 411 | return i64::try_from(v).map_err(|_| AtomicError::Overflow); |
| 412 | } |
| 413 | // Try f64 → i64 truncation for values stored as float. |
| 414 | if let Ok(v) = zerompk::from_msgpack::<f64>(bytes) |
| 415 | && v.fract() == 0.0 |
| 416 | && v >= i64::MIN as f64 |
| 417 | && v <= i64::MAX as f64 |
| 418 | { |
| 419 | return Ok(v as i64); |
| 420 | } |
| 421 | // If value is a map (typed KV entry), find the first numeric field. |
| 422 | if let Ok(nodedb_types::Value::Object(map)) = nodedb_types::value_from_msgpack(bytes) { |
| 423 | for (k, v) in &map { |
| 424 | if k == "key" { |
| 425 | continue; |
| 426 | } |
| 427 | match v { |
| 428 | nodedb_types::Value::Integer(i) => return Ok(*i), |
| 429 | nodedb_types::Value::Float(f) if f.fract() == 0.0 => return Ok(*f as i64), |
| 430 | _ => {} |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | Err(AtomicError::TypeMismatch { |
| 435 | detail: "value is not an integer".into(), |
| 436 | }) |
| 437 | } |
| 438 | |
| 439 | /// Decode a MessagePack-encoded value as f64. |
| 440 | /// |
no test coverage detected