melib: move backends out of the backends module

No reason to have such a deep module tree.
pull/262/head
Manos Pitsidianakis 11 months ago
parent 9216e7bc65
commit 1e084c1d85
No known key found for this signature in database
GPG Key ID: 7729C7707F7E09D0

@ -460,7 +460,7 @@ define_commands!([
let (input, _) = is_a(" ")(input)?; let (input, _) = is_a(" ")(input)?;
let (input, path) = quoted_argument(input.trim())?; let (input, path) = quoted_argument(input.trim())?;
let (input, _) = eof(input)?; let (input, _) = eof(input)?;
Ok((input, Listing(ExportMbox(Some(melib::backends::mbox::MboxFormat::MboxCl2), path.to_string().into())))) Ok((input, Listing(ExportMbox(Some(melib::mbox::MboxFormat::MboxCl2), path.to_string().into()))))
} }
) )
}, },

@ -49,7 +49,7 @@ pub enum ListingAction {
MoveTo(MailboxPath), MoveTo(MailboxPath),
MoveToOtherAccount(AccountName, MailboxPath), MoveToOtherAccount(AccountName, MailboxPath),
Import(PathBuf, MailboxPath), Import(PathBuf, MailboxPath),
ExportMbox(Option<melib::backends::mbox::MboxFormat>, PathBuf), ExportMbox(Option<melib::mbox::MboxFormat>, PathBuf),
Delete, Delete,
OpenInNewTab, OpenInNewTab,
Tag(TagAction), Tag(TagAction),

@ -670,7 +670,7 @@ pub trait MailListingTrait: ListingTrait {
let fut: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>> = let fut: Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>> =
Box::pin(async move { Box::pin(async move {
let cl = async move { let cl = async move {
use melib::backends::mbox::MboxMetadata; use melib::mbox::MboxMetadata;
let bytes: Vec<Vec<u8>> = try_join_all(futures?).await?; let bytes: Vec<Vec<u8>> = try_join_all(futures?).await?;
let envs: Vec<_> = envs_to_set let envs: Vec<_> = envs_to_set
.iter() .iter()

@ -31,7 +31,7 @@ extern crate serde_json;
extern crate smallvec; extern crate smallvec;
extern crate termion; extern crate termion;
use melib::{backends::imap::managesieve::ManageSieveConnection, Result}; use melib::{imap::managesieve::ManageSieveConnection, Result};
#[macro_use] #[macro_use]
pub mod types; pub mod types;

@ -22,20 +22,6 @@
pub mod utf7; pub mod utf7;
use smallvec::SmallVec; use smallvec::SmallVec;
#[cfg(feature = "imap")]
pub mod imap;
#[cfg(feature = "nntp")]
pub mod nntp;
#[cfg(feature = "notmuch")]
pub mod notmuch;
#[cfg(feature = "notmuch")]
pub use self::notmuch::NotmuchDb;
#[cfg(feature = "jmap")]
pub mod jmap;
#[cfg(feature = "maildir")]
pub mod maildir;
#[cfg(feature = "mbox")]
pub mod mbox;
use std::{ use std::{
any::Any, any::Any,
borrow::Cow, borrow::Cow,
@ -45,20 +31,13 @@ use std::{
future::Future, future::Future,
ops::Deref, ops::Deref,
pin::Pin, pin::Pin,
sync::{Arc, RwLock}, sync::Arc,
}; };
use futures::stream::Stream; use futures::stream::Stream;
#[cfg(feature = "imap")]
pub use self::imap::ImapType;
#[cfg(feature = "maildir")]
use self::maildir::MaildirType;
#[cfg(feature = "mbox")]
use self::mbox::MboxType;
#[cfg(feature = "nntp")]
pub use self::nntp::NntpType;
use super::email::{Envelope, EnvelopeHash, Flag}; use super::email::{Envelope, EnvelopeHash, Flag};
use crate::{ use crate::{
conf::AccountSettings, conf::AccountSettings,
error::{Error, ErrorKind, Result}, error::{Error, ErrorKind, Result},
@ -153,6 +132,8 @@ impl Backends {
}; };
#[cfg(feature = "maildir")] #[cfg(feature = "maildir")]
{ {
use crate::maildir::MaildirType;
b.register( b.register(
"maildir".to_string(), "maildir".to_string(),
Backend { Backend {
@ -163,6 +144,8 @@ impl Backends {
} }
#[cfg(feature = "mbox")] #[cfg(feature = "mbox")]
{ {
use crate::mbox::MboxType;
b.register( b.register(
"mbox".to_string(), "mbox".to_string(),
Backend { Backend {
@ -173,26 +156,32 @@ impl Backends {
} }
#[cfg(feature = "imap")] #[cfg(feature = "imap")]
{ {
use crate::imap::ImapType;
b.register( b.register(
"imap".to_string(), "imap".to_string(),
Backend { Backend {
create_fn: Box::new(|| Box::new(|f, i, ev| imap::ImapType::new(f, i, ev))), create_fn: Box::new(|| Box::new(|f, i, ev| ImapType::new(f, i, ev))),
validate_conf_fn: Box::new(imap::ImapType::validate_config), validate_conf_fn: Box::new(ImapType::validate_config),
}, },
); );
} }
#[cfg(feature = "nntp")] #[cfg(feature = "nntp")]
{ {
use crate::nntp::NntpType;
b.register( b.register(
"nntp".to_string(), "nntp".to_string(),
Backend { Backend {
create_fn: Box::new(|| Box::new(|f, i, ev| nntp::NntpType::new(f, i, ev))), create_fn: Box::new(|| Box::new(|f, i, ev| NntpType::new(f, i, ev))),
validate_conf_fn: Box::new(nntp::NntpType::validate_config), validate_conf_fn: Box::new(NntpType::validate_config),
}, },
); );
} }
#[cfg(feature = "notmuch")] #[cfg(feature = "notmuch")]
{ {
use crate::notmuch::NotmuchDb;
b.register( b.register(
"notmuch".to_string(), "notmuch".to_string(),
Backend { Backend {
@ -203,11 +192,13 @@ impl Backends {
} }
#[cfg(feature = "jmap")] #[cfg(feature = "jmap")]
{ {
use crate::jmap::JmapType;
b.register( b.register(
"jmap".to_string(), "jmap".to_string(),
Backend { Backend {
create_fn: Box::new(|| Box::new(|f, i, ev| jmap::JmapType::new(f, i, ev))), create_fn: Box::new(|| Box::new(|f, i, ev| JmapType::new(f, i, ev))),
validate_conf_fn: Box::new(jmap::JmapType::validate_config), validate_conf_fn: Box::new(JmapType::validate_config),
}, },
); );
} }
@ -675,8 +666,8 @@ impl EnvelopeHashBatch {
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub struct LazyCountSet { pub struct LazyCountSet {
not_yet_seen: usize, pub not_yet_seen: usize,
set: BTreeSet<EnvelopeHash>, pub set: BTreeSet<EnvelopeHash>,
} }
impl fmt::Debug for LazyCountSet { impl fmt::Debug for LazyCountSet {
@ -744,21 +735,7 @@ impl LazyCountSet {
} }
} }
#[test] pub struct IsSubscribedFn(pub Box<dyn Fn(&str) -> bool + Send + Sync>);
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);
}
pub struct IsSubscribedFn(Box<dyn Fn(&str) -> bool + Send + Sync>);
impl std::fmt::Debug for IsSubscribedFn { impl std::fmt::Debug for IsSubscribedFn {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@ -772,3 +749,22 @@ impl std::ops::Deref for IsSubscribedFn {
&self.0 &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);
}
}

@ -284,7 +284,7 @@ pub mod parser {
} }
pub fn sieve_name(input: &[u8]) -> IResult<&[u8], &[u8]> { pub fn sieve_name(input: &[u8]) -> IResult<&[u8], &[u8]> {
crate::backends::imap::protocol_parser::string_token(input) crate::imap::protocol_parser::string_token(input)
} }
// *(sieve-name [SP "ACTIVE"] CRLF) // *(sieve-name [SP "ACTIVE"] CRLF)

@ -899,11 +899,11 @@ impl MailBackend for ImapType {
})) }))
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn std::any::Any {
self self
} }
fn as_any_mut(&mut self) -> &mut dyn Any { fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self self
} }

@ -26,16 +26,15 @@ use imap_codec::{command::CommandBody, search::SearchKey, sequence::SequenceSet}
use super::{ImapConnection, MailboxSelection, UID}; use super::{ImapConnection, MailboxSelection, UID};
use crate::{ use crate::{
backends::{ backends::{
imap::protocol_parser::{
generate_envelope_hash, FetchResponse, ImapLineSplit, RequiredResponses,
UntaggedResponse,
},
BackendMailbox, RefreshEvent, BackendMailbox, RefreshEvent,
RefreshEventKind::{self, *}, RefreshEventKind::{self, *},
TagHash, TagHash,
}, },
email::common_attributes, email::common_attributes,
error::*, error::*,
imap::protocol_parser::{
generate_envelope_hash, FetchResponse, ImapLineSplit, RequiredResponses, UntaggedResponse,
},
}; };
impl ImapConnection { impl ImapConnection {

@ -20,22 +20,24 @@
*/ */
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{BTreeSet, HashMap, HashSet},
convert::TryFrom, convert::TryFrom,
pin::Pin,
str::FromStr, str::FromStr,
sync::{Arc, Mutex, RwLock}, sync::{Arc, Mutex, RwLock},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use futures::lock::Mutex as FutureMutex; use futures::{lock::Mutex as FutureMutex, Stream};
use isahc::{config::RedirectPolicy, AsyncReadResponseExt, HttpClient}; use isahc::{config::RedirectPolicy, AsyncReadResponseExt, HttpClient};
use serde_json::Value; use serde_json::Value;
use smallvec::SmallVec;
use crate::{ use crate::{
backends::*, backends::*,
conf::AccountSettings, conf::AccountSettings,
email::*, email::*,
error::{Error, Result}, error::{Error, ErrorKind, Result},
utils::futures::{sleep, timeout}, utils::futures::{sleep, timeout},
Collection, Collection,
}; };
@ -499,11 +501,11 @@ impl MailBackend for JmapType {
})) }))
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn std::any::Any {
self self
} }
fn as_any_mut(&mut self) -> &mut dyn Any { fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self self
} }

@ -27,8 +27,8 @@ use serde_json::{value::RawValue, Value};
use super::*; use super::*;
use crate::{ use crate::{
backends::jmap::rfc8620::bool_false,
email::address::{Address, MailboxAddress}, email::address::{Address, MailboxAddress},
jmap::rfc8620::bool_false,
utils::datetime, utils::datetime,
}; };

@ -19,7 +19,7 @@
* along with meli. If not, see <http://www.gnu.org/licenses/>. * along with meli. If not, see <http://www.gnu.org/licenses/>.
*/ */
use crate::backends::jmap::{ use crate::jmap::{
protocol::Method, protocol::Method,
rfc8620::{Object, ResultField}, rfc8620::{Object, ResultField},
}; };

@ -158,6 +158,18 @@ pub mod utils;
#[cfg(feature = "gpgme")] #[cfg(feature = "gpgme")]
pub mod gpgme; pub mod gpgme;
#[cfg(feature = "imap")]
pub mod imap;
#[cfg(feature = "jmap")]
pub mod jmap;
#[cfg(feature = "maildir")]
pub mod maildir;
#[cfg(feature = "mbox")]
pub mod mbox;
#[cfg(feature = "nntp")]
pub mod nntp;
#[cfg(feature = "notmuch")]
pub mod notmuch;
#[cfg(feature = "smtp")] #[cfg(feature = "smtp")]
pub mod smtp; pub mod smtp;

@ -25,6 +25,7 @@
//! specification. <https://cr.yp.to/proto/maildir.html> //! specification. <https://cr.yp.to/proto/maildir.html>
use futures::prelude::Stream; use futures::prelude::Stream;
use smallvec::SmallVec;
use super::{MaildirMailbox, MaildirOp, MaildirPathTrait}; use super::{MaildirMailbox, MaildirOp, MaildirPathTrait};
use crate::{ use crate::{
@ -1063,11 +1064,11 @@ impl MailBackend for MaildirType {
) )
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn std::any::Any {
self self
} }
fn as_any_mut(&mut self) -> &mut dyn Any { fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self self
} }
} }

@ -26,14 +26,16 @@ pub use self::backend::*;
mod stream; mod stream;
use std::{ use std::{
collections::hash_map::DefaultHasher, collections::hash_map::DefaultHasher,
collections::HashMap,
fs, fs,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
io::{BufReader, Read}, io::{BufReader, Read},
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::{Arc, Mutex}, sync::{Arc, Mutex, RwLock},
}; };
use futures::stream::Stream; use futures::stream::Stream;
use smallvec::SmallVec;
pub use stream::*; pub use stream::*;
use crate::{ use crate::{
@ -41,6 +43,7 @@ use crate::{
email::Flag, email::Flag,
error::{Error, Result}, error::{Error, Result},
utils::shellexpand::ShellExpandTrait, utils::shellexpand::ShellExpandTrait,
AccountSettings, Envelope, EnvelopeHash,
}; };
/// `BackendOp` implementor for Maildir /// `BackendOp` implementor for Maildir

@ -32,7 +32,7 @@ use futures::{
}; };
use super::*; use super::*;
use crate::backends::maildir::backend::move_to_cur; use crate::maildir::backend::move_to_cur;
type Payload = Pin<Box<dyn Future<Output = Result<Vec<Envelope>>> + Send + 'static>>; type Payload = Pin<Box<dyn Future<Output = Result<Vec<Envelope>>> + Send + 'static>>;

@ -150,12 +150,16 @@ use crate::{
}; };
extern crate notify; extern crate notify;
use futures::Stream;
use smallvec::SmallVec;
use std::{ use std::{
collections::hash_map::HashMap, collections::hash_map::HashMap,
fs::File, fs::File,
io::{BufReader, Read}, io::{BufReader, Read},
os::unix::io::AsRawFd, os::unix::io::AsRawFd,
path::{Path, PathBuf}, path::{Path, PathBuf},
pin::Pin,
str::FromStr, str::FromStr,
sync::{mpsc::channel, Arc, Mutex, RwLock}, sync::{mpsc::channel, Arc, Mutex, RwLock},
}; };
@ -1175,11 +1179,11 @@ impl MailBackend for MboxType {
)) ))
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn std::any::Any {
self self
} }
fn as_any_mut(&mut self) -> &mut dyn Any { fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self self
} }

@ -57,7 +57,7 @@ use crate::{
email::*, email::*,
error::{Error, Result, ResultIntoError}, error::{Error, Result, ResultIntoError},
utils::futures::timeout, utils::futures::timeout,
Collection, Collection, ErrorKind,
RefreshEventKind::NewFlags, RefreshEventKind::NewFlags,
}; };
pub type UID = usize; pub type UID = usize;
@ -508,11 +508,11 @@ impl MailBackend for NntpType {
Err(Error::new("NNTP doesn't support deletion.")) Err(Error::new("NNTP doesn't support deletion."))
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn std::any::Any {
self self
} }
fn as_any_mut(&mut self) -> &mut dyn Any { fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self self
} }

