|
| 1 | +{-# LANGUAGE CPP #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +{-# LANGUAGE FlexibleContexts #-} |
| 4 | +{-# LANGUAGE FlexibleInstances #-} |
| 5 | +{-# LANGUAGE LambdaCase #-} |
| 6 | +{-# LANGUAGE StandaloneDeriving #-} |
| 7 | +{-# LANGUAGE UndecidableInstances #-} |
| 8 | + |
| 9 | +module Data.Patch.PatchOrReplacement where |
| 10 | + |
| 11 | +import Data.Patch |
| 12 | +#if !MIN_VERSION_base(4,11,0) |
| 13 | +import Data.Semigroup (Semigroup (..)) |
| 14 | +#endif |
| 15 | +import GHC.Generics |
| 16 | + |
| 17 | +-- | Like SemiMap/PartialMap but for anything patchable |
| 18 | +data PatchOrReplacement p |
| 19 | + = PatchOrReplacement_Patch p |
| 20 | + | PatchOrReplacement_Complete (PatchTarget p) |
| 21 | + deriving (Generic) |
| 22 | + |
| 23 | +deriving instance (Eq p, Eq (PatchTarget p)) => Eq (PatchOrReplacement p) |
| 24 | +deriving instance (Ord p, Ord (PatchTarget p)) => Ord (PatchOrReplacement p) |
| 25 | +deriving instance (Show p, Show (PatchTarget p)) => Show (PatchOrReplacement p) |
| 26 | +deriving instance (Read p, Read (PatchTarget p)) => Read (PatchOrReplacement p) |
| 27 | + |
| 28 | +completePatchOrReplacement :: PatchOrReplacement p -> Maybe (PatchTarget p) |
| 29 | +completePatchOrReplacement = \case |
| 30 | + PatchOrReplacement_Complete t -> Just t |
| 31 | + PatchOrReplacement_Patch _ -> Nothing |
| 32 | + |
| 33 | +instance ( Monoid p |
| 34 | +#if !MIN_VERSION_base(4,11,0) |
| 35 | + , Semigroup p |
| 36 | +#endif |
| 37 | + , Patch p |
| 38 | + ) => Monoid (PatchOrReplacement p) where |
| 39 | + mempty = PatchOrReplacement_Patch mempty |
| 40 | + mappend = (<>) |
| 41 | + |
| 42 | +instance (Semigroup p, Patch p) => Semigroup (PatchOrReplacement p) where |
| 43 | + (<>) = curry $ \case |
| 44 | + (PatchOrReplacement_Patch a, PatchOrReplacement_Patch b) -> PatchOrReplacement_Patch $ a <> b |
| 45 | + (PatchOrReplacement_Patch a, PatchOrReplacement_Complete b) -> PatchOrReplacement_Complete $ applyAlways a b |
| 46 | + (PatchOrReplacement_Complete a, _) -> PatchOrReplacement_Complete a |
0 commit comments