1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-12-26 22:16:54 +00:00
cchat-discord/internal/config/boolstamp.go

40 lines
571 B
Go

package config
import (
"strconv"
"time"
"github.com/pkg/errors"
)
type boolStamp struct {
stamp time.Duration
value bool
}
var _ customType = (*boolStamp)(nil)
func (bs boolStamp) Marshal() string {
if bs.stamp > 0 {
return bs.stamp.String()
}
return strconv.FormatBool(bs.value)
}
func (bs *boolStamp) Unmarshal(v string) error {
t, err := time.ParseDuration(v)
if err == nil && t > 0 {
bs.stamp = t
return nil
}
b, err := strconv.ParseBool(v)
if err == nil {
bs.value = b
return nil
}
return errors.New("invalid bool or timestamp")
}