melib/backends: add prelude module for import cleanup

Add a prelude module under melib::backends that exports all types needed
to interact with a mail backend. This reduces the number of imports in
every related file.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
pull/425/head
Manos Pitsidianakis 4 months ago
parent c99633e141
commit e107d613a0
No known key found for this signature in database
GPG Key ID: 7729C7707F7E09D0

@ -38,13 +38,12 @@ use std::{
use futures::{future::FutureExt, stream::StreamExt};
use indexmap::IndexMap;
use melib::{
backends::*,
email::*,
backends::{prelude::*, Backends},
error::{Error, ErrorKind, Result},
log,
text::GlobMatch,
thread::Threads,
AddressBook, Collection, LogLevel, SortField, SortOrder,
AddressBook, SortField, SortOrder,
};
use smallvec::SmallVec;

@ -20,25 +20,42 @@
*/
pub mod utf7;
use std::{
any::Any,
borrow::Cow,
collections::{BTreeSet, HashMap},
future::Future,
ops::Deref,
pin::Pin,
sync::Arc,
};
use futures::stream::Stream;
use smallvec::SmallVec;
use super::email::{Envelope, EnvelopeHash, Flag};
use crate::{
conf::AccountSettings,
error::{Error, ErrorKind, Result},
HeaderName, LogLevel,
};
#[cfg(test)]
mod tests;
pub mod prelude {
pub use futures::{
future::BoxFuture,
stream::{BoxStream, Stream},
};
pub type ResultFuture<T> = crate::Result<BoxFuture<'static, crate::Result<T>>>;
pub use std::{
any::Any,
borrow::Cow,
cell::RefCell,
collections::{BTreeSet, HashMap},
ops::Deref,
sync::Arc,
};
pub use smallvec::{self, SmallVec};
pub use super::{
AccountHash, BackendEvent, BackendEventConsumer, BackendMailbox, BackendOp,
EnvelopeHashBatch, FlagOp, IsSubscribedFn, LazyCountSet, MailBackend,
MailBackendCapabilities, MailBackendExtensionStatus, Mailbox, MailboxHash,
MailboxPermissions, ReadOnlyOp, RefreshEvent, RefreshEventKind, TagHash,
};
pub use crate::{
conf::AccountSettings,
email::{Envelope, EnvelopeHash, Flag},
error::{Error, ErrorKind, Result},
search::Query,
Collection, HeaderName, LogLevel, SpecialUsageMailbox,
};
}
use prelude::*;
#[macro_export]
macro_rules! get_path_hash {
@ -308,7 +325,7 @@ impl BackendEventConsumer {
impl std::fmt::Debug for BackendEventConsumer {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "BackendEventConsumer")
write!(f, stringify!(BackendEventConsumer))
}
}
@ -369,19 +386,16 @@ pub enum MailBackendExtensionStatus {
Enabled { comment: Option<&'static str> },
}
pub type ResultFuture<T> = Result<Pin<Box<dyn Future<Output = Result<T>> + Send + 'static>>>;
pub trait MailBackend: ::std::fmt::Debug + Send + Sync {
fn capabilities(&self) -> MailBackendCapabilities;
fn is_online(&self) -> ResultFuture<()> {
Ok(Box::pin(async { Ok(()) }))
}
#[allow(clippy::type_complexity)]
fn fetch(
&mut self,
mailbox_hash: MailboxHash,
) -> Result<Pin<Box<dyn Stream<Item = Result<Vec<Envelope>>> + Send + 'static>>>;
) -> Result<BoxStream<'static, Result<Vec<Envelope>>>>;
fn refresh(&mut self, mailbox_hash: MailboxHash) -> ResultFuture<()>;
fn watch(&self) -> ResultFuture<()>;
@ -782,22 +796,3 @@ impl std::ops::Deref for IsSubscribedFn {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lazy_count_set() {
let mut new = LazyCountSet::default();
assert_eq!(new.len(), 0);
new.set_not_yet_seen(10);
assert_eq!(new.len(), 10);
for i in 0..10 {
assert!(new.insert_existing(EnvelopeHash(i)));
}
assert_eq!(new.len(), 10);
assert!(!new.insert_existing(EnvelopeHash(10)));
assert_eq!(new.len(), 10);
}
}

@ -0,0 +1,37 @@
//
// meli - backends module
//
// Copyright 2017 Manos Pitsidianakis
//
// This file is part of meli.
//
// meli is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// meli is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with meli. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: EUPL-1.2 OR GPL-3.0-or-later
use super::{EnvelopeHash, LazyCountSet};
#[test]
fn test_lazy_count_set() {
let mut new = LazyCountSet::default();
assert_eq!(new.len(), 0);
new.set_not_yet_seen(10);
assert_eq!(new.len(), 10);
for i in 0..10 {
assert!(new.insert_existing(EnvelopeHash(i)));
}
assert_eq!(new.len(), 10);
assert!(!new.insert_existing(EnvelopeHash(10)));
assert_eq!(new.len(), 10);
}

@ -65,10 +65,7 @@ use imap_codec::imap_types::{
};
use crate::{
backends::{
RefreshEventKind::{self, *},
*,
},
backends::{prelude::*, RefreshEventKind::*},
collection::Collection,
conf::AccountSettings,
email::{parser::BytesExt, *},

@ -31,26 +31,20 @@ use std::{
time::{Duration, Instant},
};
use futures::{
lock::{
MappedMutexGuard as FutureMappedMutexGuard, Mutex as FutureMutex,
MutexGuard as FutureMutexGuard,
},
Stream,
use futures::lock::{
MappedMutexGuard as FutureMappedMutexGuard, Mutex as FutureMutex,
MutexGuard as FutureMutexGuard,
};
use indexmap::{IndexMap, IndexSet};
use isahc::AsyncReadResponseExt;
use serde_json::{json, Value};
use smallvec::SmallVec;
use url::Url;
use crate::{
backends::*,
conf::AccountSettings,
backends::prelude::*,
email::*,
error::{Error, ErrorKind, Result},
utils::futures::{sleep, timeout},
Collection,
};
#[macro_export]

@ -25,8 +25,9 @@ use futures::lock::Mutex as FutureMutex;
use isahc::AsyncReadResponseExt;
use crate::{
backends::prelude::{BackendOp, ResultFuture},
jmap::{connection::JmapConnection, methods::download_request_format, Store},
BackendOp, EnvelopeHash, ResultFuture,
EnvelopeHash,
};
/// `BackendOp` implementor for JMAP

@ -25,7 +25,6 @@
//! specification. <https://cr.yp.to/proto/maildir.html>
use std::{
borrow::Cow,
collections::{hash_map::DefaultHasher, HashMap, HashSet},
ffi::OsStr,
fs,
@ -38,18 +37,13 @@ use std::{
time::Duration,
};
use futures::prelude::Stream;
use notify::{event::EventKind as NotifyEvent, RecommendedWatcher, RecursiveMode, Watcher};
use smallvec::SmallVec;
use super::{MaildirMailbox, MaildirOp, MaildirPathTrait};
use crate::{
backends::{RefreshEventKind::*, *},
conf::AccountSettings,
email::{Envelope, EnvelopeHash, Flag},
backends::{prelude::*, RefreshEventKind::*},
error::{Error, ErrorKind, IntoError, Result},
utils::shellexpand::ShellExpandTrait,
Collection,
};
#[derive(Clone, Debug, PartialEq)]

@ -33,16 +33,12 @@ use std::{
sync::{Arc, Mutex, RwLock},
};
use futures::stream::Stream;
use smallvec::SmallVec;
pub use stream::*;
use crate::{
backends::*,
email::Flag,
backends::prelude::*,
error::{Error, Result},
utils::shellexpand::ShellExpandTrait,
AccountSettings, Envelope, EnvelopeHash,
};
/// `BackendOp` implementor for Maildir

@ -130,6 +130,17 @@
//! # Ok::<(), melib::Error>(())
//! ```
use std::{
collections::hash_map::HashMap,
fs::File,
io::{BufReader, Read},
os::unix::io::AsRawFd,
path::{Path, PathBuf},
pin::Pin,
str::FromStr,
sync::{mpsc::channel, Arc, Mutex, RwLock},
};
use nom::{
self,
bytes::complete::tag,
@ -138,33 +149,16 @@ use nom::{
error::{Error as NomError, ErrorKind as NomErrorKind},
IResult,
};
use notify::{event::EventKind as NotifyEvent, RecommendedWatcher, RecursiveMode, Watcher};
use crate::{
backends::*,
collection::Collection,
conf::AccountSettings,
backends::prelude::*,
email::{parser::BytesExt, *},
error::{Error, ErrorKind, IntoError, Result},
get_path_hash,
utils::shellexpand::ShellExpandTrait,
};
extern crate notify;
use std::{
collections::hash_map::HashMap,
fs::File,
io::{BufReader, Read},
os::unix::io::AsRawFd,
path::{Path, PathBuf},
pin::Pin,
str::FromStr,
sync::{mpsc::channel, Arc, Mutex, RwLock},
};
use futures::Stream;
use notify::{event::EventKind as NotifyEvent, RecommendedWatcher, RecursiveMode, Watcher};
use smallvec::SmallVec;
pub mod write;
pub type Offset = usize;

@ -26,8 +26,6 @@
//! Transfer Protocol (NNTP) Additions to LIST
//! Command](https://datatracker.ietf.org/doc/html/rfc6048).
use smallvec::SmallVec;
use crate::get_path_hash;
mod store;
pub use store::*;
@ -52,13 +50,9 @@ pub use connection::*;
use futures::{lock::Mutex as FutureMutex, stream::Stream};
use crate::{
backends::*,
conf::AccountSettings,
email::*,
error::{Error, Result, ResultIntoError},
backends::prelude::*,
error::{Error, ErrorKind, Result, ResultIntoError},
utils::futures::timeout,
Collection, ErrorKind,
RefreshEventKind::NewFlags,
};
pub type UID = usize;
@ -477,7 +471,10 @@ impl MailBackend for NntpType {
BackendEvent::Refresh(RefreshEvent {
account_hash: uid_store.account_hash,
mailbox_hash,
kind: NewFlags(*env_hash, (current_val, vec![])),
kind: RefreshEventKind::NewFlags(
*env_hash,
(current_val, vec![]),
),
}),
);
}

@ -20,7 +20,6 @@
*/
use std::{
borrow::Cow,
collections::{hash_map::HashMap, BTreeMap, BTreeSet},
ffi::{CStr, CString, OsStr},
io::Read,
@ -31,17 +30,12 @@ use std::{
sync::{Arc, Mutex, RwLock},
};
use futures::Stream;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use smallvec::SmallVec;
use crate::{
backends::*,
conf::AccountSettings,
email::{Envelope, EnvelopeHash, Flag},
backends::prelude::*,
error::{Error, ErrorKind, IntoError, Result},
utils::shellexpand::ShellExpandTrait,
Collection,
};
macro_rules! call {

Loading…
Cancel
Save