add error handling per backend
This commit is contained in:
+16
-10
@@ -2,7 +2,7 @@ use std::collections::HashSet;
|
||||
use std::fs::read_to_string;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::{Backend, Switches, Text};
|
||||
@@ -20,28 +20,34 @@ const SWITCHES_REMOVE: Switches = &["uninstall"];
|
||||
impl Backend for Rust {
|
||||
impl_backend_constants!();
|
||||
|
||||
fn get_all_installed_packages(&self) -> HashSet<Package> {
|
||||
fn get_all_installed_packages(&self) -> Result<HashSet<Package>> {
|
||||
let file = get_crates_file().unwrap();
|
||||
let content = read_to_string(file).unwrap();
|
||||
let json: Value = serde_json::from_str(&content).unwrap();
|
||||
extract_packages(json)
|
||||
}
|
||||
|
||||
fn get_explicitly_installed_packages(&self) -> HashSet<Package> {
|
||||
fn get_explicitly_installed_packages(&self) -> Result<HashSet<Package>> {
|
||||
self.get_all_installed_packages()
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_packages(json: Value) -> HashSet<Package> {
|
||||
json.get("installs")
|
||||
.unwrap()
|
||||
fn extract_packages(json: Value) -> Result<HashSet<Package>> {
|
||||
let result: HashSet<_> = json
|
||||
.get("installs")
|
||||
.context("get 'installs' field from json")?
|
||||
.as_object()
|
||||
.unwrap()
|
||||
.context("getting object")?
|
||||
.into_iter()
|
||||
.map(|(name, _)| name)
|
||||
.map(|name| name.split_whitespace().next().unwrap())
|
||||
.filter_map(Package::try_from)
|
||||
.collect()
|
||||
.map(|name| {
|
||||
name.split_whitespace()
|
||||
.next()
|
||||
.expect("identifier is whitespace-delimited")
|
||||
})
|
||||
.map(|name| Package::try_from(name).expect("name is valid"))
|
||||
.collect();
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
impl Rust {
|
||||
|
||||
Reference in New Issue
Block a user