From e107d613a065be8b47e2adcaabe0ad568ab765a0 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Tue, 18 Jun 2024 15:26:56 +0300 Subject: [PATCH] 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 --- meli/src/accounts.rs | 5 +-- melib/src/backends.rs | 81 +++++++++++++++++------------------- melib/src/backends/tests.rs | 37 ++++++++++++++++ melib/src/imap/mod.rs | 5 +-- melib/src/jmap/mod.rs | 14 ++----- melib/src/jmap/operations.rs | 3 +- melib/src/maildir/backend.rs | 8 +--- melib/src/maildir/mod.rs | 6 +-- melib/src/mbox/mod.rs | 32 ++++++-------- melib/src/nntp/mod.rs | 15 +++---- melib/src/notmuch/mod.rs | 8 +--- 11 files changed, 106 insertions(+), 108 deletions(-) create mode 100644 melib/src/backends/tests.rs diff --git a/meli/src/accounts.rs b/meli/src/accounts.rs index 0f7d43e5..43cba2ff 100644 --- a/meli/src/accounts.rs +++ b/meli/src/accounts.rs @@ -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; diff --git a/melib/src/backends.rs b/melib/src/backends.rs index ac5b4ae7..35fb154c 100644 --- a/melib/src/backends.rs +++ b/melib/src/backends.rs @@ -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 = crate::Result>>; + 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 = Result> + 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>> + Send + 'static>>>; + ) -> Result>>>; 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); - } -} diff --git a/melib/src/backends/tests.rs b/melib/src/backends/tests.rs new file mode 100644 index 00000000..710bdd84 --- /dev/null +++ b/melib/src/backends/tests.rs @@ -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 . +// +// 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); +} diff --git a/melib/src/imap/mod.rs b/melib/src/imap/mod.rs index 125ef47c..31cd5876 100644 --- a/melib/src/imap/mod.rs +++ b/melib/src/imap/mod.rs @@ -65,10 +65,7 @@ use imap_codec::imap_types::{ }; use crate::{ - backends::{ - RefreshEventKind::{self, *}, - *, - }, + backends::{prelude::*, RefreshEventKind::*}, collection::Collection, conf::AccountSettings, email::{parser::BytesExt, *}, diff --git a/melib/src/jmap/mod.rs b/melib/src/jmap/mod.rs index 47e79f29..859a0035 100644 --- a/melib/src/jmap/mod.rs +++ b/melib/src/jmap/mod.rs @@ -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] diff --git a/melib/src/jmap/operations.rs b/melib/src/jmap/operations.rs index 979b21cc..eee49fdc 100644 --- a/melib/src/jmap/operations.rs +++ b/melib/src/jmap/operations.rs @@ -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 diff --git a/melib/src/maildir/backend.rs b/melib/src/maildir/backend.rs index 4ab80e72..acf517b3 100644 --- a/melib/src/maildir/backend.rs +++ b/melib/src/maildir/backend.rs @@ -25,7 +25,6 @@ //! specification. 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)] diff --git a/melib/src/maildir/mod.rs b/melib/src/maildir/mod.rs index 6b406270..369a131f 100644 --- a/melib/src/maildir/mod.rs +++ b/melib/src/maildir/mod.rs @@ -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 diff --git a/melib/src/mbox/mod.rs b/melib/src/mbox/mod.rs index 73a14e62..e541f499 100644 --- a/melib/src/mbox/mod.rs +++ b/melib/src/mbox/mod.rs @@ -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; diff --git a/melib/src/nntp/mod.rs b/melib/src/nntp/mod.rs index 2d4b3a17..b920df17 100644 --- a/melib/src/nntp/mod.rs +++ b/melib/src/nntp/mod.rs @@ -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![]), + ), }), ); } diff --git a/melib/src/notmuch/mod.rs b/melib/src/notmuch/mod.rs index 2b2ec291..541a4436 100644 --- a/melib/src/notmuch/mod.rs +++ b/melib/src/notmuch/mod.rs @@ -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 {