]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Implement fmt.Formatter for metainfo.Hash
authorMatt Joiner <anacrolix@gmail.com>
Wed, 11 Nov 2020 04:31:55 +0000 (15:31 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 11 Nov 2020 04:31:55 +0000 (15:31 +1100)
It's so easy to make mistakes by specifying %x when printing these.

metainfo/hash.go

index 511647ed730b680ffe58c25ea48db38849fbf3cb..41ee956613de131b5f6c3280f79992c7174e7ae8 100644 (file)
@@ -11,6 +11,14 @@ const HashSize = 20
 // 20-byte SHA1 hash used for info and pieces.
 type Hash [HashSize]byte
 
+var _ fmt.Formatter = (*Hash)(nil)
+
+func (h Hash) Format(f fmt.State, c rune) {
+       // TODO: I can't figure out a nice way to just override the 'x' rune, since it's meaningless
+       // with the "default" 'v', or .String() already returning the hex.
+       f.Write([]byte(h.HexString()))
+}
+
 func (h Hash) Bytes() []byte {
        return h[:]
 }