module Hercules.Formats.NixCache where

import Data.Aeson
import Data.Text (Text)
import Hercules.Formats.Common
  ( noVersion,
    withKind,
    withVersions,
  )
import Prelude

-- | Credentials and keys for a cache.
data NixCache = NixCache
  { NixCache -> Text
storeURI :: Text,
    NixCache -> [Text]
signingKeys :: [Text],
    NixCache -> [Text]
publicKeys :: [Text]
  }

instance ToJSON NixCache where
  toJSON :: NixCache -> Value
toJSON NixCache
a =
    [Pair] -> Value
object
      [ Key
"kind" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"NixCache",
        Key
"storeURI" Key -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= NixCache -> Text
storeURI NixCache
a,
        Key
"signingKeys" Key -> [Text] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= NixCache -> [Text]
signingKeys NixCache
a,
        Key
"publicKeys" Key -> [Text] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= NixCache -> [Text]
publicKeys NixCache
a
      ]

  toEncoding :: NixCache -> Encoding
toEncoding NixCache
a =
    Series -> Encoding
pairs
      ( Key
"kind"
          Key -> Value -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"NixCache"
          Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"storeURI"
          Key -> Text -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= NixCache -> Text
storeURI NixCache
a
          Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"signingKeys"
          Key -> [Text] -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= NixCache -> [Text]
signingKeys NixCache
a
          Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Key
"publicKeys"
          Key -> [Text] -> Series
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= NixCache -> [Text]
publicKeys NixCache
a
      )

instance FromJSON NixCache where
  parseJSON :: Value -> Parser NixCache
parseJSON =
    Text -> (Object -> Parser NixCache) -> Value -> Parser NixCache
forall a. Text -> (Object -> Parser a) -> Value -> Parser a
withKind Text
"NixCache" ((Object -> Parser NixCache) -> Value -> Parser NixCache)
-> (Object -> Parser NixCache) -> Value -> Parser NixCache
forall a b. (a -> b) -> a -> b
$
      [VersionParser NixCache] -> Object -> Parser NixCache
forall a. [VersionParser a] -> Object -> Parser a
withVersions
        [ (Object -> Parser NixCache) -> VersionParser NixCache
forall a. (Object -> Parser a) -> VersionParser a
noVersion ((Object -> Parser NixCache) -> VersionParser NixCache)
-> (Object -> Parser NixCache) -> VersionParser NixCache
forall a b. (a -> b) -> a -> b
$ \Object
o ->
            Text -> [Text] -> [Text] -> NixCache
NixCache
              (Text -> [Text] -> [Text] -> NixCache)
-> Parser Text -> Parser ([Text] -> [Text] -> NixCache)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"storeURI"
              Parser ([Text] -> [Text] -> NixCache)
-> Parser [Text] -> Parser ([Text] -> NixCache)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser [Text]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"signingKeys"
              Parser ([Text] -> NixCache) -> Parser [Text] -> Parser NixCache
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser [Text]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"publicKeys"
        ]