-
Notifications
You must be signed in to change notification settings - Fork 5
/
acct.go
48 lines (37 loc) · 895 Bytes
/
acct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package acct
import (
"net/url"
"strings"
"github.com/pkg/errors"
)
type URL struct {
User string `json:"user"`
Host string `json:"host"`
}
func (a *URL) URL() *url.URL {
return &url.URL{
Scheme: "acct",
Opaque: a.User + "@" + a.Host,
}
}
func (a *URL) String() string {
return a.URL().String()
}
func FromString(s string) (*URL, error) {
s = strings.TrimPrefix(strings.TrimSpace(s), "@")
if !strings.HasPrefix(s, "acct:") {
s = "acct:" + s
}
u, err := url.Parse(s)
if err != nil {
return nil, errors.Wrap(err, "FromString")
}
if u.Scheme != "acct" {
return nil, errors.Errorf("FromString: invalid scheme; expected acct but got %q", u.Scheme)
}
bits := strings.Split(u.Opaque, "@")
if len(bits) != 2 {
return nil, errors.Errorf("FromString: expected two strings separated by @ but got %d", len(bits))
}
return &URL{User: bits[0], Host: bits[1]}, nil
}