{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric  #-}

module Hyperion.ProgramId where

import           Data.Aeson                     (FromJSON, ToJSON)
import           Data.Binary                    (Binary)
import           Data.Text                      (Text, pack)
import qualified Database.SQLite.Simple.ToField as Sql
import           GHC.Generics                   (Generic)
import           Hyperion.Util                  (randomString)

newtype ProgramId = ProgramId Text
  deriving (Int -> ProgramId -> ShowS
[ProgramId] -> ShowS
ProgramId -> String
(Int -> ProgramId -> ShowS)
-> (ProgramId -> String)
-> ([ProgramId] -> ShowS)
-> Show ProgramId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ProgramId] -> ShowS
$cshowList :: [ProgramId] -> ShowS
show :: ProgramId -> String
$cshow :: ProgramId -> String
showsPrec :: Int -> ProgramId -> ShowS
$cshowsPrec :: Int -> ProgramId -> ShowS
Show, ProgramId -> ProgramId -> Bool
(ProgramId -> ProgramId -> Bool)
-> (ProgramId -> ProgramId -> Bool) -> Eq ProgramId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ProgramId -> ProgramId -> Bool
$c/= :: ProgramId -> ProgramId -> Bool
== :: ProgramId -> ProgramId -> Bool
$c== :: ProgramId -> ProgramId -> Bool
Eq, Eq ProgramId
Eq ProgramId
-> (ProgramId -> ProgramId -> Ordering)
-> (ProgramId -> ProgramId -> Bool)
-> (ProgramId -> ProgramId -> Bool)
-> (ProgramId -> ProgramId -> Bool)
-> (ProgramId -> ProgramId -> Bool)
-> (ProgramId -> ProgramId -> ProgramId)
-> (ProgramId -> ProgramId -> ProgramId)
-> Ord ProgramId
ProgramId -> ProgramId -> Bool
ProgramId -> ProgramId -> Ordering
ProgramId -> ProgramId -> ProgramId
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ProgramId -> ProgramId -> ProgramId
$cmin :: ProgramId -> ProgramId -> ProgramId
max :: ProgramId -> ProgramId -> ProgramId
$cmax :: ProgramId -> ProgramId -> ProgramId
>= :: ProgramId -> ProgramId -> Bool
$c>= :: ProgramId -> ProgramId -> Bool
> :: ProgramId -> ProgramId -> Bool
$c> :: ProgramId -> ProgramId -> Bool
<= :: ProgramId -> ProgramId -> Bool
$c<= :: ProgramId -> ProgramId -> Bool
< :: ProgramId -> ProgramId -> Bool
$c< :: ProgramId -> ProgramId -> Bool
compare :: ProgramId -> ProgramId -> Ordering
$ccompare :: ProgramId -> ProgramId -> Ordering
$cp1Ord :: Eq ProgramId
Ord, (forall x. ProgramId -> Rep ProgramId x)
-> (forall x. Rep ProgramId x -> ProgramId) -> Generic ProgramId
forall x. Rep ProgramId x -> ProgramId
forall x. ProgramId -> Rep ProgramId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ProgramId x -> ProgramId
$cfrom :: forall x. ProgramId -> Rep ProgramId x
Generic, Get ProgramId
[ProgramId] -> Put
ProgramId -> Put
(ProgramId -> Put)
-> Get ProgramId -> ([ProgramId] -> Put) -> Binary ProgramId
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [ProgramId] -> Put
$cputList :: [ProgramId] -> Put
get :: Get ProgramId
$cget :: Get ProgramId
put :: ProgramId -> Put
$cput :: ProgramId -> Put
Binary, Value -> Parser [ProgramId]
Value -> Parser ProgramId
(Value -> Parser ProgramId)
-> (Value -> Parser [ProgramId]) -> FromJSON ProgramId
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [ProgramId]
$cparseJSONList :: Value -> Parser [ProgramId]
parseJSON :: Value -> Parser ProgramId
$cparseJSON :: Value -> Parser ProgramId
FromJSON, [ProgramId] -> Encoding
[ProgramId] -> Value
ProgramId -> Encoding
ProgramId -> Value
(ProgramId -> Value)
-> (ProgramId -> Encoding)
-> ([ProgramId] -> Value)
-> ([ProgramId] -> Encoding)
-> ToJSON ProgramId
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ProgramId] -> Encoding
$ctoEncodingList :: [ProgramId] -> Encoding
toJSONList :: [ProgramId] -> Value
$ctoJSONList :: [ProgramId] -> Value
toEncoding :: ProgramId -> Encoding
$ctoEncoding :: ProgramId -> Encoding
toJSON :: ProgramId -> Value
$ctoJSON :: ProgramId -> Value
ToJSON)

instance Sql.ToField ProgramId where
  toField :: ProgramId -> SQLData
toField = Text -> SQLData
forall a. ToField a => a -> SQLData
Sql.toField (Text -> SQLData) -> (ProgramId -> Text) -> ProgramId -> SQLData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProgramId -> Text
programIdToText

programIdToText :: ProgramId -> Text
programIdToText :: ProgramId -> Text
programIdToText (ProgramId Text
t) = Text
t

newProgramId :: IO ProgramId
newProgramId :: IO ProgramId
newProgramId = (String -> ProgramId) -> IO String -> IO ProgramId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> ProgramId
ProgramId (Text -> ProgramId) -> (String -> Text) -> String -> ProgramId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack) (Int -> IO String
randomString Int
5)