{-# LANGUAGE QuasiQuotes #-}

-- | Provides the 'Backend' data type.
module Charon.Backend.Data
  ( Backend (..),
    parseBackend,
    backendTestDesc,
    backendName,
    backendArgOsPath,
    backendExt,
  )
where

import Charon.Prelude
import Data.Text qualified as T
import TOML (DecodeTOML (tomlDecoder))

-- | Type of backend.
data Backend
  = -- | For use with the cbor backend.
    BackendCbor
  | -- | For use with the FreeDesktopOrg backend.
    BackendFdo
  | -- | For use with the json backend.
    BackendJson
  deriving stock (Backend
Backend -> Backend -> Bounded Backend
forall a. a -> a -> Bounded a
$cminBound :: Backend
minBound :: Backend
$cmaxBound :: Backend
maxBound :: Backend
Bounded, Int -> Backend
Backend -> Int
Backend -> [Backend]
Backend -> Backend
Backend -> Backend -> [Backend]
Backend -> Backend -> Backend -> [Backend]
(Backend -> Backend)
-> (Backend -> Backend)
-> (Int -> Backend)
-> (Backend -> Int)
-> (Backend -> [Backend])
-> (Backend -> Backend -> [Backend])
-> (Backend -> Backend -> [Backend])
-> (Backend -> Backend -> Backend -> [Backend])
-> Enum Backend
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 :: Backend -> Backend
succ :: Backend -> Backend
$cpred :: Backend -> Backend
pred :: Backend -> Backend
$ctoEnum :: Int -> Backend
toEnum :: Int -> Backend
$cfromEnum :: Backend -> Int
fromEnum :: Backend -> Int
$cenumFrom :: Backend -> [Backend]
enumFrom :: Backend -> [Backend]
$cenumFromThen :: Backend -> Backend -> [Backend]
enumFromThen :: Backend -> Backend -> [Backend]
$cenumFromTo :: Backend -> Backend -> [Backend]
enumFromTo :: Backend -> Backend -> [Backend]
$cenumFromThenTo :: Backend -> Backend -> Backend -> [Backend]
enumFromThenTo :: Backend -> Backend -> Backend -> [Backend]
Enum, Backend -> Backend -> Bool
(Backend -> Backend -> Bool)
-> (Backend -> Backend -> Bool) -> Eq Backend
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Backend -> Backend -> Bool
== :: Backend -> Backend -> Bool
$c/= :: Backend -> Backend -> Bool
/= :: Backend -> Backend -> Bool
Eq, Int -> Backend -> ShowS
[Backend] -> ShowS
Backend -> [Char]
(Int -> Backend -> ShowS)
-> (Backend -> [Char]) -> ([Backend] -> ShowS) -> Show Backend
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Backend -> ShowS
showsPrec :: Int -> Backend -> ShowS
$cshow :: Backend -> [Char]
show :: Backend -> [Char]
$cshowList :: [Backend] -> ShowS
showList :: [Backend] -> ShowS
Show)

instance DecodeTOML Backend where
  tomlDecoder :: Decoder Backend
tomlDecoder = Decoder Text
forall a. DecodeTOML a => Decoder a
tomlDecoder Decoder Text -> (Text -> Decoder Backend) -> Decoder Backend
forall a b. Decoder a -> (a -> Decoder b) -> Decoder b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Decoder Backend
forall (m :: * -> *). MonadFail m => Text -> m Backend
parseBackend

-- | Parses a 'PathDataBackend'.
parseBackend :: (MonadFail m) => Text -> m Backend
parseBackend :: forall (m :: * -> *). MonadFail m => Text -> m Backend
parseBackend Text
"cbor" = Backend -> m Backend
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Backend
BackendCbor
parseBackend Text
"fdo" = Backend -> m Backend
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Backend
BackendFdo
parseBackend Text
"json" = Backend -> m Backend
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Backend
BackendJson
parseBackend Text
other =
  [Char] -> m Backend
forall a. [Char] -> m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail
    ([Char] -> m Backend) -> [Char] -> m Backend
forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
forall a. Monoid a => [a] -> a
mconcat
      [ [Char]
"Could not parse backend: '",
        Text -> [Char]
T.unpack Text
other,
        [Char]
"'. Expected 'cbor' or 'fdo'."
      ]

backendTestDesc :: Backend -> String
backendTestDesc :: Backend -> [Char]
backendTestDesc Backend
BackendCbor = [Char]
"(backend := cbor)"
backendTestDesc Backend
BackendFdo = [Char]
"(backend := fdo)"
backendTestDesc Backend
BackendJson = [Char]
"(backend := json)"

backendName :: (IsString s) => Backend -> s
backendName :: forall s. IsString s => Backend -> s
backendName Backend
BackendCbor = s
"cbor"
backendName Backend
BackendFdo = s
"fdo"
backendName Backend
BackendJson = s
"json"

backendArgOsPath :: Backend -> OsPath
backendArgOsPath :: Backend -> OsPath
backendArgOsPath Backend
BackendCbor = [osp|cbor|]
backendArgOsPath Backend
BackendFdo = [osp|fdo|]
backendArgOsPath Backend
BackendJson = [osp|json|]

backendExt :: Backend -> OsPath
backendExt :: Backend -> OsPath
backendExt Backend
BackendCbor = [osp|.cbor|]
backendExt Backend
BackendFdo = [osp|.trashinfo|]
backendExt Backend
BackendJson = [osp|.json|]