Fix rustfmt suggestions
parent
0a83b99e7c
commit
e4a1ab8a09
|
@ -517,11 +517,11 @@ impl MailBackend for ImapType {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn::std::any::Any {
|
fn as_any(&self) -> &dyn ::std::any::Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_any_mut(&mut self) -> &mut dyn::std::any::Any {
|
fn as_any_mut(&mut self) -> &mut dyn ::std::any::Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,10 +34,10 @@ extern crate notify;
|
||||||
use self::notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
|
use self::notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use std::collections::{HashMap, HashSet, hash_map::DefaultHasher};
|
use std::collections::{hash_map::DefaultHasher, HashMap, HashSet};
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::hash::{Hasher, Hash};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
|
@ -618,7 +618,7 @@ impl MailBackend for MboxType {
|
||||||
Err(MeliError::new("Unimplemented."))
|
Err(MeliError::new("Unimplemented."))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn::std::any::Any {
|
fn as_any(&self) -> &dyn ::std::any::Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -621,7 +621,7 @@ impl MailBackend for NotmuchDb {
|
||||||
crate::backends::MaildirType::save_to_mailbox(path, bytes, flags)
|
crate::backends::MaildirType::save_to_mailbox(path, bytes, flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_any(&self) -> &dyn::std::any::Any {
|
fn as_any(&self) -> &dyn ::std::any::Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
103
src/sqlite3.rs
103
src/sqlite3.rs
|
@ -21,9 +21,11 @@
|
||||||
|
|
||||||
/*! Use an sqlite3 database for fast searching.
|
/*! Use an sqlite3 database for fast searching.
|
||||||
*/
|
*/
|
||||||
use smallvec::SmallVec;
|
|
||||||
use melib::search::{escape_double_quote, query, Query::{self, *}};
|
|
||||||
use crate::melib::parsec::Parser;
|
use crate::melib::parsec::Parser;
|
||||||
|
use melib::search::{
|
||||||
|
escape_double_quote, query,
|
||||||
|
Query::{self, *},
|
||||||
|
};
|
||||||
use melib::{
|
use melib::{
|
||||||
backends::MailBackend,
|
backends::MailBackend,
|
||||||
email::{Envelope, EnvelopeHash},
|
email::{Envelope, EnvelopeHash},
|
||||||
|
@ -32,8 +34,9 @@ use melib::{
|
||||||
MeliError, Result, ERROR,
|
MeliError, Result, ERROR,
|
||||||
};
|
};
|
||||||
use rusqlite::{params, Connection};
|
use rusqlite::{params, Connection};
|
||||||
use std::path::PathBuf;
|
use smallvec::SmallVec;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
use std::path::PathBuf;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
pub fn db_path() -> Result<PathBuf> {
|
pub fn db_path() -> Result<PathBuf> {
|
||||||
|
@ -70,7 +73,9 @@ pub fn db_path() -> Result<PathBuf> {
|
||||||
pub fn open_db() -> Result<Connection> {
|
pub fn open_db() -> Result<Connection> {
|
||||||
let db_path = db_path()?;
|
let db_path = db_path()?;
|
||||||
if !db_path.exists() {
|
if !db_path.exists() {
|
||||||
return Err(MeliError::new("Database hasn't been initialised. Run `reindex` command"));
|
return Err(MeliError::new(
|
||||||
|
"Database hasn't been initialised. Run `reindex` command",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Connection::open(&db_path).map_err(|e| MeliError::new(e.to_string()))
|
Connection::open(&db_path).map_err(|e| MeliError::new(e.to_string()))
|
||||||
}
|
}
|
||||||
|
@ -166,7 +171,11 @@ END; ",
|
||||||
Ok(conn)
|
Ok(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(envelope: &Envelope, backend: &Arc<RwLock<Box<dyn MailBackend>>>, acc_name: &str) -> Result<()> {
|
pub fn insert(
|
||||||
|
envelope: &Envelope,
|
||||||
|
backend: &Arc<RwLock<Box<dyn MailBackend>>>,
|
||||||
|
acc_name: &str,
|
||||||
|
) -> Result<()> {
|
||||||
let conn = open_db()?;
|
let conn = open_db()?;
|
||||||
let backend_lck = backend.read().unwrap();
|
let backend_lck = backend.read().unwrap();
|
||||||
let op = backend_lck.operation(envelope.hash());
|
let op = backend_lck.operation(envelope.hash());
|
||||||
|
@ -193,7 +202,10 @@ pub fn insert(envelope: &Envelope, backend: &Arc<RwLock<Box<dyn MailBackend>>>,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(err) = conn.execute("INSERT OR IGNORE INTO accounts (name) VALUES (?1)", params![acc_name, ]) {
|
if let Err(err) = conn.execute(
|
||||||
|
"INSERT OR IGNORE INTO accounts (name) VALUES (?1)",
|
||||||
|
params![acc_name,],
|
||||||
|
) {
|
||||||
debug!(
|
debug!(
|
||||||
"Failed to insert envelope {}: {}",
|
"Failed to insert envelope {}: {}",
|
||||||
envelope.message_id_display(),
|
envelope.message_id_display(),
|
||||||
|
@ -210,8 +222,15 @@ pub fn insert(envelope: &Envelope, backend: &Arc<RwLock<Box<dyn MailBackend>>>,
|
||||||
return Err(MeliError::new(err.to_string()));
|
return Err(MeliError::new(err.to_string()));
|
||||||
}
|
}
|
||||||
let account_id: i32 = {
|
let account_id: i32 = {
|
||||||
let mut stmt = conn.prepare("SELECT id FROM accounts WHERE name = ?").unwrap();
|
let mut stmt = conn
|
||||||
let x = stmt.query_map(params![acc_name], |row| row.get(0)).unwrap().next().unwrap().unwrap();
|
.prepare("SELECT id FROM accounts WHERE name = ?")
|
||||||
|
.unwrap();
|
||||||
|
let x = stmt
|
||||||
|
.query_map(params![acc_name], |row| row.get(0))
|
||||||
|
.unwrap()
|
||||||
|
.next()
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
x
|
x
|
||||||
};
|
};
|
||||||
if let Err(err) = conn.execute(
|
if let Err(err) = conn.execute(
|
||||||
|
@ -239,46 +258,66 @@ pub fn insert(envelope: &Envelope, backend: &Arc<RwLock<Box<dyn MailBackend>>>,
|
||||||
|
|
||||||
pub fn remove(env_hash: EnvelopeHash) -> Result<()> {
|
pub fn remove(env_hash: EnvelopeHash) -> Result<()> {
|
||||||
let conn = open_db()?;
|
let conn = open_db()?;
|
||||||
if let Err(err) = conn.execute(
|
if let Err(err) = conn
|
||||||
"DELETE FROM envelopes WHERE hash = ?",
|
.execute(
|
||||||
params![env_hash.to_be_bytes().to_vec(), ])
|
"DELETE FROM envelopes WHERE hash = ?",
|
||||||
.map_err(|e| MeliError::new(e.to_string())) {
|
params![env_hash.to_be_bytes().to_vec(),],
|
||||||
debug!(
|
)
|
||||||
|
.map_err(|e| MeliError::new(e.to_string()))
|
||||||
|
{
|
||||||
|
debug!(
|
||||||
|
"Failed to remove envelope {}: {}",
|
||||||
|
env_hash,
|
||||||
|
err.to_string()
|
||||||
|
);
|
||||||
|
log(
|
||||||
|
format!(
|
||||||
"Failed to remove envelope {}: {}",
|
"Failed to remove envelope {}: {}",
|
||||||
env_hash,
|
env_hash,
|
||||||
err.to_string()
|
err.to_string()
|
||||||
);
|
),
|
||||||
log(
|
ERROR,
|
||||||
format!(
|
);
|
||||||
"Failed to remove envelope {}: {}",
|
return Err(err);
|
||||||
env_hash,
|
|
||||||
err.to_string()
|
|
||||||
),
|
|
||||||
ERROR,
|
|
||||||
);
|
|
||||||
return Err(err);
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn index(context: &mut crate::state::Context, account_name: &str) -> Result<()> {
|
pub fn index(context: &mut crate::state::Context, account_name: &str) -> Result<()> {
|
||||||
let account = if let Some(a) = context.accounts.iter().find(|acc| acc.name() == account_name) {
|
let account = if let Some(a) = context
|
||||||
a} else {
|
.accounts
|
||||||
return Err(MeliError::new(format!("Account {} was not found.", account_name)));
|
.iter()
|
||||||
|
.find(|acc| acc.name() == account_name)
|
||||||
|
{
|
||||||
|
a
|
||||||
|
} else {
|
||||||
|
return Err(MeliError::new(format!(
|
||||||
|
"Account {} was not found.",
|
||||||
|
account_name
|
||||||
|
)));
|
||||||
};
|
};
|
||||||
|
|
||||||
let (acc_name, acc_mutex, backend_mutex):( String, Arc<RwLock<_>>, Arc<_>) = if *account.settings.conf.cache_type() != crate::conf::CacheType::Sqlite3 {
|
let (acc_name, acc_mutex, backend_mutex): (String, Arc<RwLock<_>>, Arc<_>) =
|
||||||
return Err(MeliError::new(format!("Account {} doesn't have an sqlite3 search backend.", account_name)));
|
if *account.settings.conf.cache_type() != crate::conf::CacheType::Sqlite3 {
|
||||||
} else {
|
return Err(MeliError::new(format!(
|
||||||
|
"Account {} doesn't have an sqlite3 search backend.",
|
||||||
|
account_name
|
||||||
|
)));
|
||||||
|
} else {
|
||||||
(
|
(
|
||||||
account.name().to_string(),
|
account.name().to_string(),
|
||||||
account.collection.envelopes.clone(),
|
account.collection.envelopes.clone(),
|
||||||
account.backend.clone(),
|
account.backend.clone(),
|
||||||
)};
|
)
|
||||||
|
};
|
||||||
let conn = open_or_create_db()?;
|
let conn = open_or_create_db()?;
|
||||||
let work_context = context.work_controller().get_context();
|
let work_context = context.work_controller().get_context();
|
||||||
let env_hashes =
|
let env_hashes = acc_mutex
|
||||||
acc_mutex.read().unwrap().keys().cloned().collect::<Vec<_>>();
|
.read()
|
||||||
|
.unwrap()
|
||||||
|
.keys()
|
||||||
|
.cloned()
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
/* Sleep, index and repeat in order not to block the main process */
|
/* Sleep, index and repeat in order not to block the main process */
|
||||||
let handle = std::thread::Builder::new().name(String::from("rebuilding index")).spawn(move || {
|
let handle = std::thread::Builder::new().name(String::from("rebuilding index")).spawn(move || {
|
||||||
|
|
Loading…
Reference in New Issue