Skip to content

Commit

Permalink
wip: design basic tree using petgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed May 17, 2024
1 parent 7a951b4 commit 90e12dd
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 10 deletions.
7 changes: 2 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ md5 = "0.7"
mml-lib = { version = "=1.0.12", default-features = false, features = ["derive"] }
oauth-lib = "=0.1.1"
once_cell = "1.16"
petgraph = "0.6"
process-lib = { version = "=0.4.2", features = ["derive"] }
secret-lib = { version = "=0.4.4", features = ["derive"] }
serde = { version = "1", features = ["derive"] }
Expand All @@ -86,8 +87,11 @@ uuid = { version = "0.8", features = ["v4"] }

[patch.crates-io]
# WIP: transition from `imap` to `imap-codec`
email-lib = { git = "https://git.sr.ht/~soywod/pimalaya" }
imap-client = { git = "https://github.com/soywod/imap-flow.git", branch = "session" }
tasks = { git = "https://github.com/soywod/imap-flow.git", branch = "session" }
email-lib = { path = "/home/soywod/sourcehut/pimalaya/email" }
imap-client = { path = "/home/soywod/code/imap-flow/client" }
tasks = { path = "/home/soywod/code/imap-flow/tasks" }
# email-lib = { git = "https://git.sr.ht/~soywod/pimalaya" }
# imap-client = { git = "https://github.com/soywod/imap-flow.git", branch = "session" }
# tasks = { git = "https://github.com/soywod/imap-flow.git", branch = "session" }
imap-codec = { git = "https://github.com/duesee/imap-codec.git" }
imap-types = { git = "https://github.com/duesee/imap-codec.git" }
8 changes: 8 additions & 0 deletions src/account/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ impl TomlAccountConfig {
.or(self.backend.as_ref())
}

pub fn thread_envelopes_kind(&self) -> Option<&BackendKind> {
self.envelope
.as_ref()
.and_then(|envelope| envelope.thread.as_ref())
.and_then(|thread| thread.backend.as_ref())
.or(self.backend.as_ref())
}

pub fn watch_envelopes_kind(&self) -> Option<&BackendKind> {
self.envelope
.as_ref()
Expand Down
32 changes: 32 additions & 0 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub(crate) mod wizard;

use async_trait::async_trait;
use color_eyre::Result;
use petgraph::graphmap::DiGraphMap;
use std::{fmt::Display, ops::Deref, sync::Arc};

#[cfg(feature = "imap")]
Expand All @@ -23,6 +24,7 @@ use email::{
envelope::{
get::GetEnvelope,
list::{ListEnvelopes, ListEnvelopesOptions},
thread::ThreadEnvelopes,
watch::WatchEnvelopes,
Id, SingleId,
},
Expand Down Expand Up @@ -337,6 +339,23 @@ impl email::backend::context::BackendContextBuilder for BackendContextBuilder {
}
}

fn thread_envelopes(&self) -> Option<BackendFeature<Self::Context, dyn ThreadEnvelopes>> {
match self.toml_account_config.thread_envelopes_kind() {
#[cfg(feature = "imap")]
Some(BackendKind::Imap) => self.thread_envelopes_with_some(&self.imap),
#[cfg(all(feature = "imap", feature = "account-sync"))]
Some(BackendKind::ImapCache) => {
let f = self.imap_cache.as_ref()?.thread_envelopes()?;
Some(Arc::new(move |ctx| f(ctx.imap_cache.as_ref()?)))
}
#[cfg(feature = "maildir")]
Some(BackendKind::Maildir) => self.thread_envelopes_with_some(&self.maildir),
#[cfg(feature = "notmuch")]
Some(BackendKind::Notmuch) => self.thread_envelopes_with_some(&self.notmuch),
_ => None,
}
}

fn watch_envelopes(&self) -> Option<BackendFeature<Self::Context, dyn WatchEnvelopes>> {
match self.toml_account_config.watch_envelopes_kind() {
#[cfg(feature = "imap")]
Expand Down Expand Up @@ -687,6 +706,19 @@ impl Backend {
Ok(envelopes)
}

pub async fn thread_envelopes(
&self,
folder: &str,
opts: ListEnvelopesOptions,
) -> Result<DiGraphMap<u32, u32>> {
let backend_kind = self.toml_account_config.thread_envelopes_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
let envelopes = self.backend.thread_envelopes(folder, opts).await?;
// let envelopes =
// Envelopes::from_backend(&self.backend.account_config, &id_mapper, envelopes)?;
Ok(envelopes)
}

pub async fn add_flags(&self, folder: &str, ids: &[usize], flags: &Flags) -> Result<()> {
let backend_kind = self.toml_account_config.add_flags_kind();
let id_mapper = self.build_id_mapper(folder, backend_kind)?;
Expand Down
1 change: 1 addition & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ impl TomlConfig {
}),
envelope: config.envelope.map(|c| EnvelopeConfig {
list: c.list.map(|c| c.remote),
thread: c.thread.map(|c| c.remote),
watch: c.watch.map(|c| c.remote),
#[cfg(feature = "account-sync")]
sync: c.sync,
Expand Down
11 changes: 9 additions & 2 deletions src/email/envelope/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
pub mod list;
pub mod thread;
pub mod watch;

use color_eyre::Result;
use clap::Subcommand;
use color_eyre::Result;

use crate::{config::TomlConfig, printer::Printer};

use self::{list::ListEnvelopesCommand, watch::WatchEnvelopesCommand};
use self::{
list::ListEnvelopesCommand, thread::ThreadEnvelopesCommand, watch::WatchEnvelopesCommand,
};

/// Manage envelopes.
///
Expand All @@ -19,6 +22,9 @@ pub enum EnvelopeSubcommand {
#[command(alias = "lst")]
List(ListEnvelopesCommand),

#[command()]
Thread(ThreadEnvelopesCommand),

#[command()]
Watch(WatchEnvelopesCommand),
}
Expand All @@ -28,6 +34,7 @@ impl EnvelopeSubcommand {
pub async fn execute(self, printer: &mut impl Printer, config: &TomlConfig) -> Result<()> {
match self {
Self::List(cmd) => cmd.execute(printer, config).await,
Self::Thread(cmd) => cmd.execute(printer, config).await,
Self::Watch(cmd) => cmd.execute(printer, config).await,
}
}
Expand Down
Loading

0 comments on commit 90e12dd

Please sign in to comment.