module U.Codebase.Sqlite.ObjectType where

import Unison.Sqlite (FromField (..), SQLData (SQLInteger), ToField (..))

-- | Don't reorder these, they are part of the database,
--  and the ToField and FromField implementation currently
--  depends on the derived Enum implementation.
data ObjectType
  = TermComponent -- 0
  | DeclComponent -- 1
  | Namespace -- 2
  | Patch -- 3
  deriving (ObjectType -> ObjectType -> Bool
(ObjectType -> ObjectType -> Bool)
-> (ObjectType -> ObjectType -> Bool) -> Eq ObjectType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ObjectType -> ObjectType -> Bool
== :: ObjectType -> ObjectType -> Bool
$c/= :: ObjectType -> ObjectType -> Bool
/= :: ObjectType -> ObjectType -> Bool
Eq, Eq ObjectType
Eq ObjectType =>
(ObjectType -> ObjectType -> Ordering)
-> (ObjectType -> ObjectType -> Bool)
-> (ObjectType -> ObjectType -> Bool)
-> (ObjectType -> ObjectType -> Bool)
-> (ObjectType -> ObjectType -> Bool)
-> (ObjectType -> ObjectType -> ObjectType)
-> (ObjectType -> ObjectType -> ObjectType)
-> Ord ObjectType
ObjectType -> ObjectType -> Bool
ObjectType -> ObjectType -> Ordering
ObjectType -> ObjectType -> ObjectType
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
$ccompare :: ObjectType -> ObjectType -> Ordering
compare :: ObjectType -> ObjectType -> Ordering
$c< :: ObjectType -> ObjectType -> Bool
< :: ObjectType -> ObjectType -> Bool
$c<= :: ObjectType -> ObjectType -> Bool
<= :: ObjectType -> ObjectType -> Bool
$c> :: ObjectType -> ObjectType -> Bool
> :: ObjectType -> ObjectType -> Bool
$c>= :: ObjectType -> ObjectType -> Bool
>= :: ObjectType -> ObjectType -> Bool
$cmax :: ObjectType -> ObjectType -> ObjectType
max :: ObjectType -> ObjectType -> ObjectType
$cmin :: ObjectType -> ObjectType -> ObjectType
min :: ObjectType -> ObjectType -> ObjectType
Ord, Int -> ObjectType -> ShowS
[ObjectType] -> ShowS
ObjectType -> String
(Int -> ObjectType -> ShowS)
-> (ObjectType -> String)
-> ([ObjectType] -> ShowS)
-> Show ObjectType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ObjectType -> ShowS
showsPrec :: Int -> ObjectType -> ShowS
$cshow :: ObjectType -> String
show :: ObjectType -> String
$cshowList :: [ObjectType] -> ShowS
showList :: [ObjectType] -> ShowS
Show, Int -> ObjectType
ObjectType -> Int
ObjectType -> [ObjectType]
ObjectType -> ObjectType
ObjectType -> ObjectType -> [ObjectType]
ObjectType -> ObjectType -> ObjectType -> [ObjectType]
(ObjectType -> ObjectType)
-> (ObjectType -> ObjectType)
-> (Int -> ObjectType)
-> (ObjectType -> Int)
-> (ObjectType -> [ObjectType])
-> (ObjectType -> ObjectType -> [ObjectType])
-> (ObjectType -> ObjectType -> [ObjectType])
-> (ObjectType -> ObjectType -> ObjectType -> [ObjectType])
-> Enum ObjectType
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: ObjectType -> ObjectType
succ :: ObjectType -> ObjectType
$cpred :: ObjectType -> ObjectType
pred :: ObjectType -> ObjectType
$ctoEnum :: Int -> ObjectType
toEnum :: Int -> ObjectType
$cfromEnum :: ObjectType -> Int
fromEnum :: ObjectType -> Int
$cenumFrom :: ObjectType -> [ObjectType]
enumFrom :: ObjectType -> [ObjectType]
$cenumFromThen :: ObjectType -> ObjectType -> [ObjectType]
enumFromThen :: ObjectType -> ObjectType -> [ObjectType]
$cenumFromTo :: ObjectType -> ObjectType -> [ObjectType]
enumFromTo :: ObjectType -> ObjectType -> [ObjectType]
$cenumFromThenTo :: ObjectType -> ObjectType -> ObjectType -> [ObjectType]
enumFromThenTo :: ObjectType -> ObjectType -> ObjectType -> [ObjectType]
Enum)

instance ToField ObjectType where
  toField :: ObjectType -> SQLData
toField = Int64 -> SQLData
SQLInteger (Int64 -> SQLData)
-> (ObjectType -> Int64) -> ObjectType -> SQLData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int64) -> (ObjectType -> Int) -> ObjectType -> Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ObjectType -> Int
forall a. Enum a => a -> Int
fromEnum

instance FromField ObjectType where
  fromField :: FieldParser ObjectType
fromField = (Int -> ObjectType) -> Ok Int -> Ok ObjectType
forall a b. (a -> b) -> Ok a -> Ok b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Int -> ObjectType
forall a. Enum a => Int -> a
toEnum (Ok Int -> Ok ObjectType)
-> (Field -> Ok Int) -> FieldParser ObjectType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field -> Ok Int
forall a. FromField a => FieldParser a
fromField