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

Function extract_weight_from_properties

nodedb-graph/src/csr/weights.rs:186–211  ·  view source on GitHub ↗

Extract the `"weight"` property from MessagePack-encoded edge properties. Returns 1.0 if properties are empty, malformed, or don't contain a `"weight"` key. Handles F64, F32, and integer weight values; other numeric types default to 1.0.

(properties: &[u8])

Source from the content-addressed store, hash-verified

184/// `"weight"` key. Handles F64, F32, and integer weight values; other
185/// numeric types default to 1.0.
186pub fn extract_weight_from_properties(properties: &[u8]) -> f64 {
187 if properties.is_empty() {
188 return 1.0;
189 }
190 let Ok(val) = rmpv::decode::read_value(&mut &properties[..]) else {
191 return 1.0;
192 };
193 match val {
194 rmpv::Value::Map(entries) => {
195 for (k, v) in entries {
196 if let rmpv::Value::String(ref s) = k
197 && s.as_str() == Some("weight")
198 {
199 return match v {
200 rmpv::Value::F64(f) => f,
201 rmpv::Value::F32(f) => f as f64,
202 rmpv::Value::Integer(i) => i.as_f64().unwrap_or(1.0),
203 _ => 1.0,
204 };
205 }
206 }
207 1.0
208 }
209 _ => 1.0,
210 }
211}
212
213#[cfg(test)]
214mod tests {

Callers 5

execute_edge_putMethod · 0.85
build_csr_for_collectionFunction · 0.85
apply_undo_edgeMethod · 0.85
from_edge_store_as_ofMethod · 0.85

Calls 4

read_valueFunction · 0.50
is_emptyMethod · 0.45
as_strMethod · 0.45
as_f64Method · 0.45

Tested by

no test coverage detected