Merge codec and tape packages

This commit is contained in:
Sasha Koshka 2025-06-20 15:55:37 -04:00
parent ce503c4689
commit 285e83d995
7 changed files with 27 additions and 17 deletions

View File

@ -6,12 +6,11 @@ import "maps"
import "math" import "math"
import "slices" import "slices"
import "strings" import "strings"
import "git.tebibyte.media/sashakoshka/hopp/codec" import "git.tebibyte.media/sashakoshka/hopp/tape"
const imports = const imports =
` `
import "git.teibibyte.media/sashakoshka/hopp/tape" import "git.teibibyte.media/sashakoshka/hopp/tape"
import "git.teibibyte.media/sashakoshka/hopp/codec"
` `
const preamble = ` const preamble = `
@ -31,8 +30,8 @@ type Table map[uint16] any
// Message is any message that can be sent along this protocol. // Message is any message that can be sent along this protocol.
type Message interface { type Message interface {
codec.Encodable tape.Encodable
codec.Decodable tape.Decodable
// Method returns the method code of the message. // Method returns the method code of the message.
Method() uint16 Method() uint16
@ -122,7 +121,7 @@ func (this *Generator) generateTypedef(name string, typ Type) (n int, err error)
"specified by the tag, if possible.\n") "specified by the tag, if possible.\n")
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
nn, err = this.iprintf( nn, err = this.iprintf(
"func (this *%s) EncodeValue(encoder *codec.Encoder, tag tape.Tag) (n int, err error) {\n", "func (this *%s) EncodeValue(encoder *tape.Encoder, tag tape.Tag) (n int, err error) {\n",
name) name)
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
this.push() this.push()
@ -136,7 +135,21 @@ func (this *Generator) generateTypedef(name string, typ Type) (n int, err error)
nn, err = this.iprintf("}\n") nn, err = this.iprintf("}\n")
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
// TODO DecodeValue method // DecodeValue method
nn, err = this.iprintf(
"\n // DecodeValue decodes the value of this type without " +
"the tag. The value is\n// decoded according to the " +
"parameters specified by the tag, if possible.\n")
n += nn; if err != nil { return n, err }
nn, err = this.iprintf(
"func (this *%s) DecodeValue(decoder *tape.Decoder, tag tape.Tag) (n int, err error) {\n",
name)
n += nn; if err != nil { return n, err }
this.push()
// TODO
this.pop()
nn, err = this.iprintf("}\n")
n += nn; if err != nil { return n, err }
return n, nil return n, nil
} }
@ -156,7 +169,7 @@ func (this *Generator) generateMessage(method uint16, message Message) (n int, e
nn, err = this.iprintf("\n// Encode encodes this message's tag and value.\n") nn, err = this.iprintf("\n// Encode encodes this message's tag and value.\n")
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
nn, err = this.iprintf( nn, err = this.iprintf(
"func(this %s) Encode(encoder *codec.Encoder) (n int, err error) {\n", "func(this %s) Encode(encoder *tape.Encoder) (n int, err error) {\n",
this.resolveMessageName(message.Name)) this.resolveMessageName(message.Name))
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
this.push() this.push()
@ -378,7 +391,7 @@ func (this *Generator) generateTag(typ Type, source string) (n int, err error) {
nn, err := this.printf("tape.TagKTV.WithCN(tape.IntBytes(uint64(len(%s))))", source) nn, err := this.printf("tape.TagKTV.WithCN(tape.IntBytes(uint64(len(%s))))", source)
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
case TypeTableDefined: case TypeTableDefined:
nn, err := this.printf("tape.TagKTV.WithCN(%d)", codec.IntBytes(uint64(len(typ.Fields)))) nn, err := this.printf("tape.TagKTV.WithCN(%d)", tape.IntBytes(uint64(len(typ.Fields))))
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
case TypeNamed: case TypeNamed:
resolved, err := this.resolveTypeName(typ.Name) resolved, err := this.resolveTypeName(typ.Name)

View File

@ -1,4 +1,4 @@
package codec package tape
import "io" import "io"

View File

@ -2,7 +2,6 @@ package tape
import "fmt" import "fmt"
import "reflect" import "reflect"
import "git.tebibyte.media/sashakoshka/hopp/codec"
// EncodeAny encodes an "any" value. Returns an error if the underlying type is // EncodeAny encodes an "any" value. Returns an error if the underlying type is
// unsupported. Supported types are: // unsupported. Supported types are:
@ -14,7 +13,7 @@ import "git.tebibyte.media/sashakoshka/hopp/codec"
// - string // - string
// - []<supported type> // - []<supported type>
// - map[uint16]<supported type> // - map[uint16]<supported type>
func EncodeAny(encoder *codec.Encoder, value any) (Tag, error) { func EncodeAny(encoder *Encoder, value any) (Tag, error) {
// TODO // TODO
} }

View File

@ -1,4 +1,4 @@
package codec package tape
import "io" import "io"

View File

@ -1,4 +1,4 @@
package codec package tape
// GBEUSize returns the size (in octets) of a GBEU integer. // GBEUSize returns the size (in octets) of a GBEU integer.
func GBEUSize(value uint64) int { func GBEUSize(value uint64) int {

View File

@ -1,4 +1,4 @@
package codec package tape
import "testing" import "testing"

View File

@ -1,7 +1,5 @@
package tape package tape
import "git.tebibyte.media/sashakoshka/hopp/codec"
type Tag byte; const ( type Tag byte; const (
SI Tag = 0 << 5 // Small integer SI Tag = 0 << 5 // Small integer
LI Tag = 1 << 5 // Large integer LI Tag = 1 << 5 // Large integer
@ -40,6 +38,6 @@ func bufferLenTag(length int) Tag {
if length < int(CNLimit) { if length < int(CNLimit) {
return SBA.WithCN(length) return SBA.WithCN(length)
} else { } else {
return LBA.WithCN(codec.IntBytes(uint64(length))) return LBA.WithCN(IntBytes(uint64(length)))
} }
} }