{-# LANGUAGE OverloadedStrings #-}

module Inferno.VersionControl.Server.Types where

import Data.Aeson.Types
import Data.ByteString (readFile)
import Data.Text (Text)
import Data.Yaml

data ServerConfig = ServerConfig
  { ServerConfig -> Text
_serverHost :: Text,
    ServerConfig -> Int
_serverPort :: Int,
    ServerConfig -> FilePath
_vcPath :: FilePath
  }
  deriving (Int -> ServerConfig -> ShowS
[ServerConfig] -> ShowS
ServerConfig -> FilePath
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [ServerConfig] -> ShowS
$cshowList :: [ServerConfig] -> ShowS
show :: ServerConfig -> FilePath
$cshow :: ServerConfig -> FilePath
showsPrec :: Int -> ServerConfig -> ShowS
$cshowsPrec :: Int -> ServerConfig -> ShowS
Show, ServerConfig -> ServerConfig -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ServerConfig -> ServerConfig -> Bool
$c/= :: ServerConfig -> ServerConfig -> Bool
== :: ServerConfig -> ServerConfig -> Bool
$c== :: ServerConfig -> ServerConfig -> Bool
Eq, Eq ServerConfig
ServerConfig -> ServerConfig -> Bool
ServerConfig -> ServerConfig -> Ordering
ServerConfig -> ServerConfig -> ServerConfig
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 :: ServerConfig -> ServerConfig -> ServerConfig
$cmin :: ServerConfig -> ServerConfig -> ServerConfig
max :: ServerConfig -> ServerConfig -> ServerConfig
$cmax :: ServerConfig -> ServerConfig -> ServerConfig
>= :: ServerConfig -> ServerConfig -> Bool
$c>= :: ServerConfig -> ServerConfig -> Bool
> :: ServerConfig -> ServerConfig -> Bool
$c> :: ServerConfig -> ServerConfig -> Bool
<= :: ServerConfig -> ServerConfig -> Bool
$c<= :: ServerConfig -> ServerConfig -> Bool
< :: ServerConfig -> ServerConfig -> Bool
$c< :: ServerConfig -> ServerConfig -> Bool
compare :: ServerConfig -> ServerConfig -> Ordering
$ccompare :: ServerConfig -> ServerConfig -> Ordering
Ord)

instance FromJSON ServerConfig where
  parseJSON :: Value -> Parser ServerConfig
parseJSON = forall a. FilePath -> (Object -> Parser a) -> Value -> Parser a
withObject FilePath
"ServerConfig" forall a b. (a -> b) -> a -> b
$ \Object
o -> do
    Object
server <- Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"server"
    Text
serverHost <- Object
server forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"host"
    Int
serverPort <- Object
server forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"port"
    FilePath
vcPath <- Object
server forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"vcPath"

    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Text -> Int -> FilePath -> ServerConfig
ServerConfig Text
serverHost Int
serverPort FilePath
vcPath

readServerConfig ::
  FilePath ->
  IO (Either String ServerConfig)
readServerConfig :: FilePath -> IO (Either FilePath ServerConfig)
readServerConfig FilePath
fp = do
  ByteString
f <- FilePath -> IO ByteString
Data.ByteString.readFile FilePath
fp
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> Either a b
Left forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseException -> FilePath
prettyPrintParseException) forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a. FromJSON a => ByteString -> Either ParseException a
decodeEither' ByteString
f