diff mbox series

[bug#63802,1/3] client: Separate serialize-email-address into a function.

Message ID 20230608171453.14788-1-arunisaac@systemreboot.net
State New
Headers show
Series [bug#63802,1/3] client: Separate serialize-email-address into a function. | expand

Commit Message

Arun Isaac June 8, 2023, 5:14 p.m. UTC
* mumi/client.scm (serialize-email-address): New function.
(reply-email-headers): Use serialize-email-address.
* tests/client.scm (serialize-email-address): New variable.
("serialize email address", "serialize email address with comma in
name"): New tests.
---
 mumi/client.scm  | 14 ++++++++++----
 tests/client.scm | 11 +++++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)

Comments

Maxim Cournoyer July 16, 2023, 3:14 a.m. UTC | #1
Hi Arun!

Arun Isaac <arunisaac@systemreboot.net> writes:

> * mumi/client.scm (serialize-email-address): New function.
> (reply-email-headers): Use serialize-email-address.
> * tests/client.scm (serialize-email-address): New variable.
> ("serialize email address", "serialize email address with comma in
> name"): New tests.

LGTM!
diff mbox series

Patch

diff --git a/mumi/client.scm b/mumi/client.scm
index 64ccbeb..c30429d 100644
--- a/mumi/client.scm
+++ b/mumi/client.scm
@@ -221,6 +221,14 @@  arguments."
            (unless (zero? (close-pipe port))
              (error "Command invocation failed" command))))))))
 
+(define (serialize-email-address name address)
+  "Combine NAME and ADDRESS into a complete email address of the form
+\"NAME <ADDRESS>\". Double quote NAME if necessary."
+  (string-append (if (string-contains name ",")
+                     (string-append "\"" name "\"")
+                     name)
+                 " <" address ">"))
+
 
 (define* (git-send-email to patches #:optional (options '()))
   "Send PATCHES using git send-email to the TO address with
@@ -269,10 +277,8 @@  ISSUE-NUMBER."
       ,@(match (delete-duplicates
                 (map (lambda (message)
                        (let ((from (assoc-ref message "from")))
-                         (string-append (if (string-contains (assoc-ref from "name") ",")
-                                            (string-append "\"" (assoc-ref from "name") "\"")
-                                            (assoc-ref from "name"))
-                                        " <" (assoc-ref from "address") ">")))
+                         (serialize-email-address (assoc-ref from "name")
+                                                  (assoc-ref from "address"))))
                      (vector->list (assoc-ref issue "messages"))))
           (() (list))
           (cc (list (cons 'cc (string-join cc ", "))))))))
diff --git a/tests/client.scm b/tests/client.scm
index 5352b08..2b2c1be 100644
--- a/tests/client.scm
+++ b/tests/client.scm
@@ -65,8 +65,19 @@  called with."
         (lambda _
           (error "Do not poll server for issue number"))))
 
+(define serialize-email-address
+  (@@ (mumi client) serialize-email-address))
+
 (test-begin "client")
 
+(test-equal "serialize email address"
+  "Foo <foo@example.com>"
+  (serialize-email-address "Foo" "foo@example.com"))
+
+(test-equal "serialize email address with comma in name"
+  "\"Bar, Foo\" <foobar@example.com>"
+  (serialize-email-address "Bar, Foo" "foobar@example.com"))
+
 (test-equal "send patches to new issue"
   '(("git" "send-email" "--to=foo@patches.com" "foo.patch")
     ("git" "send-email" "--to=12345@example.com" "bar.patch" "foobar.patch"))