arrs

Function arrs 

Source
pub fn arrs(v: &Value, keys: &[&str]) -> Vec<String>
Expand description

What: Extract an array of strings from a JSON object by trying keys in order.

Inputs:

  • v: JSON value to extract from.
  • keys: Array of candidate keys to try in order.

Output:

  • Returns the first found array as Vec<String>, filtering out non-string elements.
  • Returns an empty vector if no array of strings is found.

Details:

  • Tries keys in the order provided and returns the first array found.
  • Filters out non-string elements from the array.
  • Returns an empty vector if no key maps to an array or if all elements are non-string.

ยงExamples

use pacsea::util::arrs;
use serde_json::json;

// Getting the list of dependencies from a package's metadata
let pkg_metadata = json!({
    "Depends": ["glibc", "gcc-libs", "bash"],
    "MakeDepends": ["git", "pkgconf"]
});
// Tries "Depends" first, returns those dependencies
assert_eq!(arrs(&pkg_metadata, &["Depends", "MakeDepends"]), vec!["glibc", "gcc-libs", "bash"]);

// Getting the list of provides or alternate package names
let provides_info = json!({
    "Provides": ["python-cryptography", "python-crypto"],
    "Conflicts": ["python-crypto-legacy"]
});
assert_eq!(arrs(&provides_info, &["Provides", "Replaces"]), vec!["python-cryptography", "python-crypto"]);

// Returns empty vector if no array of strings is found
let simple_json = json!({"Name": "firefox"});
assert_eq!(arrs(&simple_json, &["Depends", "OptDepends"]), Vec::<String>::new());