summaryrefslogtreecommitdiff
path: root/src/Propellor/EnsureProperty.hs
blob: 9f3d0a191c9c60e4f2935865111156561042a8ec (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

module Propellor.EnsureProperty
	( ensureProperty
	, property'
	, OuterMetaTypesWitness
	, EnsurePropertyAllowed
	) where

import Propellor.Types
import Propellor.Types.Core
import Propellor.Types.MetaTypes
import Propellor.Exception

import GHC.TypeLits
import GHC.Exts (Constraint)
import Data.Type.Bool
import Data.Monoid
import Prelude

-- | For when code running in the Propellor monad needs to ensure a
-- Property.
--
-- Use `property'` to get the `OuterMetaTypesWithness`. For example:
--
-- > foo = Property Debian
-- > foo = property' "my property" $ \w -> do
-- > 	ensureProperty w (aptInstall "foo")
--
-- The type checker will prevent using ensureProperty with a property
-- that does not support the target OSes needed by the OuterMetaTypesWitness.
-- In the example above, aptInstall must support Debian, since foo
-- is supposed to support Debian.
--
-- The type checker will also prevent using ensureProperty with a property
-- with HasInfo in its MetaTypes. Doing so would cause the `Info` associated
-- with the property to be lost.
ensureProperty
	::
		-- -Wredundant-constraints is turned off because
		-- this constraint appears redundant, but is actually
		-- crucial.
		( EnsurePropertyAllowed inner outer)
	=> OuterMetaTypesWitness outer
	-> Property (MetaTypes inner)
	-> Propellor Result
ensureProperty _ = maybe (return NoChange) catchPropellor . getSatisfy

type family EnsurePropertyAllowed inner outer :: Constraint where
	EnsurePropertyAllowed inner outer = 'True ~
		((EnsurePropertyNoInfo inner)
			&&
		(EnsurePropertyTargetOSMatches inner outer))

type family EnsurePropertyNoInfo (l :: [a]) :: Bool where
	EnsurePropertyNoInfo '[] = 'True
	EnsurePropertyNoInfo (t ': ts) = If (Not (t `EqT` 'WithInfo))
		(EnsurePropertyNoInfo ts)
		(TypeError ('Text "Cannot use ensureProperty with a Property that HasInfo."))

type family EnsurePropertyTargetOSMatches inner outer where
	EnsurePropertyTargetOSMatches inner outer = 
		If (Targets outer `IsSubset` Targets inner)
			'True
			(IfStuck (Targets outer)
				(DelayError
					('Text "ensureProperty outer Property type is not able to be inferred here."
					 ':$$: 'Text "Consider adding a type annotation."
					)
				)
				(DelayErrorFcf
					('Text "ensureProperty inner Property is missing support for: "
					 ':$$: PrettyPrintMetaTypes (Difference (Targets outer) (Targets inner))
					)
				)
			)

-- | Constructs a property, like `property`, but provides its
-- `OuterMetaTypesWitness`.
property'
	:: SingI metatypes
	=> Desc
	-> (OuterMetaTypesWitness metatypes -> Propellor Result)
	-> Property (MetaTypes metatypes)
property' d a =
	let p = Property sing d (Just (a (outerMetaTypesWitness p))) mempty mempty
	in p

-- | Used to provide the metatypes of a Property to calls to 
-- 'ensureProperty` within it.
newtype OuterMetaTypesWitness metatypes = OuterMetaTypesWitness (MetaTypes metatypes)

outerMetaTypesWitness :: Property (MetaTypes l) -> OuterMetaTypesWitness l
outerMetaTypesWitness (Property metatypes _ _ _ _) = OuterMetaTypesWitness metatypes