2015-07-09 20:20:30 +00:00
|
|
|
{- git-annex deferred parse values
|
|
|
|
-
|
2021-04-06 19:14:00 +00:00
|
|
|
- Copyright 2015-2021 Joey Hess <id@joeyh.name>
|
2015-07-09 20:20:30 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2015-07-09 20:20:30 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
|
|
|
|
module Types.DeferredParse where
|
|
|
|
|
|
|
|
import Annex
|
|
|
|
|
2015-07-10 04:55:53 +00:00
|
|
|
import Options.Applicative
|
2021-04-06 19:14:00 +00:00
|
|
|
import qualified Data.Semigroup as Sem
|
|
|
|
import Prelude
|
2015-07-09 23:03:21 +00:00
|
|
|
|
2015-07-09 20:20:30 +00:00
|
|
|
-- Some values cannot be fully parsed without performing an action.
|
|
|
|
-- The action may be expensive, so it's best to call finishParse on such a
|
|
|
|
-- value before using getParsed repeatedly.
|
|
|
|
data DeferredParse a = DeferredParse (Annex a) | ReadyParse a
|
|
|
|
|
|
|
|
class DeferredParseClass a where
|
|
|
|
finishParse :: a -> Annex a
|
|
|
|
|
|
|
|
getParsed :: DeferredParse a -> Annex a
|
|
|
|
getParsed (DeferredParse a) = a
|
|
|
|
getParsed (ReadyParse a) = pure a
|
|
|
|
|
|
|
|
instance DeferredParseClass (DeferredParse a) where
|
|
|
|
finishParse (DeferredParse a) = ReadyParse <$> a
|
|
|
|
finishParse (ReadyParse a) = pure (ReadyParse a)
|
|
|
|
|
|
|
|
instance DeferredParseClass (Maybe (DeferredParse a)) where
|
|
|
|
finishParse Nothing = pure Nothing
|
|
|
|
finishParse (Just v) = Just <$> finishParse v
|
2015-07-09 23:03:21 +00:00
|
|
|
|
|
|
|
instance DeferredParseClass [DeferredParse a] where
|
|
|
|
finishParse v = mapM finishParse v
|
|
|
|
|
2022-06-29 17:28:08 +00:00
|
|
|
type AnnexOption = Parser AnnexSetter
|
2021-04-06 19:14:00 +00:00
|
|
|
|
2022-06-29 17:28:08 +00:00
|
|
|
-- Used for options that can modify Annex state by running
|
2021-04-06 19:14:00 +00:00
|
|
|
-- an arbitrary action in it, and can also set up AnnexRead.
|
2022-06-29 17:28:08 +00:00
|
|
|
data AnnexSetter = AnnexSetter
|
2021-04-06 19:14:00 +00:00
|
|
|
{ annexStateSetter :: Annex ()
|
|
|
|
, annexReadSetter :: AnnexRead -> AnnexRead
|
|
|
|
}
|
|
|
|
|
2022-06-29 17:28:08 +00:00
|
|
|
instance Sem.Semigroup AnnexSetter where
|
|
|
|
a <> b = AnnexSetter
|
2021-04-06 19:14:00 +00:00
|
|
|
{ annexStateSetter = annexStateSetter a >> annexStateSetter b
|
|
|
|
, annexReadSetter = annexReadSetter b . annexReadSetter a
|
|
|
|
}
|
|
|
|
|
2022-06-29 17:28:08 +00:00
|
|
|
instance Monoid AnnexSetter where
|
|
|
|
mempty = AnnexSetter (return ()) id
|