Skip to content

Commit

Permalink
Need fallback behaviour when dirs::download_dir returns None (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa committed Jul 8, 2023
1 parent 3da9835 commit 6e8e12b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ pub enum IambError {
#[error("Serialization/deserialization error: {0}")]
Serde(#[from] serde_json::Error),

#[error("No download directory configured")]
NoDownloadDir,

#[error("Selected message does not have any attachments")]
NoAttachment,

Expand Down
7 changes: 2 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl Tunables {
pub struct DirectoryValues {
pub cache: PathBuf,
pub logs: PathBuf,
pub downloads: PathBuf,
pub downloads: Option<PathBuf>,
}

#[derive(Clone, Default, Deserialize)]
Expand Down Expand Up @@ -354,10 +354,7 @@ impl Directories {
dir
});

let downloads = self
.downloads
.or_else(dirs::download_dir)
.expect("no dirs.downloads value configured!");
let downloads = self.downloads.or_else(dirs::download_dir);

DirectoryValues { cache, logs, downloads }
}
Expand Down
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn mock_dirs() -> DirectoryValues {
DirectoryValues {
cache: PathBuf::new(),
logs: PathBuf::new(),
downloads: PathBuf::new(),
downloads: None,
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/windows/room/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ impl ChatState {
if let MessageEvent::Original(ev) = &msg.event {
let media = client.media();

let mut filename = match filename {
Some(f) => PathBuf::from(f),
None => settings.dirs.downloads.clone(),
let mut filename = match (filename, &settings.dirs.downloads) {
(Some(f), _) => PathBuf::from(f),
(None, Some(downloads)) => downloads.clone(),
(None, None) => return Err(IambError::NoDownloadDir.into()),
};

let (source, msg_filename) = match &ev.content.msgtype {
Expand Down

0 comments on commit 6e8e12b

Please sign in to comment.