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

Function decode_msgpack_f64

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

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

(bytes: &[u8])

Source from the content-addressed store, hash-verified

440///
441/// If the value is a map (typed KV entry), extracts the first numeric field.
442fn 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)]
472mod tests {

Callers 1

incr_floatMethod · 0.85

Calls 1

value_from_msgpackFunction · 0.85

Tested by

no test coverage detected