pub fn ss(v: &Value, keys: &[&str]) -> Option<String>Expand description
What: Extract the first available string from a list of candidate keys.
Inputs:
v: JSON value to extract from.keys: Array of candidate keys to try in order.
Output:
- Returns
Some(String)for the first key that maps to a JSON string, orNoneif none match.
Details:
- Tries keys in the order provided and returns the first match.
- Returns
Noneif no key maps to a string value.
ยงExamples
use pacsea::util::ss;
use serde_json::json;
// Trying multiple possible version keys from different AUR API responses
let pkg_info = json!({
"Version": "1.2.3",
"pkgver": "1.2.3",
"ver": "1.2.3"
});
// Returns the first matching key: "pkgver"
assert_eq!(ss(&pkg_info, &["pkgver", "Version", "ver"]), Some("1.2.3".to_string()));
// Trying to get a maintainer, falling back to a packager field
let maintainer_info = json!({
"Packager": "Arch Linux Pacsea Team <pacsea@example.org>"
// "Maintainer" key is missing to demonstrate fallback
});
assert_eq!(ss(&maintainer_info, &["Maintainer", "Packager"]), Some("Arch Linux Pacsea Team <pacsea@example.org>".to_string()));
// Returns None if no key matches
assert_eq!(ss(&pkg_info, &["License", "URL"]), None);