go-emlid/reach/client/options.go

76 lines
1.7 KiB
Go

package client
import (
"log/slog"
"time"
"forge.cadoles.com/cadoles/go-emlid/reach/client/logger"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
v1 "forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/v1"
v2 "forge.cadoles.com/cadoles/go-emlid/reach/client/protocol/v2"
)
type Options struct {
Protocols *protocol.Registry
PreferredProtocol protocol.Identifier
FallbackProtocol protocol.Identifier
AvailableTimeout time.Duration
Logger logger.Logger
Dial protocol.DialFunc
}
type OptionFunc func(opts *Options)
func NewOptions(funcs ...OptionFunc) *Options {
opts := &Options{
PreferredProtocol: v2.Identifier,
FallbackProtocol: v1.Identifier,
Protocols: protocol.DefaultRegistry(),
AvailableTimeout: 5 * time.Second,
Logger: slog.Default(),
Dial: protocol.DefaultDialFunc,
}
for _, fn := range funcs {
fn(opts)
}
return opts
}
func WithPreferredProtocol(identifier protocol.Identifier) OptionFunc {
return func(opts *Options) {
opts.PreferredProtocol = identifier
}
}
func WithFallbackProtocol(identifier protocol.Identifier) OptionFunc {
return func(opts *Options) {
opts.FallbackProtocol = identifier
}
}
func WithProtocols(protocols *protocol.Registry) OptionFunc {
return func(opts *Options) {
opts.Protocols = protocols
}
}
func WithLogger(logger logger.Logger) OptionFunc {
return func(opts *Options) {
opts.Logger = logger
}
}
func WithAvailableTimeout(timeout time.Duration) OptionFunc {
return func(opts *Options) {
opts.AvailableTimeout = timeout
}
}
func WithDial(dial protocol.DialFunc) OptionFunc {
return func(opts *Options) {
opts.Dial = dial
}
}