From: Matt Joiner Date: Wed, 11 Nov 2020 04:31:55 +0000 (+1100) Subject: Implement fmt.Formatter for metainfo.Hash X-Git-Tag: v1.19.0~31 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=493ee9dfd11405c7ddb81a2ad66705081ca79976;p=btrtrc.git Implement fmt.Formatter for metainfo.Hash It's so easy to make mistakes by specifying %x when printing these. --- diff --git a/metainfo/hash.go b/metainfo/hash.go index 511647ed..41ee9566 100644 --- a/metainfo/hash.go +++ b/metainfo/hash.go @@ -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[:] }