simplify manifest and account loading

This commit is contained in:
Carson McManus 2021-08-01 09:47:50 -04:00
parent 258e758395
commit d6cdbd5541

View file

@ -1,6 +1,5 @@
use log::*; use log::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use std::path::Path; use std::path::Path;
@ -33,49 +32,24 @@ pub struct ManifestEntry {
} }
impl Manifest { impl Manifest {
pub fn load(path: &Path) -> Result<Manifest, Box<dyn Error>> { pub fn load(path: &Path) -> anyhow::Result<Manifest> {
debug!("loading manifest: {:?}", &path); debug!("loading manifest: {:?}", &path);
match File::open(path) { let file = File::open(path)?;
Ok(file) => { let reader = BufReader::new(file);
let reader = BufReader::new(file); let mut manifest: Manifest = serde_json::from_reader(reader)?;
match serde_json::from_reader(reader) { manifest.folder = String::from(path.parent().unwrap().to_str().unwrap());
Ok(m) => { return Ok(manifest);
let mut manifest: Manifest = m;
manifest.folder = String::from(path.parent().unwrap().to_str().unwrap());
return Ok(manifest);
}
Err(e) => {
return Err(Box::new(e));
}
}
}
Err(e) => {
return Err(Box::new(e));
}
}
} }
pub fn load_accounts(&mut self) { pub fn load_accounts(&mut self) -> anyhow::Result<()> {
for entry in &self.entries { for entry in &self.entries {
let path = Path::new(&self.folder).join(&entry.filename); let path = Path::new(&self.folder).join(&entry.filename);
debug!("loading account: {:?}", path); debug!("loading account: {:?}", path);
match File::open(path) { let file = File::open(path)?;
Ok(f) => { let reader = BufReader::new(file);
let reader = BufReader::new(f); let account: SteamGuardAccount = serde_json::from_reader(reader)?;
match serde_json::from_reader(reader) { self.accounts.push(account);
Ok(a) => {
let account: SteamGuardAccount = a;
self.accounts.push(account);
}
Err(e) => {
error!("invalid json: {}", e)
}
}
}
Err(e) => {
error!("unable to open account: {}", e)
}
}
} }
Ok(())
} }
} }