~soywod/pimalaya

email-lib: removing some redundencies v1 APPLIED

Perma Alesheikh: 8
 remove useless Into conversions
 remove the redundent referencing
 remove redundent return keyword
 use as_deref instead of as_ref and map
 create 0 as u64 without type casting
 remove redundent import of env_logger
 simplify and remove conversion to the sametype
 use concise operators

 16 files changed, 21 insertions(+), 21 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~soywod/pimalaya/patches/49339/mbox | git am -3
Learn more about email & git

[PATCH email-lib 1/8] remove useless Into conversions Export this patch

In these cases, the item already has the type that we expect from into
conversion.

Signed-off-by: Perma Alesheikh <me@prma.dev>
---
 email/src/account/discover/mod.rs              | 2 +-
 mml/examples/pgp-commands.rs                   | 2 +-
 mml/examples/pgp-gpg.rs                        | 2 +-
 mml/src/message/body/compiler/parsers/parts.rs | 4 ++--
 mml/src/message/body/interpreter.rs            | 2 +-
 mml/src/pgp/native.rs                          | 2 +-
 6 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/email/src/account/discover/mod.rs b/email/src/account/discover/mod.rs
index 5c7ff33..08c0cc5 100644
--- a/email/src/account/discover/mod.rs
+++ b/email/src/account/discover/mod.rs
@@ -206,7 +206,7 @@ async fn from_dns_mx(
        Err(err) => {
            trace!("{err}");
            debug!("ISP discovery failed for {domain}, falling back to TXT records");
            from_dns_txt(http, &dns, &domain).await
            from_dns_txt(http, dns, domain).await
        }
    }
}
diff --git a/mml/examples/pgp-commands.rs b/mml/examples/pgp-commands.rs
index 99f4ec6..bb7deeb 100644
--- a/mml/examples/pgp-commands.rs
+++ b/mml/examples/pgp-commands.rs
@@ -30,7 +30,7 @@ async fn main() {
            sign_cmd: Some(Cmd::from("gpg --homedir ./tests/gpg-home -saq")),
            verify_cmd: Some(Cmd::from("gpg --homedir ./tests/gpg-home --verify -q")),
        }))
        .build(&mml)
        .build(mml)
        .unwrap();
    let mime = mml_compiler.compile().await.unwrap().into_string().unwrap();

diff --git a/mml/examples/pgp-gpg.rs b/mml/examples/pgp-gpg.rs
index adbd544..bd81c58 100644
--- a/mml/examples/pgp-gpg.rs
+++ b/mml/examples/pgp-gpg.rs
@@ -23,7 +23,7 @@ async fn main() {
        .with_pgp(Pgp::Gpg(Gpg {
            home_dir: Some(PathBuf::from("./tests/gpg-home")),
        }))
        .build(&mml)
        .build(mml)
        .unwrap();
    let mime = mml_compiler.compile().await.unwrap().into_string().unwrap();

diff --git a/mml/src/message/body/compiler/parsers/parts.rs b/mml/src/message/body/compiler/parsers/parts.rs
index 41173a3..3a11cbf 100644
--- a/mml/src/message/body/compiler/parsers/parts.rs
+++ b/mml/src/message/body/compiler/parsers/parts.rs
@@ -177,7 +177,7 @@ fn single_html_part() {
                ))
                .into_result(),
            Ok(Part::Single(
                HashMap::from_iter([(TYPE, "text/html".into())]),
                HashMap::from_iter([(TYPE, "text/html")]),
                "<h1>This is a HTML text part.</h1>\n",
            )),
        );
