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])
| 184 | /// `"weight"` key. Handles F64, F32, and integer weight values; other |
| 185 | /// numeric types default to 1.0. |
| 186 | pub 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)] |
| 214 | mod tests { |
no test coverage detected