This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTypes.hs
More file actions
173 lines (128 loc) · 5.3 KB
/
Types.hs
File metadata and controls
173 lines (128 loc) · 5.3 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
{-# LANGUAGE TemplateHaskell #-}
module NStack.Module.Types (module NStack.Module.Types) where
import Control.Lens (Lens', iso, Iso') -- from: lens
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as Base16
import Data.Coerce (coerce)
import Data.Monoid ((<>))
import Data.SafeCopy (base, deriveSafeCopy, SafeCopy(..), safePut, safeGet, contain)
import Data.Serialize (Serialize(..))
import Data.String (IsString)
import Data.Text (Text) -- from: text
import qualified Data.Text as T -- from: text
import Data.Aeson (ToJSON(..), FromJSON(..), ToJSONKey, FromJSONKey)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Data.Typeable (Typeable)
import Data.Data (Data(..))
import Data.UUID (UUID)
import GHC.Generics (Generic)
import Text.PrettyPrint.Mainland (Pretty, ppr, text, commasep, char, integer) -- from: mainland-pretty
import Text.Printf (printf)
import NStack.Module.Name (ModuleRef, mkNStackModuleRef, NSUri(..))
import NStack.Module.Version (ExactRelease(..), SemVer(..))
import NStack.SafeCopyOrphans ()
import NStack.UUIDOrphans ()
import NStack.Prelude.Text (putText, getText, pprS)
type APIVersion = Integer
data Language = Python | Python2 | NodeJS | Haskell | R
deriving (Show, Eq, Ord, Generic)
data Stack = Stack Language APIVersion
deriving (Show, Eq, Ord, Generic)
instance Serialize Language
$(deriveSafeCopy 0 'base ''Language)
instance Serialize Stack
$(deriveSafeCopy 0 'base ''Stack)
instance Pretty Language where
ppr = text . show
instance Pretty Stack where
ppr (Stack l v) = ppr l <> char '-' <> integer v
newtype Author = Author Text
instance Serialize Author where
put = coerce putText
get = coerce getText
type FedoraVersion = Integer
type FedoraSnapshot = Integer
-- | Make an NStack module name used internally for base images by the system
mkBaseModuleName :: Text -> APIVersion -> FedoraVersion -> FedoraSnapshot -> ModuleRef
mkBaseModuleName s v majS minS = mkNStackModuleRef (NSUri ["NStack", s]) (SemVer majS minS v Release)
-- | The name of an NStack function
newtype FnName = FnName Text
deriving (Eq, Ord, Typeable, IsString, Pretty, Generic, ToJSON, FromJSON, ToJSONKey, FromJSONKey, Data)
instance Show FnName where
show = coerce T.unpack
instance Serialize FnName where
put = coerce putText
get = coerce getText
$(deriveSafeCopy 0 'base ''FnName)
-- | The name of an NStack type
newtype TyName = TyName Text
deriving (Eq, Ord, Typeable, Data, IsString, Pretty, Generic, ToJSON, FromJSON, ToJSONKey, FromJSONKey)
instance Show TyName where
show = coerce T.unpack
instance Serialize TyName where
put = coerce putText
get = coerce getText
$(deriveSafeCopy 0 'base ''TyName)
-- | A fully-qualified function or type name
data Qualified a = Qualified
{ _modName :: ModuleRef
, _unqualName :: a
}
deriving (Eq, Ord, Typeable, Generic, Data)
type QFnName = Qualified FnName
type QTyName = Qualified TyName
instance SafeCopy QFnName where
putCopy (Qualified mod' fn) = contain $ safePut mod' >> safePut fn
getCopy = contain $ Qualified <$> safeGet <*> safeGet
instance Show a => Show (Qualified a) where
show (Qualified modName methName) = pprS modName <> "." <> show methName
instance Show a => Pretty (Qualified a) where
ppr = text . show
instance Serialize a => Serialize (Qualified a)
newtype BaseImage = BaseImage { _baseImage :: T.Text }
deriving (Eq)
instance Show BaseImage where
show = coerce (show :: T.Text -> String)
newtype SHA512 = SHA512 { _sha512 :: ByteString } deriving (Eq, Generic)
-- | Takes a textual version of a hash, i.e. that generated by sha512sum, and encode it
mkSHA512 :: Text -> Maybe SHA512
mkSHA512 x = if T.length x == 128 && BS.length hash == 64 && BS.length hashRem == 0 then Just (SHA512 hash) else Nothing
where (hash, hashRem) = Base16.decode . encodeUtf8 $ x
instance Show SHA512 where
show = T.unpack . decodeUtf8 . Base16.encode . _sha512
-- data LayerType = BtrfsXz deriving (Eq, Show, Read, Generic)
-- | A image is simply a single btrfs layer, that is applied to other parent module images
-- within the module
data Image = Image {
-- _layerType :: LayerType, -- mediatype, should be 'btrfs'
_sha :: SHA512, -- the SHA of the image layer for distribution
_iUuid :: UUID, -- btrfs uuid
_size :: Integer -- size in bytes of the compressed form of the layer
} deriving (Show, Eq, Generic)
instance Pretty Image where
ppr Image{..} = commasep [hashFrag, sizeMb]
where
hashFrag = text . take 8 . show $ _sha
sizeMb = text $ printf "%.1f MB" (fromInteger _size / 1000000 :: Double)
$(deriveSafeCopy 0 'base ''SHA512)
-- $(deriveSafeCopy 0 'base ''LayerType)
$(deriveSafeCopy 0 'base ''Image)
-- instance Serialize LayerType
instance Serialize SHA512
instance Serialize Image
newtype BusName = BusName { _unBusName :: Text } deriving (Eq, Ord, Show, IsString)
data DebugOpt = NoDebug | Debug deriving (Eq, Ord, Show, Generic)
-- monoid instance for DebugOpt is OR
instance Monoid DebugOpt where
mempty = NoDebug
NoDebug `mappend` a = a
Debug `mappend` _ = Debug
instance Serialize DebugOpt
instance ToJSON DebugOpt where
toJSON Debug = toJSON True
toJSON NoDebug = toJSON False
class HasDebugOpt a where
debugOpt :: Lens' a DebugOpt
unBusName :: Iso' BusName Text
unBusName = iso _unBusName BusName