pub fn s(v: &Value, key: &str) -> StringExpand description
What: Extract a string value from a JSON object by key, defaulting to empty string.
Inputs:
v: JSON value to extract from.key: Key to look up in the JSON object.
Output:
- Returns the string value if found, or an empty string if the key is missing or not a string.
Details:
- Returns
""if the key is missing or the value is not a string type.
ยงExamples
use pacsea::util::s;
use serde_json::json;
// Simulating a real AUR RPC API response for a package like 'yay'
let aur_pkg_info = json!({
"Name": "yay",
"Version": "12.3.4-1",
"Description": "Yet another Yogurt - An AUR Helper written in Go"
});
assert_eq!(s(&aur_pkg_info, "Name"), "yay");
assert_eq!(s(&aur_pkg_info, "Description"), "Yet another Yogurt - An AUR Helper written in Go");
assert_eq!(s(&aur_pkg_info, "Maintainer"), ""); // Returns empty string for missing keys
// Simulating a package search result from the official repository API
let repo_pkg_info = json!({
"pkgname": "firefox",
"pkgver": "128.0-1",
"repo": "extra"
});
assert_eq!(s(&repo_pkg_info, "pkgname"), "firefox");
assert_eq!(s(&repo_pkg_info, "repo"), "extra");