use std::io::{self, BufReader};
use super::{ReadError, StdinGuard, UiCallbacks};
use crate::{identity::IdentityFile, Identity};
pub fn read_identities(
filenames: Vec<String>,
_max_work_factor: Option<u8>,
stdin_guard: &mut StdinGuard,
) -> Result<Vec<Box<dyn Identity>>, ReadError> {
let mut identities: Vec<Box<dyn Identity>> = Vec::with_capacity(filenames.len());
for filename in filenames {
let reader = stdin_guard.open(filename.clone()).map_err(|e| match e {
ReadError::Io(e) if matches!(e.kind(), io::ErrorKind::NotFound) => {
ReadError::IdentityNotFound(filename.clone())
}
other => other,
})?;
let identity_file = IdentityFile::from_buffer(BufReader::new(reader))
.map_err(ReadError::from)?
.with_callbacks(UiCallbacks);
let new_identities = identity_file
.into_identities()
.map_err(ReadError::EncryptedIdentities)?;
identities.extend(new_identities.into_iter().map(|i| i as Box<dyn Identity>));
}
Ok(identities)
}