MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / decode_msgpack_i64

Function decode_msgpack_i64

nodedb/src/engine/kv/engine_atomic.rs:405–437  ·  view source on GitHub ↗

Decode a MessagePack-encoded value as i64. If the value is a map (typed KV entry), extracts the first numeric field.

(bytes: &[u8])

Source from the content-addressed store, hash-verified

403///
404/// If the value is a map (typed KV entry), extracts the first numeric field.
405fn 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///

Callers 1

incrMethod · 0.85

Calls 1

value_from_msgpackFunction · 0.85

Tested by

no test coverage detected