]> Sergey Matveev's repositories - btrtrc.git/blobdiff - metainfo/hash.go
Reinstate cmd/announce announce subcommand
[btrtrc.git] / metainfo / hash.go
index 41ee956613de131b5f6c3280f79992c7174e7ae8..b2bab751b6564927590562c3ab28a2fb3f5e37f7 100644 (file)
@@ -2,6 +2,7 @@ package metainfo
 
 import (
        "crypto/sha1"
+       "encoding"
        "encoding/hex"
        "fmt"
 )
@@ -11,7 +12,10 @@ const HashSize = 20
 // 20-byte SHA1 hash used for info and pieces.
 type Hash [HashSize]byte
 
-var _ fmt.Formatter = (*Hash)(nil)
+var (
+       _ fmt.Formatter            = (*Hash)(nil)
+       _ encoding.TextUnmarshaler = (*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
@@ -50,6 +54,10 @@ func (h *Hash) FromHexString(s string) (err error) {
        return
 }
 
+func (h *Hash) UnmarshalText(b []byte) error {
+       return h.FromHexString(string(b))
+}
+
 func NewHashFromHex(s string) (h Hash) {
        err := h.FromHexString(s)
        if err != nil {