@ -27,8 +27,8 @@ pub use inner::*;
#[cfg(feature = "sqlite3")] #[cfg(feature = "sqlite3")]
mod inner { mod inner {
use crate::{ use crate::{
backends::nntp::UID,
email::Flag, email::Flag,
nntp::UID,
utils::sqlite3::{self, Connection, DatabaseDescription}, utils::sqlite3::{self, Connection, DatabaseDescription},
EnvelopeHash, MailboxHash, Result, EnvelopeHash, MailboxHash, Result,
}; };

@ -25,9 +25,11 @@ use std::{
io::Read, io::Read,
os::unix::ffi::OsStrExt, os::unix::ffi::OsStrExt,
path::{Path, PathBuf}, path::{Path, PathBuf},
pin::Pin,
sync::{Arc, Mutex, RwLock}, sync::{Arc, Mutex, RwLock},
}; };
use futures::Stream;
use smallvec::SmallVec; use smallvec::SmallVec;
use crate::{ use crate::{
@ -36,7 +38,7 @@ use crate::{
email::{Envelope, EnvelopeHash, Flag}, email::{Envelope, EnvelopeHash, Flag},
error::{Error, Result}, error::{Error, Result},
utils::shellexpand::ShellExpandTrait, utils::shellexpand::ShellExpandTrait,
Collection, Collection, ErrorKind,
}; };
macro_rules! call { macro_rules! call {
@ -798,7 +800,7 @@ impl MailBackend for NotmuchDb {
.as_ref() .as_ref()
.unwrap_or(&self.path) .unwrap_or(&self.path)
.to_path_buf(); .to_path_buf();
MaildirType::save_to_mailbox(path, bytes, flags)?; crate::maildir::MaildirType::save_to_mailbox(path, bytes, flags)?;
Ok(Box::pin(async { Ok(()) })) Ok(Box::pin(async { Ok(()) }))
} }
@ -976,11 +978,11 @@ impl MailBackend for NotmuchDb {
self.collection.clone() self.collection.clone()
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn std::any::Any {
self self
} }
fn as_any_mut(&mut self) -> &mut dyn Any { fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self self
} }
Loading…
Cancel
Save