summaryrefslogtreecommitdiff
path: root/Types/UUID.hs
blob: f5c9cda301e653e69eb104783203111de2af73d0 (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
{- git-annex UUID type
 -
 - Copyright 2011 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}

module Types.UUID where

import qualified Data.Map as M
import qualified Data.UUID as U
import Data.Maybe

import qualified Utility.SimpleProtocol as Proto

-- A UUID is either an arbitrary opaque string, or UUID info may be missing.
data UUID = NoUUID | UUID String
	deriving (Eq, Ord, Show, Read)

fromUUID :: UUID -> String
fromUUID (UUID u) = u
fromUUID NoUUID = ""

class ToUUID a where
	toUUID :: a -> UUID

instance ToUUID UUID where
	toUUID = id

instance ToUUID String where
	toUUID [] = NoUUID
	toUUID s = UUID s

isUUID :: String -> Bool
isUUID = isJust . U.fromString

type UUIDMap = M.Map UUID String

instance Proto.Serializable UUID where
	serialize = fromUUID
	deserialize = Just . toUUID