1 package envelopes
2 3 import (
4 "io"
5 )
6 7 // Marshaller is a function signature the same as the codec.JSON Marshal but
8 // without the requirement of there being a full implementation or declared
9 // receiver variable of this interface. Used here to encapsulate one or more
10 // other data structures into an envelope.
11 type Marshaller func(dst []byte) (b []byte)
12 13 // Marshal is a parser for dynamic typed arrays like nostr codec.Envelope
14 // types.
15 func Marshal(dst []byte, label string, m Marshaller) (b []byte) {
16 b = dst
17 b = append(b, '[', '"')
18 b = append(b, label...)
19 b = append(b, '"', ',')
20 b = m(b)
21 b = append(b, ']')
22 return
23 }
24 25 // SkipToTheEnd scans forward after all fields in an envelope have been read to
26 // find the closing bracket.
27 func SkipToTheEnd(dst []byte) (rem []byte, err error) {
28 if len(dst) == 0 {
29 return
30 }
31 rem = dst
32 // we have everything, just need to snip the end
33 for ; len(rem) > 0; rem = rem[1:] {
34 if rem[0] == ']' {
35 rem = rem[:0]
36 return
37 }
38 }
39 err = io.EOF
40 return
41 }
42