diff options
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()]) } |