1
1
mirror of https://github.com/wader/fq.git synced 2024-12-12 02:22:44 +03:00
fq/format/bits/bits.go
Mattias Wadman b08ef00dd1 decode,interp: Refactor format groups into a proper struct
Replaces []Format with a Group type.
A bit more type safe.
Breaking change for RegisterFormat, now takes a first argument that is a "single" format group.
Lots of naming cleanup.

This is also preparation for decode group argument which will enable doing intresting
probing, ex a format decoder could know it's decode as part of probe group  (html could
be probed possibly), or have "arg probe" group for decoder who inspect args to know
if they should probe (-d /path/to/schema etc) to enable nice CLI-ergonomics.
2023-04-29 20:02:34 +02:00

44 lines
939 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 {
return func(d *decode.D) 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(
format.Bits,
&decode.Format{
Description: "Raw bits",
DecodeFn: decodeBits(1),
SkipDecodeFunction: true, // skip add bits and frombits function
})
interp.RegisterFormat(
format.Bytes,
&decode.Format{
Description: "Raw bytes",
DecodeFn: decodeBits(8),
SkipDecodeFunction: true, // skip add bytes and frombytes function
})
interp.RegisterFS(bitsFS)
}