factory.go

 1package backend
 2
 3import (
 4	"fmt"
 5
 6	"github.com/floatpane/matcha/config"
 7)
 8
 9// NewFunc is the constructor signature for backend providers.
10type NewFunc func(account *config.Account) (Provider, error)
11
12var registry = map[string]NewFunc{}
13
14// RegisterBackend registers a backend constructor for a protocol name.
15func RegisterBackend(protocol string, fn NewFunc) {
16	registry[protocol] = fn
17}
18
19// New creates a Provider for the given account based on its Protocol field.
20// An empty protocol defaults to "imap".
21func New(account *config.Account) (Provider, error) {
22	protocol := account.Protocol
23	if protocol == "" {
24		protocol = "imap"
25	}
26	fn, ok := registry[protocol]
27	if !ok {
28		return nil, fmt.Errorf("unknown email protocol: %q", protocol)
29	}
30	return fn(account)
31}