pub fn u64_of(v: &Value, keys: &[&str]) -> Option<u64>Expand description
What: Extract an unsigned 64-bit integer by trying multiple keys and representations.
Inputs:
v: JSON value to extract from.keys: Array of candidate keys to try in order.
Output:
- Returns
Some(u64)if a valid value is found, orNoneif no usable value is found.
Details:
- Accepts any of the following representations for the first matching key:
- JSON
u64 - JSON
i64convertible tou64 - String that parses as
u64
- JSON
- Tries keys in the order provided and returns the first match.
- Returns
Noneif no key maps to a convertible value.
ยงExamples
use pacsea::util::u64_of;
use serde_json::json;
// Extracting the vote count from an AUR package info (can be a number or a string)
let aur_vote_data = json!({
"NumVotes": 123,
"Popularity": "45.67"
});
assert_eq!(u64_of(&aur_vote_data, &["NumVotes", "Votes"]), Some(123));
// Extracting the first seen timestamp (often a string in JSON APIs)
let timestamp_data = json!({
"FirstSubmitted": "1672531200",
"LastModified": 1672617600
});
assert_eq!(u64_of(×tamp_data, &["FirstSubmitted", "Submitted"]), Some(1672531200));
assert_eq!(u64_of(×tamp_data, &["LastModified", "Modified"]), Some(1672617600));
// Returns None for negative numbers or if no convertible value is found
let negative_data = json!({"OutOfDate": -1});
assert_eq!(u64_of(&negative_data, &["OutOfDate"]), None);