Skip to content

Commit

Permalink
Add subfolder api to open and create subfolders.
Browse files Browse the repository at this point in the history
Cleanup tests for subfolder api.
  • Loading branch information
poelzi committed Oct 13, 2023
1 parent 707b052 commit c91781d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
25 changes: 21 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ pub enum MaildirError {
Io(std::io::Error),
Utf8(std::str::Utf8Error),
Time(std::time::SystemTimeError),
InvalidFolderName(std::string::String),
}

impl fmt::Display for MaildirError {
Expand All @@ -304,6 +305,7 @@ impl fmt::Display for MaildirError {
Io(ref e) => write!(f, "IO Error: {}", e),
Utf8(ref e) => write!(f, "UTF8 Encoding Error: {}", e),
Time(ref e) => write!(f, "Time Error: {}", e),
InvalidFolderName(ref e) => write!(f, "Invalid Folder Name: {}", e),
}
}
}
Expand All @@ -316,6 +318,7 @@ impl error::Error for MaildirError {
Io(ref e) => Some(e),
Utf8(ref e) => Some(e),
Time(ref e) => Some(e),
InvalidFolderName(ref _e) => None,
}
}
}
Expand Down Expand Up @@ -412,6 +415,18 @@ impl Maildir {
&self.path
}

/// Creates a Maildir from the subfolder
pub fn subfolder(&self, subfolder: &str) -> Result<Maildir, MaildirError> {
if !subfolder.starts_with('.') {
return Err(MaildirError::InvalidFolderName(format!(
"Subfolder must start with a single period: {}",
subfolder
)));
}
let new_path = self.path.join(subfolder);
Ok(Maildir { path: new_path })
}

/// Returns the number of messages found inside the `new`
/// maildir folder.
pub fn count_new(&self) -> usize {
Expand Down Expand Up @@ -622,13 +637,15 @@ impl Maildir {

/// Creates all neccessary directories for a `subfolder` if they don't exist yet. It is the library user's
/// responsibility to call this before using `store` with a subfolder.
pub fn create_subfolder_dirs(&self, subfolder: &str) -> std::io::Result<()> {
if ! subfolder.starts_with('.') {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Subfolder must start with ."));
pub fn create_subfolder_dirs(&self, subfolder: &str) -> Result<(), MaildirError> {
if !subfolder.starts_with('.') {
return Err(MaildirError::InvalidFolderName(format!(
"Subfolder must start with a single period: {}",
subfolder
)));
}
let subpath = PathBuf::from(subfolder);
let mut path = self.path.clone().join(&subpath);
fs::create_dir_all(path.as_path())?;
for d in MAILDIR_FOLDER_LIST {
path.push(d);
fs::create_dir_all(path.as_path())?;
Expand Down
26 changes: 22 additions & 4 deletions tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ fn maildir_list_subdirs() {
assert!(subdirs.contains(&".Subdir2".into()));
assert!(!subdirs.contains(&"..Subdir3".into()));
});
}

with_maildir(SUBMAILDIRS_NAME, |maildir| {
#[test]
fn maildir_subdir() {
with_maildir(MAILDIR_NAME, |maildir| {
let _e = maildir.create_subfolder_dirs(".created");
let _e = maildir.create_subfolder_dirs(".folder.with.subs");
let e2 = maildir.create_subfolder_dirs("invalid");
Expand All @@ -151,12 +154,27 @@ fn maildir_list_subdirs() {
})
.collect();

assert_eq!(4, subdirs.len());
assert!(subdirs.contains(&".Subdir1".into()));
assert!(subdirs.contains(&".Subdir2".into()));
assert_eq!(2, subdirs.len());
assert!(subdirs.contains(&".created".into()));
assert!(subdirs.contains(&".folder.with.subs".into()));
assert!(!subdirs.contains(&"..Subdir3".into()));

// test subfolder api
let sf = maildir.subfolder(".folder.with.subs").unwrap();
let store_res = sf.store_new(&TEST_MAIL_BODY).unwrap();
sf.move_new_to_cur(&store_res).unwrap();
assert!(sf.find(&store_res).is_some());

// test subfolder api with newly created folder
let sf = maildir.subfolder(".notyes").unwrap();
sf.store_new(&TEST_MAIL_BODY)
.expect_err("should not store new message when folder is missing");
sf.create_dirs().expect("should create folders");
let store_res = sf
.store_new(&TEST_MAIL_BODY)
.expect("message should be stored");
sf.move_new_to_cur(&store_res).unwrap();
assert!(sf.find(&store_res).is_some());
});
}

Expand Down

0 comments on commit c91781d

Please sign in to comment.