aboutsummaryrefslogtreecommitdiff
path: root/contact.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-03-06 02:44:40 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-03-06 02:44:40 +0100
commitc9ef855f5a3a46c8ec8106cf9b62fd12fc03268b (patch)
treee6caed5d7187208f666f7f7b1078f743e3a74bc5 /contact.go
parent0a76591ecf52cd7791a96d3f1b93dea6d1623702 (diff)
Refactor contacts
Diffstat (limited to 'contact.go')
-rw-r--r--contact.go40
1 files changed, 18 insertions, 22 deletions
diff --git a/contact.go b/contact.go
index 11061b9..71b4ecd 100644
--- a/contact.go
+++ b/contact.go
@@ -14,18 +14,6 @@ type Contact interface {
encoding.TextMarshaler
}
-type Mail string
-
-func (m Mail) MarshalText() ([]byte, error) {
- return []byte("mailto:" + m), nil
-}
-
-type Phone string
-
-func (ph Phone) MarshalText() ([]byte, error) {
- return []byte("tel:" + ph), nil
-}
-
type Contacts []Contact
func (c *Contacts) UnmarshalJSON(b []byte) error {
@@ -48,18 +36,26 @@ func (c *Contacts) UnmarshalJSON(b []byte) error {
return nil
}
-func (c *Contacts) Mail(email string) error {
+type Mail string
+
+func (m Mail) MarshalText() ([]byte, error) {
+ return []byte("mailto:" + m), nil
+}
+
+type Phone string
+
+func (ph Phone) MarshalText() ([]byte, error) {
+ return []byte("tel:" + ph), nil
+}
+
+func NewContacts(email, phone string) (Contacts, error) {
m, err := mail.ParseAddress(email)
if err != nil {
- return err
+ return nil, err
}
- *c = append(*c, Mail(m.Address))
- return nil
-}
-
-func (c *Contacts) Phone(phone string) error {
- if ph := strings.TrimSpace(phone); ph != "" {
- *c = append(*c, Phone(ph))
+ c := Contacts{Mail(m.Address)}
+ if phone != "" {
+ c = append(c, Phone(phone))
}
- return nil
+ return c, nil
}