@@ -315,7 +315,7 @@ fn simple_mml() {
                ))
                .into_result(),
            Ok(vec![Part::Multi(
                HashMap::from_iter([(TYPE.into(), "alternative")]),
                HashMap::from_iter([(TYPE, "alternative")]),
                vec![
                    Part::PlainText("This is a plain text part.\n"),
                    Part::Single(
diff --git a/mml/src/message/body/interpreter.rs b/mml/src/message/body/interpreter.rs
index 080ece2..b1e53cc 100644
--- a/mml/src/message/body/interpreter.rs
+++ b/mml/src/message/body/interpreter.rs
@@ -501,7 +501,7 @@ async fn interpret_part(&self, msg: &Message<'_>, part: &MessagePart<'_>) -> Res
            }
            #[cfg(feature = "pgp")]
            PartType::Multipart(ids) if ctype == "multipart/signed" => {
                match self.verify_msg(msg, &ids).await {
                match self.verify_msg(msg, ids).await {
                    Ok(()) => {
                        debug!("email part successfully verified using pgp");
                    }
diff --git a/mml/src/pgp/native.rs b/mml/src/pgp/native.rs
index c7cee6c..c598bea 100644
--- a/mml/src/pgp/native.rs
+++ b/mml/src/pgp/native.rs
@@ -78,7 +78,7 @@ pub async fn get(&self, recipient: impl ToString) -> Result<SignedSecretKey> {
            ))?),
            Self::Raw(skey) => Ok(skey.clone()),
            Self::Path(path) => {
                let path = shellexpand_path(&path);
                let path = shellexpand_path(path);
                let skey = pgp::read_skey_from_file(path)
                    .await
                    .map_err(Error::ReadNativePgpSecretKeyError)?;
-- 
2.43.0

[PATCH email-lib 2/8] remove the redundent referencing Export this patch

Since these identifiers already have the value of a reference, we don't
need to reference them again.

Signed-off-by: Perma Alesheikh <me@prma.dev>
---
 mml/examples/pgp-native.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mml/examples/pgp-native.rs b/mml/examples/pgp-native.rs
index 1161107..f7b8c5d 100644
--- a/mml/examples/pgp-native.rs
+++ b/mml/examples/pgp-native.rs
@@ -45,7 +45,7 @@ async fn main() {
                bob_pkey.clone(),
            )],
        }))
        .build(&mml)
        .build(mml)
        .unwrap();
    let mime = mml_compiler.compile().await.unwrap().into_string().unwrap();

-- 
2.43.0

[PATCH email-lib 3/8] remove redundent return keyword Export this patch

We can more concisely return the expression in the block.

Signed-off-by: Perma Alesheikh <me@prma.dev>
---
 email/src/email/envelope/list/imap.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/email/src/email/envelope/list/imap.rs b/email/src/email/envelope/list/imap.rs
index d175e59..86176c0 100644
--- a/email/src/email/envelope/list/imap.rs
+++ b/email/src/email/envelope/list/imap.rs
@@ -95,7 +95,7 @@ async fn list_envelopes(
fn build_page_range(page: usize, page_size: usize, size: usize) -> result::Result<String, Error> {
    let page_cursor = page * page_size;
    if page_cursor >= size {
        return Err(Error::BuildPageRangeOutOfBoundsError(page + 1))?;
        Err(Error::BuildPageRangeOutOfBoundsError(page + 1))?
    }

    let range = if page_size == 0 {
-- 
2.43.0

[PATCH email-lib 4/8] use as_deref instead of as_ref and map Export this patch

As deref does the job in a more concise manner.

Signed-off-by: Perma Alesheikh <me@prma.dev>
---
 email/src/notmuch/config.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/email/src/notmuch/config.rs b/email/src/notmuch/config.rs
index e78e691..63a54fb 100644
--- a/email/src/notmuch/config.rs
+++ b/email/src/notmuch/config.rs
@@ -78,6 +78,6 @@ pub fn find_config_path(&self) -> Option<&Path> {

    /// Find the Notmuch profile.
    pub fn find_profile(&self) -> Option<&str> {
        self.profile.as_ref().map(String::as_str)
        self.profile.as_deref()
    }
}
-- 
2.43.0

[PATCH email-lib 5/8] create 0 as u64 without type casting Export this patch

This is just more concise. The compiler probably would have optimized
this part away, anyways.

Signed-off-by: Perma Alesheikh <me@prma.dev>
---
 mml/src/message/body/compiler/mod.rs | 6 +++---
 mml/src/message/interpreter.rs       | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/mml/src/message/body/compiler/mod.rs b/mml/src/message/body/compiler/mod.rs
index ffad2da..9676d2a 100644
--- a/mml/src/message/body/compiler/mod.rs
+++ b/mml/src/message/body/compiler/mod.rs
@@ -383,7 +383,7 @@ async fn plain() {
            .await
            .unwrap()
            .message_id("id@localhost")
            .date(0 as u64)
            .date(0_u64)
            .write_to_string()
            .unwrap();

@@ -413,7 +413,7 @@ async fn html() {
            .await
            .unwrap()
            .message_id("id@localhost")
            .date(0 as u64)
            .date(0_u64)
            .write_to_string()
            .unwrap();

@@ -450,7 +450,7 @@ async fn attachment() {
            .await
            .unwrap()
            .message_id("id@localhost")
            .date(0 as u64)
            .date(0_u64)
            .write_to_string()
            .unwrap();

diff --git a/mml/src/message/interpreter.rs b/mml/src/message/interpreter.rs
index 6093914..43c2c01 100644
--- a/mml/src/message/interpreter.rs
+++ b/mml/src/message/interpreter.rs
@@ -310,7 +310,7 @@ fn msg_builder() -> MessageBuilder<'static> {
        MessageBuilder::new()
            .message_id("id@localhost")
            .in_reply_to("reply-id@localhost")
            .date(0 as u64)
            .date(0_u64)
            .from("from@localhost")
            .to("to@localhost")
            .subject("subject")
@@ -402,7 +402,7 @@ async fn mml_markup_escaped() {
        let msg_builder = MessageBuilder::new()
            .message_id("id@localhost")
            .in_reply_to("reply-id@localhost")
            .date(0 as u64)
            .date(0_u64)
            .from("from@localhost")
            .to("to@localhost")
            .subject("subject")
-- 
2.43.0

[PATCH email-lib 6/8] remove redundent import of env_logger Export this patch

It does not do anything.

Signed-off-by: Perma Alesheikh <me@prma.dev>
---
 email/tests/sync.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/email/tests/sync.rs b/email/tests/sync.rs
index 415e129..d188ae9 100644
--- a/email/tests/sync.rs
+++ b/email/tests/sync.rs
@@ -15,7 +15,7 @@
    },
    maildir::config::MaildirConfig,
};
use env_logger;

use mail_builder::MessageBuilder;
use secret::Secret;
use std::{
-- 
2.43.0

[PATCH email-lib 7/8] simplify and remove conversion to the sametype Export this patch

Signed-off-by: Perma Alesheikh <me@prma.dev>
---
 mml/tests/pgp-native.rs | 2 +-
 process/src/lib.rs      | 2 +-
 time/src/server/tcp.rs  | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/mml/tests/pgp-native.rs b/mml/tests/pgp-native.rs
index 3c268f3..0489b04 100644
--- a/mml/tests/pgp-native.rs
+++ b/mml/tests/pgp-native.rs
@@ -94,7 +94,7 @@ async fn spawn_fake_key_server(pkeys: HashMap<String, String>) -> String {
            secret_key: NativePgpSecretKey::Path(alice_skey_path.clone()),
            secret_key_passphrase: Secret::new_raw(""),
            public_keys_resolvers: vec![NativePgpPublicKeysResolver::KeyServers(vec![
                String::from(key_server_addr),
                key_server_addr,
            ])],
        }))
        .build(mml)
diff --git a/process/src/lib.rs b/process/src/lib.rs
index 64b1bad..5d4bdce 100644
--- a/process/src/lib.rs
+++ b/process/src/lib.rs
@@ -351,7 +351,7 @@ fn from(cmd: Vec<&str>) -> Self {

impl From<&[String]> for Pipeline {
    fn from(cmd: &[String]) -> Self {
        Self(cmd.into_iter().map(Into::into).collect())
        Self(cmd.iter().map(Into::into).collect())
    }
}

diff --git a/time/src/server/tcp.rs b/time/src/server/tcp.rs
index b5cddc0..f5639b4 100644
--- a/time/src/server/tcp.rs
+++ b/time/src/server/tcp.rs
@@ -106,7 +106,7 @@ async fn read(&mut self) -> io::Result<Request> {
impl ResponseWriter for TcpHandler {
    async fn write(&mut self, res: Response) -> io::Result<()> {
        let res = match res {
            Response::Ok => format!("ok\n"),
            Response::Ok => "ok\n".to_string(),
            Response::Timer(timer) => {
                format!("timer {}\n", serde_json::to_string(&timer).unwrap())
            }
-- 
2.43.0

[PATCH email-lib 8/8] use concise operators Export this patch

the `a -= b` and `a %= b` have the same effect as `a = a - b` and
  `a = a % b`.

Signed-off-by: Perma Alesheikh <me@prma.dev>
---
 time/src/timer.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/time/src/timer.rs b/time/src/timer.rs
index 6e1d696..cad81b4 100644
--- a/time/src/timer.rs
+++ b/time/src/timer.rs
@@ -271,14 +271,14 @@ pub async fn update(&mut self) {
                    }
                }

                elapsed = elapsed % total_duration;
                elapsed %= total_duration;

                let last_cycle = cycles[cycles.len() - 1].clone();
                let next_cycle = cycles
                    .into_iter()
                    .fold(None, |next_cycle, mut cycle| match next_cycle {
                        None if elapsed < cycle.duration => {
                            cycle.duration = cycle.duration - elapsed;
                            cycle.duration -= elapsed;
                            Some(cycle)
                        }
                        _ => next_cycle,
-- 
2.43.0