From 8588600a2cb53e292d50e753a3614e4e966d19a3 Mon Sep 17 00:00:00 2001 From: jglad Date: Sat, 8 Feb 2025 10:52:37 +0100 Subject: [PATCH 1/2] fix: handle missing file [#715] --- .../notedeck/src/storage/file_key_storage.rs | 22 +++++++++++++++---- crates/notedeck/src/storage/file_storage.rs | 6 ++--- crates/notedeck_columns/src/decks.rs | 7 +++++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/crates/notedeck/src/storage/file_key_storage.rs b/crates/notedeck/src/storage/file_key_storage.rs index 9e5a9e5b..aacd32c4 100644 --- a/crates/notedeck/src/storage/file_key_storage.rs +++ b/crates/notedeck/src/storage/file_key_storage.rs @@ -47,11 +47,14 @@ impl FileKeyStorage { } fn get_selected_pubkey(&self) -> Result> { - let pubkey_str = self + match self .selected_key_directory - .get_file(SELECTED_PUBKEY_FILE_NAME.to_owned())?; - - Ok(serde_json::from_str(&pubkey_str)?) + .get_file(SELECTED_PUBKEY_FILE_NAME.to_owned()) + { + Ok(pubkey_str) => Ok(Some(serde_json::from_str(&pubkey_str)?)), + Err(crate::Error::Io(_)) => Ok(None), + Err(e) => Err(e), + } } fn select_pubkey(&self, pubkey: Option) -> Result<()> { @@ -164,4 +167,15 @@ mod tests { assert!(resp.is_ok()); } + + #[test] + fn test_get_selected_key_when_no_file() { + let storage = FileKeyStorage::mock().unwrap(); + + // Should return Ok(None) when no key has been selected + match storage.get_selected_key() { + KeyStorageResponse::ReceivedResult(Ok(None)) => (), // This is what we expect + other => panic!("Expected Ok(None), got {:?}", other), + } + } } diff --git a/crates/notedeck/src/storage/file_storage.rs b/crates/notedeck/src/storage/file_storage.rs index fed1ac67..37bdc2dc 100644 --- a/crates/notedeck/src/storage/file_storage.rs +++ b/crates/notedeck/src/storage/file_storage.rs @@ -105,9 +105,9 @@ impl Directory { .ok_or_else(|| Error::Generic("Could not turn path to string".to_owned()))?; Ok(fs::read_to_string(filepath_str)?) } else { - Err(Error::Generic(format!( - "Requested file was not found: {}", - file_name + Err(Error::Io(io::Error::new( + io::ErrorKind::NotFound, + format!("Requested file was not found: {}", file_name), ))) } } diff --git a/crates/notedeck_columns/src/decks.rs b/crates/notedeck_columns/src/decks.rs index 1d832bd9..adeabbec 100644 --- a/crates/notedeck_columns/src/decks.rs +++ b/crates/notedeck_columns/src/decks.rs @@ -35,9 +35,14 @@ impl Default for DecksCache { } impl DecksCache { - pub fn new(account_to_decks: HashMap) -> Self { + pub fn new(mut account_to_decks: HashMap) -> Self { let fallback_pubkey = FALLBACK_PUBKEY(); + // Ensure fallback deck exists + if !account_to_decks.contains_key(&fallback_pubkey) { + account_to_decks.insert(fallback_pubkey, Decks::default()); + } + Self { account_to_decks, fallback_pubkey, From 4aefe1f1fe4828e3c0c4af6e73eacd54600d13af Mon Sep 17 00:00:00 2001 From: jglad Date: Sat, 8 Feb 2025 10:59:15 +0100 Subject: [PATCH 2/2] refactor --- crates/notedeck_columns/src/decks.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/notedeck_columns/src/decks.rs b/crates/notedeck_columns/src/decks.rs index adeabbec..7295600a 100644 --- a/crates/notedeck_columns/src/decks.rs +++ b/crates/notedeck_columns/src/decks.rs @@ -37,11 +37,7 @@ impl Default for DecksCache { impl DecksCache { pub fn new(mut account_to_decks: HashMap) -> Self { let fallback_pubkey = FALLBACK_PUBKEY(); - - // Ensure fallback deck exists - if !account_to_decks.contains_key(&fallback_pubkey) { - account_to_decks.insert(fallback_pubkey, Decks::default()); - } + account_to_decks.entry(fallback_pubkey).or_default(); Self { account_to_decks,