1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 13:22:58 +03:00
fq/format/bits/bits.go
Mattias Wadman 9b81d4d3ab decode: More type safe API and split scalar into multiple types
Preparation to make decoder use less memory and API more type safe.
Now each scalar type has it's own struct type so it can store different
things and enables to have a scalar interface.
Also own types will enable experimenting with decode DLS designs like
using chained methods that are type aware.
2022-12-14 16:23:58 +01:00

42 lines
977 B
Go

package bits
import (
"embed"
"github.com/wader/fq/format"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
)
//go:embed bits.md
//go:embed bytes.md
var bitsFS embed.FS
func decodeBits(unit int) func(d *decode.D, _ any) any {
return func(d *decode.D, _ any) any {
var s scalar.Any
b, _ := interp.NewBinaryFromBitReader(d.BitBufRange(0, d.Len()), unit, 0)
s.Actual = b
d.Value.V = &s
d.Value.Range.Len = d.Len()
return nil
}
}
func init() {
interp.RegisterFormat(decode.Format{
Name: format.BITS,
Description: "Raw bits",
DecodeFn: decodeBits(1),
SkipDecodeFunction: true, // skip add bits and frombits function
})
interp.RegisterFormat(decode.Format{
Name: format.BYTES,
Description: "Raw bytes",
DecodeFn: decodeBits(8),
SkipDecodeFunction: true, // skip add bytes and frombytes function
})
interp.RegisterFS(bitsFS)
}