diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-30 02:36:25 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-08-17 22:50:57 -0600 |
commit | 13b65d51ffc9768464e8330fdb73b6febd7410af (patch) | |
tree | 271fe78abc75573a330353632171284ccf2ecd47 /lib/btrfs/btrfssum | |
parent | eb4e771e2a99edeb5cc3a1511ab611480f98255d (diff) |
json: Implement a bunch of streaming JSON encoding/decoding
Diffstat (limited to 'lib/btrfs/btrfssum')
-rw-r--r-- | lib/btrfs/btrfssum/csum.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/btrfs/btrfssum/csum.go b/lib/btrfs/btrfssum/csum.go index 8f9ac1a..c7c1f37 100644 --- a/lib/btrfs/btrfssum/csum.go +++ b/lib/btrfs/btrfssum/csum.go @@ -5,6 +5,7 @@ package btrfssum import ( + "encoding" "encoding/binary" "encoding/hex" "fmt" @@ -15,10 +16,29 @@ import ( type CSum [0x20]byte +var ( + _ fmt.Stringer = CSum{} + _ fmt.Formatter = CSum{} + _ encoding.TextMarshaler = CSum{} + _ encoding.TextUnmarshaler = (*CSum)(nil) +) + func (csum CSum) String() string { return hex.EncodeToString(csum[:]) } +func (csum CSum) MarshalText() ([]byte, error) { + var ret [len(csum) * 2]byte + hex.Encode(ret[:], csum[:]) + return ret[:], nil +} + +func (csum *CSum) UnmarshalText(text []byte) error { + *csum = CSum{} + _, err := hex.Decode(csum[:], text) + return err +} + func (csum CSum) Fmt(typ CSumType) string { return hex.EncodeToString(csum[:typ.Size()]) } |