diff --git a/CHANGELOG.md b/CHANGELOG.md index cdf4940..0c87e63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Missing `FLAGS` column in messages table [#40] +- Subtract with overflow if next page empty [#38] ## [0.2.0] - 2021-03-10 @@ -80,5 +81,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#25]: https://github.com/soywod/himalaya/issues/25 [#29]: https://github.com/soywod/himalaya/issues/29 [#32]: https://github.com/soywod/himalaya/issues/32 +[#38]: https://github.com/soywod/himalaya/issues/38 [#39]: https://github.com/soywod/himalaya/issues/39 [#40]: https://github.com/soywod/himalaya/issues/40 diff --git a/src/imap.rs b/src/imap.rs index f6ae210..3d9b991 100644 --- a/src/imap.rs +++ b/src/imap.rs @@ -101,10 +101,16 @@ impl<'a> ImapConnector<'a> { .sess .select(mbox) .chain_err(|| format!("Cannot select mailbox `{}`", mbox))? - .exists; + .exists as i64; - let begin = last_seq - page * page_size; - let end = begin - (begin - 1).min(page_size - 1); + if last_seq == 0 { + return Err(format!("Cannot select empty mailbox `{}`", mbox).into()); + } + + // TODO: add tests, improve error management when empty page + let cursor = (page * page_size) as i64; + let begin = 1.max(last_seq - cursor); + let end = begin - begin.min(*page_size as i64) + 1; let range = format!("{}:{}", begin, end); let msgs = self