Skip to content

Commit abc57f5

Browse files
committed
core: Add Eq and Ord for SomeNamedDigest
1 parent ef9fb46 commit abc57f5

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

hnix-store-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* `BuildResult`s `timesBuild` field changes type from `Integer` to `Int` [#231](https://github.com/haskell-nix/hnix-store/pull/231)
99

1010
* Additions:
11+
* Added `Eq` and `Ord` instances for `SomeNamedDigest` [#231](https://github.com/haskell-nix/hnix-store/pull/231)
1112
* `BuildStatus` grows `NoSubstituters` and `ResolvesToAlreadyValid` constructors [#231](https://github.com/haskell-nix/hnix-store/pull/231)
1213
* `InvalidPathError` replacing previous stringy error [#231](https://github.com/haskell-nix/hnix-store/pull/231)
1314
* Added `Arbitrary` instances for (exported by default) [#230](https://github.com/haskell-nix/hnix-store/pull/230)

hnix-store-core/src/System/Nix/Internal/Hash.hs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module System.Nix.Internal.Hash
1919
)
2020
where
2121

22+
import Crypto.Hash (Digest)
2223
import qualified Text.Show
2324
import qualified Crypto.Hash as C
2425
import qualified Data.ByteString as BS
@@ -45,11 +46,23 @@ instance NamedAlgo C.SHA512 where
4546
algoName = "sha512"
4647

4748
-- | A digest whose 'NamedAlgo' is not known at compile time.
48-
data SomeNamedDigest = forall a . NamedAlgo a => SomeDigest (C.Digest a)
49+
data SomeNamedDigest = forall a . NamedAlgo a => SomeDigest (Digest a)
4950

5051
instance Show SomeNamedDigest where
5152
show sd = case sd of
52-
SomeDigest (digest :: C.Digest hashType) -> toString $ "SomeDigest " <> algoName @hashType <> ":" <> encodeDigestWith NixBase32 digest
53+
SomeDigest (digest :: Digest hashType) -> toString $ "SomeDigest " <> algoName @hashType <> ":" <> encodeDigestWith NixBase32 digest
54+
55+
instance Eq SomeNamedDigest where
56+
(==) (SomeDigest (a :: Digest aType))
57+
(SomeDigest (b :: Digest bType))
58+
= algoName @aType == algoName @bType
59+
&& encodeDigestWith NixBase32 a == encodeDigestWith NixBase32 b
60+
61+
instance Ord SomeNamedDigest where
62+
(<=) (SomeDigest (a :: Digest aType))
63+
(SomeDigest (b :: Digest bType))
64+
= algoName @aType <= algoName @bType
65+
&& encodeDigestWith NixBase32 a <= encodeDigestWith NixBase32 b
5366

5467
mkNamedDigest :: Text -> Text -> Either String SomeNamedDigest
5568
mkNamedDigest name sriHash =

0 commit comments

Comments
 (0)