Skip to content

Commit

Permalink
Makes each iteration from Keystore::list fallible
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed May 6, 2024
1 parent d974c75 commit fca11b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use self::store::{DefaultStore, Keystore};
use crate::config::AppConfig;
use crate::home::Home;
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::iter::FusedIterator;
use std::sync::Arc;
Expand All @@ -25,6 +26,8 @@ impl KeyMgr {
let s = Arc::new(DefaultStore::new(home));

for k in s.list() {
let k = k.map_err(|e| KeyMgrError::ListKeyFailed(s.id(), e))?;

assert!(keys.insert(k.id().clone(), k).is_none());
}

Expand Down Expand Up @@ -76,4 +79,7 @@ impl Key {

/// Represents an error when [`KeyMgr`] fails to initialize.
#[derive(Debug, Error)]
pub enum KeyMgrError {}
pub enum KeyMgrError {
#[error("couldn't list keys from '{0}' store")]
ListKeyFailed(&'static str, #[source] Box<dyn Error>),
}
23 changes: 20 additions & 3 deletions src/key/store/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,24 @@ impl Keystore for DefaultStore {
"default"
}

fn list(self: &Arc<Self>) -> impl Iterator<Item = Key>
#[cfg(target_os = "linux")]
fn list(self: &Arc<Self>) -> impl Iterator<Item = Result<Key, Box<dyn Error>>>
where
Self: Sized,
{
KeyList {}
}

#[cfg(target_os = "macos")]
fn list(self: &Arc<Self>) -> impl Iterator<Item = Result<Key, Box<dyn Error>>>
where
Self: Sized,
{
KeyList {}
}

#[cfg(target_os = "windows")]
fn list(self: &Arc<Self>) -> impl Iterator<Item = Result<Key, Box<dyn Error>>>
where
Self: Sized,
{
Expand Down Expand Up @@ -134,15 +151,15 @@ impl Keystore for DefaultStore {

self.store(&id, key)?;

todo!()
Ok(Key { id })
}
}

/// Iterator to list all keys in the [`DefaultStore`].
struct KeyList {}

impl Iterator for KeyList {
type Item = Key;
type Item = Result<Key, Box<dyn Error>>;

fn next(&mut self) -> Option<Self::Item> {
todo!()
Expand Down
2 changes: 1 addition & 1 deletion src/key/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod default;
pub trait Keystore: Send + Sync {
fn id(&self) -> &'static str;

fn list(self: &Arc<Self>) -> impl Iterator<Item = Key>
fn list(self: &Arc<Self>) -> impl Iterator<Item = Result<Key, Box<dyn Error>>>
where
Self: Sized;

Expand Down

0 comments on commit fca11b6

Please sign in to comment.