diff --git a/option.go b/option.go index bb00187..80fd97d 100644 --- a/option.go +++ b/option.go @@ -19,18 +19,37 @@ func WithRateLimit(ratelimit time.Duration) Option { // WithBPSOpt is a functional option to set the bps index for {VbanTxt}.packet. func WithBPSOpt(bps int) Option { return func(vt *VbanTxt) { + defaultBps := BpsOpts[vt.packet.bpsIndex] + bpsIndex := indexOf(BpsOpts, bps) if bpsIndex == -1 { - log.Warnf("invalid bps value %d, expected one of %v, defaulting to 0", bps, BpsOpts) + log.Warnf( + "invalid bps value %d, expected one of %v, defaulting to %d", + bps, + BpsOpts, + defaultBps, + ) return } - vt.packet.bpsIndex = bpsIndex + if bpsIndex > 255 { + log.Warnf("bps index %d too large for uint8, defaulting to %d", bpsIndex, defaultBps) + return + } + vt.packet.bpsIndex = uint8(bpsIndex) } } // WithChannel is a functional option to set the channel for {VbanTxt}.packet. func WithChannel(channel int) Option { return func(vt *VbanTxt) { - vt.packet.channel = channel + if channel < 0 || channel > 255 { + log.Warnf( + "channel value %d out of range [0,255], defaulting to %d", + channel, + vt.packet.channel, + ) + return + } + vt.packet.channel = uint8(channel) } }