summaryrefslogtreecommitdiff
path: root/src/Hakyll/Alias.hs
blob: b127a058553d85ed44351361c41d0e14ad2b9588 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Hakyll.Alias where

import Prolog
import Data.HashSet qualified as HS


newtype Aliased a x = Aliased { unAliased :: Either a x }
    deriving newtype (Binary, Eq, Foldable, Functor, Generic, Generic1, Hashable, Ord, Show, Traversable)

class (Eq a, Hashable a) => ToAliases x a where
    toAliases :: x -> HashSet a

instance Hashable a => ToAliases a a where
    toAliases = HM.singleton

instance (Eq a, Hashable a) => ToAliases (HashSet a) a where
    toAliases = id

instance ToAliases x a => ToAliases [x] a where
    toAliases = foldMap toAliases


class FromAlias a x where
    fromAlias :: MonadThrow m => a -> m x

instance FromAlias a a where
    fromAlias = pure

instance (FromAlias a x, FromAlias b x) => FromAlias (Either a b) x where
    fromAlias = either fromAlias fromAlias

instance FromAlias a x => FromAlias (Aliased a x) x where
    fromAlias = fromAlias . unAliased

newtype NoAlias a =NoAlias a
    deriving (Eq, Show)

instance Show a => Exception (NoAlias a) where
    displayException (NoAlias a) = show a <> " is not an alias"

missingAlias :: MonadThrow m => a -> m b
missingAlias = throwM . NoAlias