fix: handle missing file [#715]

This commit is contained in:
jglad
2025-02-08 10:52:37 +01:00
parent 9dd33d5c5b
commit 8588600a2c
3 changed files with 27 additions and 8 deletions

View File

@@ -47,11 +47,14 @@ impl FileKeyStorage {
} }
fn get_selected_pubkey(&self) -> Result<Option<Pubkey>> { fn get_selected_pubkey(&self) -> Result<Option<Pubkey>> {
let pubkey_str = self match self
.selected_key_directory .selected_key_directory
.get_file(SELECTED_PUBKEY_FILE_NAME.to_owned())?; .get_file(SELECTED_PUBKEY_FILE_NAME.to_owned())
{
Ok(serde_json::from_str(&pubkey_str)?) 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<Pubkey>) -> Result<()> { fn select_pubkey(&self, pubkey: Option<Pubkey>) -> Result<()> {
@@ -164,4 +167,15 @@ mod tests {
assert!(resp.is_ok()); 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),
}
}
} }

View File

@@ -105,9 +105,9 @@ impl Directory {
.ok_or_else(|| Error::Generic("Could not turn path to string".to_owned()))?; .ok_or_else(|| Error::Generic("Could not turn path to string".to_owned()))?;
Ok(fs::read_to_string(filepath_str)?) Ok(fs::read_to_string(filepath_str)?)
} else { } else {
Err(Error::Generic(format!( Err(Error::Io(io::Error::new(
"Requested file was not found: {}", io::ErrorKind::NotFound,
file_name format!("Requested file was not found: {}", file_name),
))) )))
} }
} }

View File

@@ -35,9 +35,14 @@ impl Default for DecksCache {
} }
impl DecksCache { impl DecksCache {
pub fn new(account_to_decks: HashMap<Pubkey, Decks>) -> Self { pub fn new(mut account_to_decks: HashMap<Pubkey, Decks>) -> Self {
let fallback_pubkey = FALLBACK_PUBKEY(); 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 { Self {
account_to_decks, account_to_decks,
fallback_pubkey, fallback_pubkey,