{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE MultiWayIf        #-}
{-# LANGUAGE NamedFieldPuns    #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE TypeApplications  #-}
{-|
  Module      : Auth.Biscuit.Utils
  Copyright   : © Clément Delafargue, 2021
  License     : BSD-3-Clause
  Maintainer  : clement@delafargue.name
  Conversion functions between biscuit components and protobuf-encoded components
-}
module Auth.Biscuit.ProtoBufAdapter
  ( Symbols
  , buildSymbolTable
  , pbToBlock
  , blockToPb
  , pbToSignedBlock
  , signedBlockToPb
  , pbToProof
  , pbToThirdPartyBlockRequest
  , thirdPartyBlockRequestToPb
  , pbToThirdPartyBlockContents
  , thirdPartyBlockContentsToPb
  ) where

import           Control.Monad            (unless, when)
import           Control.Monad.State      (StateT, get, lift, modify)
import           Data.Bitraversable       (bitraverse)
import           Data.ByteString          (ByteString)
import           Data.Int                 (Int64)
import qualified Data.List.NonEmpty       as NE
import qualified Data.Map                 as Map
import           Data.Maybe               (isJust, isNothing)
import qualified Data.Set                 as Set
import qualified Data.Text                as T
import           Data.Time                (UTCTime)
import           Data.Time.Clock.POSIX    (posixSecondsToUTCTime,
                                           utcTimeToPOSIXSeconds)
import           Data.Void                (absurd)
import           GHC.Records              (getField)
import           Validation               (Validation (..))

import qualified Auth.Biscuit.Crypto      as Crypto
import           Auth.Biscuit.Datalog.AST
import qualified Auth.Biscuit.Proto       as PB
import           Auth.Biscuit.Symbols
import           Auth.Biscuit.Utils       (maybeToRight)

buildSymbolTable :: Symbols -> Block -> BlockSymbols
buildSymbolTable :: Symbols -> Block -> BlockSymbols
buildSymbolTable Symbols
existingSymbols Block
block =
  let allSymbols :: Set Text
allSymbols = Block -> Set Text
listSymbolsInBlock Block
block
      allKeys :: Set PublicKey
allKeys = Block -> Set PublicKey
listPublicKeysInBlock Block
block
   in Symbols -> Set Text -> Set PublicKey -> BlockSymbols
addSymbols Symbols
existingSymbols Set Text
allSymbols Set PublicKey
allKeys

pbToPublicKey :: PB.PublicKey -> Either String Crypto.PublicKey
pbToPublicKey :: PublicKey -> Either String PublicKey
pbToPublicKey PB.PublicKey{Required 1 (Enumeration Algorithm)
Required 2 (Value ByteString)
algorithm :: Required 1 (Enumeration Algorithm)
key :: Required 2 (Value ByteString)
$sel:algorithm:PublicKey :: PublicKey -> Required 1 (Enumeration Algorithm)
$sel:key:PublicKey :: PublicKey -> Required 2 (Value ByteString)
..} =
  let keyBytes :: FieldType (Field 2 (RequiredField (Always (Value ByteString))))
keyBytes = Field 2 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 2 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Value ByteString)
Field 2 (RequiredField (Always (Value ByteString)))
key
      parseKey :: ByteString -> Maybe PublicKey
parseKey = ByteString -> Maybe PublicKey
Crypto.readEd25519PublicKey
   in case Field 1 (RequiredField (Always (Enumeration Algorithm)))
-> FieldType
     (Field 1 (RequiredField (Always (Enumeration Algorithm))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Enumeration Algorithm)
Field 1 (RequiredField (Always (Enumeration Algorithm)))
algorithm of
        FieldType
  (Field 1 (RequiredField (Always (Enumeration Algorithm))))
Algorithm
PB.Ed25519 -> String -> Maybe PublicKey -> Either String PublicKey
forall b a. b -> Maybe a -> Either b a
maybeToRight String
"Invalid ed25519 public key" (Maybe PublicKey -> Either String PublicKey)
-> Maybe PublicKey -> Either String PublicKey
forall a b. (a -> b) -> a -> b
$ ByteString -> Maybe PublicKey
parseKey ByteString
keyBytes

pbToOptionalSignature :: PB.ExternalSig -> Either String (Crypto.Signature, Crypto.PublicKey)
pbToOptionalSignature :: ExternalSig -> Either String (Signature, PublicKey)
pbToOptionalSignature PB.ExternalSig{Required 1 (Value ByteString)
Required 2 (Message PublicKey)
signature :: Required 1 (Value ByteString)
publicKey :: Required 2 (Message PublicKey)
$sel:signature:ExternalSig :: ExternalSig -> Required 1 (Value ByteString)
$sel:publicKey:ExternalSig :: ExternalSig -> Required 2 (Message PublicKey)
..} = do
  let sig :: Signature
sig = ByteString -> Signature
Crypto.signature (ByteString -> Signature) -> ByteString -> Signature
forall a b. (a -> b) -> a -> b
$ Field 1 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 1 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Value ByteString)
Field 1 (RequiredField (Always (Value ByteString)))
signature
  PublicKey
pk  <- PublicKey -> Either String PublicKey
pbToPublicKey (PublicKey -> Either String PublicKey)
-> PublicKey -> Either String PublicKey
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Message PublicKey)))
-> FieldType (Field 2 (RequiredField (Always (Message PublicKey))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Message PublicKey)
Field 2 (RequiredField (Always (Message PublicKey)))
publicKey
  (Signature, PublicKey) -> Either String (Signature, PublicKey)
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Signature
sig, PublicKey
pk)

-- | Parse a protobuf signed block into a signed biscuit block
pbToSignedBlock :: PB.SignedBlock -> Either String Crypto.SignedBlock
pbToSignedBlock :: SignedBlock -> Either String SignedBlock
pbToSignedBlock PB.SignedBlock{Required 1 (Value ByteString)
Required 2 (Message PublicKey)
Required 3 (Value ByteString)
Optional 4 (Message ExternalSig)
Optional 5 (Value Int32)
block :: Required 1 (Value ByteString)
nextKey :: Required 2 (Message PublicKey)
signature :: Required 3 (Value ByteString)
externalSig :: Optional 4 (Message ExternalSig)
version :: Optional 5 (Value Int32)
$sel:block:SignedBlock :: SignedBlock -> Required 1 (Value ByteString)
$sel:nextKey:SignedBlock :: SignedBlock -> Required 2 (Message PublicKey)
$sel:signature:SignedBlock :: SignedBlock -> Required 3 (Value ByteString)
$sel:externalSig:SignedBlock :: SignedBlock -> Optional 4 (Message ExternalSig)
$sel:version:SignedBlock :: SignedBlock -> Optional 5 (Value Int32)
..} = do
  let sig :: Signature
sig = ByteString -> Signature
Crypto.signature (ByteString -> Signature) -> ByteString -> Signature
forall a b. (a -> b) -> a -> b
$ Field 3 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 3 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 3 (Value ByteString)
Field 3 (RequiredField (Always (Value ByteString)))
signature
  Maybe (Signature, PublicKey)
mSig <- (ExternalSig -> Either String (Signature, PublicKey))
-> Maybe ExternalSig
-> Either String (Maybe (Signature, PublicKey))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse ExternalSig -> Either String (Signature, PublicKey)
pbToOptionalSignature (Maybe ExternalSig -> Either String (Maybe (Signature, PublicKey)))
-> Maybe ExternalSig
-> Either String (Maybe (Signature, PublicKey))
forall a b. (a -> b) -> a -> b
$ Field 4 (OptionalField (Maybe (Message ExternalSig)))
-> FieldType
     (Field 4 (OptionalField (Maybe (Message ExternalSig))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 4 (Message ExternalSig)
Field 4 (OptionalField (Maybe (Message ExternalSig)))
externalSig
  PublicKey
pk  <- PublicKey -> Either String PublicKey
pbToPublicKey (PublicKey -> Either String PublicKey)
-> PublicKey -> Either String PublicKey
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Message PublicKey)))
-> FieldType (Field 2 (RequiredField (Always (Message PublicKey))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Message PublicKey)
Field 2 (RequiredField (Always (Message PublicKey)))
nextKey
  let sigVersion :: Maybe Int
sigVersion = Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Int) -> Maybe Int32 -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Field 5 (OptionalField (Last (Value Int32)))
-> FieldType (Field 5 (OptionalField (Last (Value Int32))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 5 (Value Int32)
Field 5 (OptionalField (Last (Value Int32)))
version
  SignedBlock -> Either String SignedBlock
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ( Field 1 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 1 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Value ByteString)
Field 1 (RequiredField (Always (Value ByteString)))
block
       , Signature
sig
       , PublicKey
pk
       , Maybe (Signature, PublicKey)
mSig
       , Maybe Int
sigVersion
       )

publicKeyToPb :: Crypto.PublicKey -> PB.PublicKey
publicKeyToPb :: PublicKey -> PublicKey
publicKeyToPb PublicKey
pk = PB.PublicKey
  { $sel:algorithm:PublicKey :: Required 1 (Enumeration Algorithm)
algorithm = FieldType
  (Field 1 (RequiredField (Always (Enumeration Algorithm))))
-> Field 1 (RequiredField (Always (Enumeration Algorithm)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration Algorithm))))
Algorithm
PB.Ed25519
  , $sel:key:PublicKey :: Required 2 (Value ByteString)
key = FieldType (Required 2 (Value ByteString))
-> Required 2 (Value ByteString)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Value ByteString))
 -> Required 2 (Value ByteString))
-> FieldType (Required 2 (Value ByteString))
-> Required 2 (Value ByteString)
forall a b. (a -> b) -> a -> b
$ PublicKey -> ByteString
Crypto.pkBytes PublicKey
pk
  }

externalSigToPb :: (Crypto.Signature, Crypto.PublicKey) -> PB.ExternalSig
externalSigToPb :: (Signature, PublicKey) -> ExternalSig
externalSigToPb (Signature
sig, PublicKey
pk) = PB.ExternalSig
  { $sel:signature:ExternalSig :: Required 1 (Value ByteString)
signature = FieldType (Required 1 (Value ByteString))
-> Required 1 (Value ByteString)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 1 (Value ByteString))
 -> Required 1 (Value ByteString))
-> FieldType (Required 1 (Value ByteString))
-> Required 1 (Value ByteString)
forall a b. (a -> b) -> a -> b
$ Signature -> ByteString
Crypto.sigBytes Signature
sig
  , $sel:publicKey:ExternalSig :: Required 2 (Message PublicKey)
publicKey = FieldType (Required 2 (Message PublicKey))
-> Required 2 (Message PublicKey)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Message PublicKey))
 -> Required 2 (Message PublicKey))
-> FieldType (Required 2 (Message PublicKey))
-> Required 2 (Message PublicKey)
forall a b. (a -> b) -> a -> b
$ PublicKey -> PublicKey
publicKeyToPb PublicKey
pk
  }

signedBlockToPb :: Crypto.SignedBlock -> PB.SignedBlock
signedBlockToPb :: SignedBlock -> SignedBlock
signedBlockToPb (ByteString
block, Signature
sig, PublicKey
pk, Maybe (Signature, PublicKey)
eSig, Maybe Int
sigVersion) = PB.SignedBlock
  { $sel:block:SignedBlock :: Required 1 (Value ByteString)
block = FieldType (Field 1 (RequiredField (Always (Value ByteString))))
-> Field 1 (RequiredField (Always (Value ByteString)))
forall a. HasField a => FieldType a -> a
PB.putField ByteString
FieldType (Field 1 (RequiredField (Always (Value ByteString))))
block
  , $sel:signature:SignedBlock :: Required 3 (Value ByteString)
signature = FieldType (Required 3 (Value ByteString))
-> Required 3 (Value ByteString)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 3 (Value ByteString))
 -> Required 3 (Value ByteString))
-> FieldType (Required 3 (Value ByteString))
-> Required 3 (Value ByteString)
forall a b. (a -> b) -> a -> b
$ Signature -> ByteString
Crypto.sigBytes Signature
sig
  , $sel:nextKey:SignedBlock :: Required 2 (Message PublicKey)
nextKey = FieldType (Required 2 (Message PublicKey))
-> Required 2 (Message PublicKey)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Message PublicKey))
 -> Required 2 (Message PublicKey))
-> FieldType (Required 2 (Message PublicKey))
-> Required 2 (Message PublicKey)
forall a b. (a -> b) -> a -> b
$ PublicKey -> PublicKey
publicKeyToPb PublicKey
pk
  , $sel:externalSig:SignedBlock :: Optional 4 (Message ExternalSig)
externalSig = FieldType (Optional 4 (Message ExternalSig))
-> Optional 4 (Message ExternalSig)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Optional 4 (Message ExternalSig))
 -> Optional 4 (Message ExternalSig))
-> FieldType (Optional 4 (Message ExternalSig))
-> Optional 4 (Message ExternalSig)
forall a b. (a -> b) -> a -> b
$ (Signature, PublicKey) -> ExternalSig
externalSigToPb ((Signature, PublicKey) -> ExternalSig)
-> Maybe (Signature, PublicKey) -> Maybe ExternalSig
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Signature, PublicKey)
eSig
  , $sel:version:SignedBlock :: Optional 5 (Value Int32)
version = FieldType (Optional 5 (Value Int32)) -> Optional 5 (Value Int32)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Optional 5 (Value Int32)) -> Optional 5 (Value Int32))
-> FieldType (Optional 5 (Value Int32)) -> Optional 5 (Value Int32)
forall a b. (a -> b) -> a -> b
$ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int32) -> Maybe Int -> Maybe Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Int
sigVersion
  }

pbToProof :: PB.Proof -> Either String (Either Crypto.Signature Crypto.SecretKey)
pbToProof :: Proof -> Either String (Either Signature SecretKey)
pbToProof (PB.ProofSignature Required 2 (Value ByteString)
rawSig) = Signature -> Either Signature SecretKey
forall a b. a -> Either a b
Left  (Signature -> Either Signature SecretKey)
-> Either String Signature
-> Either String (Either Signature SecretKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Signature -> Either String Signature
forall a b. b -> Either a b
Right (ByteString -> Signature
Crypto.signature (ByteString -> Signature) -> ByteString -> Signature
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 2 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Value ByteString)
Field 2 (RequiredField (Always (Value ByteString)))
rawSig)
pbToProof (PB.ProofSecret    Required 1 (Value ByteString)
rawPk)  = SecretKey -> Either Signature SecretKey
forall a b. b -> Either a b
Right (SecretKey -> Either Signature SecretKey)
-> Either String SecretKey
-> Either String (Either Signature SecretKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Maybe SecretKey -> Either String SecretKey
forall b a. b -> Maybe a -> Either b a
maybeToRight String
"Invalid public key proof" (ByteString -> Maybe SecretKey
Crypto.readEd25519SecretKey (ByteString -> Maybe SecretKey) -> ByteString -> Maybe SecretKey
forall a b. (a -> b) -> a -> b
$ Field 1 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 1 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Value ByteString)
Field 1 (RequiredField (Always (Value ByteString)))
rawPk)

pbToBlock :: Maybe Crypto.PublicKey -> PB.Block -> StateT Symbols (Either String) Block
pbToBlock :: Maybe PublicKey -> Block -> StateT Symbols (Either String) Block
pbToBlock Maybe PublicKey
ePk PB.Block{Optional 2 (Value Text)
Optional 3 (Value Int32)
Repeated 1 (Value Text)
Repeated 4 (Message Fact)
Repeated 5 (Message Rule)
Repeated 6 (Message Check)
Repeated 7 (Message Scope)
Repeated 8 (Message PublicKey)
symbols :: Repeated 1 (Value Text)
context :: Optional 2 (Value Text)
version :: Optional 3 (Value Int32)
facts :: Repeated 4 (Message Fact)
rules :: Repeated 5 (Message Rule)
checks :: Repeated 6 (Message Check)
scope :: Repeated 7 (Message Scope)
pksTable :: Repeated 8 (Message PublicKey)
$sel:symbols:Block :: Block -> Repeated 1 (Value Text)
$sel:context:Block :: Block -> Optional 2 (Value Text)
$sel:version:Block :: Block -> Optional 3 (Value Int32)
$sel:facts:Block :: Block -> Repeated 4 (Message Fact)
$sel:rules:Block :: Block -> Repeated 5 (Message Rule)
$sel:checks:Block :: Block -> Repeated 6 (Message Check)
$sel:scope:Block :: Block -> Repeated 7 (Message Scope)
$sel:pksTable:Block :: Block -> Repeated 8 (Message PublicKey)
..} = do
  [PublicKey]
blockPks <- Either String [PublicKey]
-> StateT Symbols (Either String) [PublicKey]
forall (m :: * -> *) a. Monad m => m a -> StateT Symbols m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Either String [PublicKey]
 -> StateT Symbols (Either String) [PublicKey])
-> Either String [PublicKey]
-> StateT Symbols (Either String) [PublicKey]
forall a b. (a -> b) -> a -> b
$ (PublicKey -> Either String PublicKey)
-> [PublicKey] -> Either String [PublicKey]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse PublicKey -> Either String PublicKey
pbToPublicKey ([PublicKey] -> Either String [PublicKey])
-> [PublicKey] -> Either String [PublicKey]
forall a b. (a -> b) -> a -> b
$ Repeated 8 (Message PublicKey)
-> FieldType (Repeated 8 (Message PublicKey))
forall a. HasField a => a -> FieldType a
PB.getField Repeated 8 (Message PublicKey)
pksTable
  let blockSymbols :: FieldType (Repeated 1 (Value Text))
blockSymbols = Repeated 1 (Value Text) -> FieldType (Repeated 1 (Value Text))
forall a. HasField a => a -> FieldType a
PB.getField Repeated 1 (Value Text)
symbols
  -- third party blocks use an isolated symbol table,
  -- but use the global public keys table:
  --   symbols defined in 3rd party blocks are not visible
  --   to following blocks, but public keys are
  Bool
-> StateT Symbols (Either String) ()
-> StateT Symbols (Either String) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe PublicKey -> Bool
forall a. Maybe a -> Bool
isNothing Maybe PublicKey
ePk) (StateT Symbols (Either String) ()
 -> StateT Symbols (Either String) ())
-> StateT Symbols (Either String) ()
-> StateT Symbols (Either String) ()
forall a b. (a -> b) -> a -> b
$ do
    (Symbols -> Symbols) -> StateT Symbols (Either String) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ([Text] -> Symbols -> Symbols
registerNewSymbols [Text]
blockSymbols)
    (Symbols -> Symbols) -> StateT Symbols (Either String) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ([PublicKey] -> Symbols -> Symbols
registerNewPublicKeys [PublicKey]
blockPks)
  Symbols
currentSymbols <- StateT Symbols (Either String) Symbols
forall s (m :: * -> *). MonadState s m => m s
get

  let symbolsForCurrentBlock :: Symbols
symbolsForCurrentBlock =
        -- third party blocks use an isolated symbol and public keys table,
        --   3rd party blocks don't see previously defined
        --   symbols or public keys
        if Maybe PublicKey -> Bool
forall a. Maybe a -> Bool
isNothing Maybe PublicKey
ePk then Symbols
currentSymbols
                         else [PublicKey] -> Symbols -> Symbols
registerNewPublicKeys [PublicKey]
blockPks (Symbols -> Symbols) -> Symbols -> Symbols
forall a b. (a -> b) -> a -> b
$ [Text] -> Symbols -> Symbols
registerNewSymbols [Text]
blockSymbols Symbols
newSymbolTable
  let bContext :: FieldType (Field 2 (OptionalField (Last (Value Text))))
bContext = Field 2 (OptionalField (Last (Value Text)))
-> FieldType (Field 2 (OptionalField (Last (Value Text))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 2 (Value Text)
Field 2 (OptionalField (Last (Value Text)))
context
      bVersion :: FieldType (Field 3 (OptionalField (Last (Value Int32))))
bVersion = Field 3 (OptionalField (Last (Value Int32)))
-> FieldType (Field 3 (OptionalField (Last (Value Int32))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 3 (Value Int32)
Field 3 (OptionalField (Last (Value Int32)))
version
  Either String Block -> StateT Symbols (Either String) Block
forall (m :: * -> *) a. Monad m => m a -> StateT Symbols m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Either String Block -> StateT Symbols (Either String) Block)
-> Either String Block -> StateT Symbols (Either String) Block
forall a b. (a -> b) -> a -> b
$ do
    let s :: Symbols
s = Symbols
symbolsForCurrentBlock
    [Fact]
bFacts <- (Fact -> Either String Fact) -> [Fact] -> Either String [Fact]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Fact -> Either String Fact
pbToFact Symbols
s) ([Fact] -> Either String [Fact]) -> [Fact] -> Either String [Fact]
forall a b. (a -> b) -> a -> b
$ Repeated 4 (Message Fact) -> FieldType (Repeated 4 (Message Fact))
forall a. HasField a => a -> FieldType a
PB.getField Repeated 4 (Message Fact)
facts
    [Rule]
bRules <- (Rule -> Either String Rule) -> [Rule] -> Either String [Rule]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Rule -> Either String Rule
pbToRule Symbols
s) ([Rule] -> Either String [Rule]) -> [Rule] -> Either String [Rule]
forall a b. (a -> b) -> a -> b
$ Repeated 5 (Message Rule) -> FieldType (Repeated 5 (Message Rule))
forall a. HasField a => a -> FieldType a
PB.getField Repeated 5 (Message Rule)
rules
    [Check]
bChecks <- (Check -> Either String Check) -> [Check] -> Either String [Check]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Check -> Either String Check
pbToCheck Symbols
s) ([Check] -> Either String [Check])
-> [Check] -> Either String [Check]
forall a b. (a -> b) -> a -> b
$ Repeated 6 (Message Check)
-> FieldType (Repeated 6 (Message Check))
forall a. HasField a => a -> FieldType a
PB.getField Repeated 6 (Message Check)
checks
    Set RuleScope
bScope <- [RuleScope] -> Set RuleScope
forall a. Ord a => [a] -> Set a
Set.fromList ([RuleScope] -> Set RuleScope)
-> Either String [RuleScope] -> Either String (Set RuleScope)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Scope -> Either String RuleScope)
-> [Scope] -> Either String [RuleScope]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Scope -> Either String RuleScope
pbToScope Symbols
s) (Repeated 7 (Message Scope)
-> FieldType (Repeated 7 (Message Scope))
forall a. HasField a => a -> FieldType a
PB.getField Repeated 7 (Message Scope)
scope)
    let v6Plus :: Bool
v6Plus = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or
          [ (Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Check -> Bool
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> Bool
isReject [Check]
bChecks
          , Bool -> Bool
not ((Fact -> Bool) -> [Fact] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Fact -> Bool
forall (a :: PredicateOrFact) (b :: DatalogContext).
Predicate' a b -> Bool
predicateHasNoV6Values [Fact]
bFacts)
          , Bool -> Bool
not ((Rule -> Bool) -> [Rule] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Rule -> Bool
ruleHasNoV6Values [Rule]
bRules)
          , Bool -> Bool
not ((Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Check -> Bool
checkHasNoV6Values [Check]
bChecks)
          ]
        v5Plus :: Bool
v5Plus = Maybe PublicKey -> Bool
forall a. Maybe a -> Bool
isJust Maybe PublicKey
ePk
        v4Plus :: Bool
v4Plus = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and
          [ Set RuleScope -> Bool
forall a. Set a -> Bool
Set.null Set RuleScope
bScope
          , (Rule -> Bool) -> [Rule] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Rule -> Bool
ruleHasNoScope [Rule]
bRules
          , (Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Query -> Bool
queryHasNoScope (Query -> Bool) -> (Check -> Query) -> Check -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Check -> Query
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> Query' evalCtx ctx
cQueries) [Check]
bChecks
          , (Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Check -> Bool
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> Bool
isCheckOne [Check]
bChecks
          , (Rule -> Bool) -> [Rule] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Rule -> Bool
ruleHasNoV4Operators [Rule]
bRules
          , (Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Query -> Bool
queryHasNoV4Operators (Query -> Bool) -> (Check -> Query) -> Check -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Check -> Query
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> Query' evalCtx ctx
cQueries) [Check]
bChecks
          ]
    case (Maybe Int32
bVersion, Bool
v4Plus, Bool
v5Plus, Bool
v6Plus) of
      (Just Int32
6, Bool
_, Bool
_, Bool
_) -> Block -> Either String Block
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Block {[Rule]
[Check]
[Fact]
Maybe Text
Set RuleScope
bContext :: Maybe Text
bFacts :: [Fact]
bRules :: [Rule]
bChecks :: [Check]
bScope :: Set RuleScope
bRules :: [Rule]
bFacts :: [Fact]
bChecks :: [Check]
bContext :: Maybe Text
bScope :: Set RuleScope
..}
      (Just Int32
5, Bool
_, Bool
_, Bool
True) ->
        String -> Either String Block
forall a b. a -> Either a b
Left String
"Biscuit v6 features are present, but the block version is 5."
      (Just Int32
5, Bool
_, Bool
_, Bool
_) -> Block -> Either String Block
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Block {[Rule]
[Check]
[Fact]
Maybe Text
Set RuleScope
bContext :: Maybe Text
bFacts :: [Fact]
bRules :: [Rule]
bChecks :: [Check]
bScope :: Set RuleScope
bRules :: [Rule]
bFacts :: [Fact]
bChecks :: [Check]
bContext :: Maybe Text
bScope :: Set RuleScope
..}
      (Just Int32
4, Bool
_, Bool
False, Bool
False) -> Block -> Either String Block
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Block {[Rule]
[Check]
[Fact]
Maybe Text
Set RuleScope
bContext :: Maybe Text
bFacts :: [Fact]
bRules :: [Rule]
bChecks :: [Check]
bScope :: Set RuleScope
bRules :: [Rule]
bFacts :: [Fact]
bChecks :: [Check]
bContext :: Maybe Text
bScope :: Set RuleScope
..}
      (Just Int32
4, Bool
_, Bool
_, Bool
True) ->
        String -> Either String Block
forall a b. a -> Either a b
Left String
"Biscuit v6 features are present, but the block version is 4."
      (Just Int32
4, Bool
_, Bool
True, Bool
False) ->
        String -> Either String Block
forall a b. a -> Either a b
Left String
"Biscuit v5 features are present, but the block version is 4."
      (Just Int32
3, Bool
False, Bool
False, Bool
False) -> Block -> Either String Block
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Block {[Rule]
[Check]
[Fact]
Maybe Text
Set RuleScope
bContext :: Maybe Text
bFacts :: [Fact]
bRules :: [Rule]
bChecks :: [Check]
bScope :: Set RuleScope
bRules :: [Rule]
bFacts :: [Fact]
bChecks :: [Check]
bContext :: Maybe Text
bScope :: Set RuleScope
..}
      (Just Int32
3, Bool
True, Bool
False, Bool
False) ->
        String -> Either String Block
forall a b. a -> Either a b
Left String
"Biscuit v4 features are present, but the block version is 3."
      (Just Int32
3, Bool
_, Bool
True, Bool
False) ->
        String -> Either String Block
forall a b. a -> Either a b
Left String
"Biscuit v5 features are present, but the block version is 3."
      (Just Int32
3, Bool
_, Bool
_, Bool
True) ->
        String -> Either String Block
forall a b. a -> Either a b
Left String
"Biscuit v6 features are present, but the block version is 3."
      (Maybe Int32, Bool, Bool, Bool)
_ ->
        String -> Either String Block
forall a b. a -> Either a b
Left (String -> Either String Block) -> String -> Either String Block
forall a b. (a -> b) -> a -> b
$ String
"Unsupported biscuit version: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> (Int32 -> String) -> Maybe Int32 -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"0" Int32 -> String
forall a. Show a => a -> String
show Maybe Int32
bVersion String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
". Only versions 3 to 6 are supported"

-- | Turn a biscuit block into a protobuf block, for serialization,
-- along with the newly defined symbols
blockToPb :: Bool -> Symbols -> Block -> ((BlockSymbols, Int), PB.Block)
blockToPb :: Bool -> Symbols -> Block -> ((BlockSymbols, Int), Block)
blockToPb Bool
hasExternalPk Symbols
existingSymbols b :: Block
b@Block{[Rule]
[Check]
[Fact]
Maybe Text
Set RuleScope
bRules :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Block' evalCtx ctx -> [Rule' evalCtx ctx]
bFacts :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Block' evalCtx ctx -> [Predicate' 'InFact ctx]
bChecks :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Block' evalCtx ctx -> [Check' evalCtx ctx]
bContext :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Block' evalCtx ctx -> Maybe Text
bScope :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Block' evalCtx ctx -> Set (RuleScope' evalCtx ctx)
bRules :: [Rule]
bFacts :: [Fact]
bChecks :: [Check]
bContext :: Maybe Text
bScope :: Set RuleScope
..} =
  let v4Plus :: Bool
v4Plus = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and
        [Set RuleScope -> Bool
forall a. Set a -> Bool
Set.null Set RuleScope
bScope
        , (Rule -> Bool) -> [Rule] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Rule -> Bool
ruleHasNoScope [Rule]
bRules
        , (Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Query -> Bool
queryHasNoScope (Query -> Bool) -> (Check -> Query) -> Check -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Check -> Query
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> Query' evalCtx ctx
cQueries) [Check]
bChecks
        , (Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Check -> Bool
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> Bool
isCheckOne [Check]
bChecks
        , (Rule -> Bool) -> [Rule] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Rule -> Bool
ruleHasNoV4Operators [Rule]
bRules
        , (Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Query -> Bool
queryHasNoV4Operators (Query -> Bool) -> (Check -> Query) -> Check -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Check -> Query
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> Query' evalCtx ctx
cQueries) [Check]
bChecks
        ]
      v5Plus :: Bool
v5Plus = Bool
hasExternalPk
      v6Plus :: Bool
v6Plus = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or
        [ (Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Check -> Bool
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> Bool
isReject [Check]
bChecks
        , Bool -> Bool
not ((Fact -> Bool) -> [Fact] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Fact -> Bool
forall (a :: PredicateOrFact) (b :: DatalogContext).
Predicate' a b -> Bool
predicateHasNoV6Values [Fact]
bFacts)
        , Bool -> Bool
not ((Rule -> Bool) -> [Rule] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Rule -> Bool
ruleHasNoV6Values [Rule]
bRules)
        , Bool -> Bool
not ((Check -> Bool) -> [Check] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Check -> Bool
checkHasNoV6Values [Check]
bChecks)
        ]
      bSymbols :: BlockSymbols
bSymbols = Symbols -> Block -> BlockSymbols
buildSymbolTable Symbols
existingSymbols Block
b
      s :: ReverseSymbols
s = Symbols -> ReverseSymbols
reverseSymbols (Symbols -> ReverseSymbols) -> Symbols -> ReverseSymbols
forall a b. (a -> b) -> a -> b
$ Symbols -> BlockSymbols -> Symbols
addFromBlock Symbols
existingSymbols BlockSymbols
bSymbols
      symbols :: Repeated 1 (Value Text)
symbols  = FieldType (Repeated 1 (Value Text)) -> Repeated 1 (Value Text)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 1 (Value Text)) -> Repeated 1 (Value Text))
-> FieldType (Repeated 1 (Value Text)) -> Repeated 1 (Value Text)
forall a b. (a -> b) -> a -> b
$ BlockSymbols -> [Text]
getSymbolList BlockSymbols
bSymbols
      context :: Field 2 (OptionalField (Last (Value Text)))
context  = FieldType (Field 2 (OptionalField (Last (Value Text))))
-> Field 2 (OptionalField (Last (Value Text)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Text
FieldType (Field 2 (OptionalField (Last (Value Text))))
bContext
      facts :: Repeated 4 (Message Fact)
facts    = FieldType (Repeated 4 (Message Fact)) -> Repeated 4 (Message Fact)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 4 (Message Fact))
 -> Repeated 4 (Message Fact))
-> FieldType (Repeated 4 (Message Fact))
-> Repeated 4 (Message Fact)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Fact -> Fact
factToPb ReverseSymbols
s (Fact -> Fact) -> [Fact] -> [Fact]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Fact]
bFacts
      rules :: Repeated 5 (Message Rule)
rules    = FieldType (Repeated 5 (Message Rule)) -> Repeated 5 (Message Rule)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 5 (Message Rule))
 -> Repeated 5 (Message Rule))
-> FieldType (Repeated 5 (Message Rule))
-> Repeated 5 (Message Rule)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Rule -> Rule
ruleToPb ReverseSymbols
s (Rule -> Rule) -> [Rule] -> [Rule]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Rule]
bRules
      checks :: Repeated 6 (Message Check)
checks   = FieldType (Repeated 6 (Message Check))
-> Repeated 6 (Message Check)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 6 (Message Check))
 -> Repeated 6 (Message Check))
-> FieldType (Repeated 6 (Message Check))
-> Repeated 6 (Message Check)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Check -> Check
checkToPb ReverseSymbols
s (Check -> Check) -> [Check] -> [Check]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Check]
bChecks
      scope :: Repeated 7 (Message Scope)
scope    = FieldType (Repeated 7 (Message Scope))
-> Repeated 7 (Message Scope)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 7 (Message Scope))
 -> Repeated 7 (Message Scope))
-> FieldType (Repeated 7 (Message Scope))
-> Repeated 7 (Message Scope)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> RuleScope -> Scope
scopeToPb ReverseSymbols
s (RuleScope -> Scope) -> [RuleScope] -> [Scope]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Set RuleScope -> [RuleScope]
forall a. Set a -> [a]
Set.toList Set RuleScope
bScope
      pksTable :: Repeated 8 (Message PublicKey)
pksTable = FieldType (Repeated 8 (Message PublicKey))
-> Repeated 8 (Message PublicKey)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 8 (Message PublicKey))
 -> Repeated 8 (Message PublicKey))
-> FieldType (Repeated 8 (Message PublicKey))
-> Repeated 8 (Message PublicKey)
forall a b. (a -> b) -> a -> b
$ PublicKey -> PublicKey
publicKeyToPb (PublicKey -> PublicKey) -> [PublicKey] -> [PublicKey]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BlockSymbols -> [PublicKey]
getPkList BlockSymbols
bSymbols
      version :: Int
version  =  if | Bool
v6Plus    -> Int
6
                     | Bool
v5Plus    -> Int
5
                     | Bool
v4Plus    -> Int
4
                     | Bool
otherwise -> Int
3
   in ((BlockSymbols
bSymbols, Int
version), PB.Block {$sel:version:Block :: Optional 3 (Value Int32)
version = FieldType (Optional 3 (Value Int32)) -> Optional 3 (Value Int32)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Optional 3 (Value Int32)) -> Optional 3 (Value Int32))
-> FieldType (Optional 3 (Value Int32)) -> Optional 3 (Value Int32)
forall a b. (a -> b) -> a -> b
$ Int32 -> Maybe Int32
forall a. a -> Maybe a
Just (Int32 -> Maybe Int32) -> Int32 -> Maybe Int32
forall a b. (a -> b) -> a -> b
$ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
version, Optional 2 (Value Text)
Repeated 1 (Value Text)
Field 2 (OptionalField (Last (Value Text)))
Repeated 4 (Message Fact)
Repeated 5 (Message Rule)
Repeated 6 (Message Check)
Repeated 7 (Message Scope)
Repeated 8 (Message PublicKey)
$sel:symbols:Block :: Repeated 1 (Value Text)
$sel:context:Block :: Optional 2 (Value Text)
$sel:facts:Block :: Repeated 4 (Message Fact)
$sel:rules:Block :: Repeated 5 (Message Rule)
$sel:checks:Block :: Repeated 6 (Message Check)
$sel:scope:Block :: Repeated 7 (Message Scope)
$sel:pksTable:Block :: Repeated 8 (Message PublicKey)
symbols :: Repeated 1 (Value Text)
context :: Field 2 (OptionalField (Last (Value Text)))
facts :: Repeated 4 (Message Fact)
rules :: Repeated 5 (Message Rule)
checks :: Repeated 6 (Message Check)
scope :: Repeated 7 (Message Scope)
pksTable :: Repeated 8 (Message PublicKey)
..})

pbToFact :: Symbols -> PB.Fact -> Either String Fact
pbToFact :: Symbols -> Fact -> Either String Fact
pbToFact Symbols
s PB.Fact{Required 1 (Message Predicate)
predicate :: Required 1 (Message Predicate)
$sel:predicate:Fact :: Fact -> Required 1 (Message Predicate)
predicate} = do
  let pbName :: FieldType (Field 1 (RequiredField (Always (Value Int64))))
pbName  = Field 1 (RequiredField (Always (Value Int64)))
-> FieldType (Field 1 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField (Field 1 (RequiredField (Always (Value Int64)))
 -> FieldType (Field 1 (RequiredField (Always (Value Int64)))))
-> Field 1 (RequiredField (Always (Value Int64)))
-> FieldType (Field 1 (RequiredField (Always (Value Int64))))
forall a b. (a -> b) -> a -> b
$ Predicate -> Required 1 (Value Int64)
PB.name  (Predicate -> Required 1 (Value Int64))
-> Predicate -> Required 1 (Value Int64)
forall a b. (a -> b) -> a -> b
$ Field 1 (RequiredField (Always (Message Predicate)))
-> FieldType (Field 1 (RequiredField (Always (Message Predicate))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Message Predicate)
Field 1 (RequiredField (Always (Message Predicate)))
predicate
      pbTerms :: FieldType (Repeated 2 (Message Term))
pbTerms = Repeated 2 (Message Term) -> FieldType (Repeated 2 (Message Term))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 2 (Message Term)
 -> FieldType (Repeated 2 (Message Term)))
-> Repeated 2 (Message Term)
-> FieldType (Repeated 2 (Message Term))
forall a b. (a -> b) -> a -> b
$ Predicate -> Repeated 2 (Message Term)
PB.terms (Predicate -> Repeated 2 (Message Term))
-> Predicate -> Repeated 2 (Message Term)
forall a b. (a -> b) -> a -> b
$ Field 1 (RequiredField (Always (Message Predicate)))
-> FieldType (Field 1 (RequiredField (Always (Message Predicate))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Message Predicate)
Field 1 (RequiredField (Always (Message Predicate)))
predicate
  Text
name <- Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (SymbolRef -> Either String Text)
-> SymbolRef -> Either String Text
forall a b. (a -> b) -> a -> b
$ Int64 -> SymbolRef
SymbolRef Int64
pbName
  [Value]
terms <- (Term -> Either String Value) -> [Term] -> Either String [Value]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Term -> Either String Value
pbToValue Symbols
s) [Term]
pbTerms
  Fact -> Either String Fact
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Predicate{[Value]
Text
name :: Text
terms :: [Value]
name :: Text
terms :: [Value]
..}

factToPb :: ReverseSymbols -> Fact -> PB.Fact
factToPb :: ReverseSymbols -> Fact -> Fact
factToPb ReverseSymbols
s Predicate{[Value]
Text
name :: forall (pof :: PredicateOrFact) (ctx :: DatalogContext).
Predicate' pof ctx -> Text
terms :: forall (pof :: PredicateOrFact) (ctx :: DatalogContext).
Predicate' pof ctx -> [Term' 'NotWithinSet pof ctx]
name :: Text
terms :: [Value]
..} =
  let
      predicate :: Predicate
predicate = PB.Predicate
        { $sel:name:Predicate :: Required 1 (Value Int64)
name  = FieldType (Required 1 (Value Int64)) -> Required 1 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 1 (Value Int64)) -> Required 1 (Value Int64))
-> FieldType (Required 1 (Value Int64)) -> Required 1 (Value Int64)
forall a b. (a -> b) -> a -> b
$ SymbolRef -> Int64
getSymbolRef (SymbolRef -> Int64) -> SymbolRef -> Int64
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s Text
name
        , $sel:terms:Predicate :: Repeated 2 (Message Term)
terms = FieldType (Repeated 2 (Message Term)) -> Repeated 2 (Message Term)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 2 (Message Term))
 -> Repeated 2 (Message Term))
-> FieldType (Repeated 2 (Message Term))
-> Repeated 2 (Message Term)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Value -> Term
valueToPb ReverseSymbols
s (Value -> Term) -> [Value] -> [Term]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Value]
terms
        }
   in PB.Fact{$sel:predicate:Fact :: Required 1 (Message Predicate)
predicate = FieldType (Field 1 (RequiredField (Always (Message Predicate))))
-> Field 1 (RequiredField (Always (Message Predicate)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType (Field 1 (RequiredField (Always (Message Predicate))))
Predicate
predicate}

pbToRule :: Symbols -> PB.Rule -> Either String Rule
pbToRule :: Symbols -> Rule -> Either String Rule
pbToRule Symbols
s Rule
pbRule = do
  let pbHead :: FieldType (Required 1 (Message Predicate))
pbHead = Required 1 (Message Predicate)
-> FieldType (Required 1 (Message Predicate))
forall a. HasField a => a -> FieldType a
PB.getField (Required 1 (Message Predicate)
 -> FieldType (Required 1 (Message Predicate)))
-> Required 1 (Message Predicate)
-> FieldType (Required 1 (Message Predicate))
forall a b. (a -> b) -> a -> b
$ Rule -> Required 1 (Message Predicate)
PB.head Rule
pbRule
      pbBody :: FieldType (Repeated 2 (Message Predicate))
pbBody = Repeated 2 (Message Predicate)
-> FieldType (Repeated 2 (Message Predicate))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 2 (Message Predicate)
 -> FieldType (Repeated 2 (Message Predicate)))
-> Repeated 2 (Message Predicate)
-> FieldType (Repeated 2 (Message Predicate))
forall a b. (a -> b) -> a -> b
$ Rule -> Repeated 2 (Message Predicate)
PB.body Rule
pbRule
      pbExpressions :: FieldType (Repeated 3 (Message Expression))
pbExpressions = Repeated 3 (Message Expression)
-> FieldType (Repeated 3 (Message Expression))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 3 (Message Expression)
 -> FieldType (Repeated 3 (Message Expression)))
-> Repeated 3 (Message Expression)
-> FieldType (Repeated 3 (Message Expression))
forall a b. (a -> b) -> a -> b
$ Rule -> Repeated 3 (Message Expression)
PB.expressions Rule
pbRule
      pbScope :: FieldType (Repeated 4 (Message Scope))
pbScope = Repeated 4 (Message Scope)
-> FieldType (Repeated 4 (Message Scope))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 4 (Message Scope)
 -> FieldType (Repeated 4 (Message Scope)))
-> Repeated 4 (Message Scope)
-> FieldType (Repeated 4 (Message Scope))
forall a b. (a -> b) -> a -> b
$ forall {k} (x :: k) r a. HasField x r a => r -> a
forall (x :: Symbol) r a. HasField x r a => r -> a
getField @"scope" Rule
pbRule
  Predicate' 'InPredicate 'Representation
rhead       <- Symbols
-> Predicate
-> Either String (Predicate' 'InPredicate 'Representation)
pbToPredicate Symbols
s Predicate
pbHead
  [Predicate' 'InPredicate 'Representation]
body        <- (Predicate
 -> Either String (Predicate' 'InPredicate 'Representation))
-> [Predicate]
-> Either String [Predicate' 'InPredicate 'Representation]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols
-> Predicate
-> Either String (Predicate' 'InPredicate 'Representation)
pbToPredicate Symbols
s) [Predicate]
pbBody
  [Expression]
expressions <- (Expression -> Either String Expression)
-> [Expression] -> Either String [Expression]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Expression -> Either String Expression
pbToExpression Symbols
s) [Expression]
pbExpressions
  Set RuleScope
scope       <- [RuleScope] -> Set RuleScope
forall a. Ord a => [a] -> Set a
Set.fromList ([RuleScope] -> Set RuleScope)
-> Either String [RuleScope] -> Either String (Set RuleScope)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Scope -> Either String RuleScope)
-> [Scope] -> Either String [RuleScope]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Scope -> Either String RuleScope
pbToScope Symbols
s) [Scope]
pbScope
  case Predicate' 'InPredicate 'Representation
-> [Predicate' 'InPredicate 'Representation]
-> [Expression]
-> Set RuleScope
-> Validation (NonEmpty Text) Rule
forall (ctx :: DatalogContext).
Predicate' 'InPredicate ctx
-> [Predicate' 'InPredicate ctx]
-> [Expression' ctx]
-> Set (RuleScope' 'Repr ctx)
-> Validation (NonEmpty Text) (Rule' 'Repr ctx)
makeRule Predicate' 'InPredicate 'Representation
rhead [Predicate' 'InPredicate 'Representation]
body [Expression]
expressions Set RuleScope
scope of
    Failure NonEmpty Text
vs -> String -> Either String Rule
forall a b. a -> Either a b
Left (String -> Either String Rule) -> String -> Either String Rule
forall a b. (a -> b) -> a -> b
$ String
"Unbound variables in rule: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (Text -> [Text] -> Text
T.intercalate Text
", " ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ NonEmpty Text -> [Text]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty Text
vs)
    Success Rule
r  -> Rule -> Either String Rule
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Rule
r

ruleToPb :: ReverseSymbols -> Rule -> PB.Rule
ruleToPb :: ReverseSymbols -> Rule -> Rule
ruleToPb ReverseSymbols
s Rule{[Expression]
[Predicate' 'InPredicate 'Representation]
Set RuleScope
Predicate' 'InPredicate 'Representation
rhead :: Predicate' 'InPredicate 'Representation
body :: [Predicate' 'InPredicate 'Representation]
expressions :: [Expression]
scope :: Set RuleScope
rhead :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Rule' evalCtx ctx -> Predicate' 'InPredicate ctx
body :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Rule' evalCtx ctx -> [Predicate' 'InPredicate ctx]
expressions :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Rule' evalCtx ctx -> [Expression' ctx]
scope :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Rule' evalCtx ctx -> Set (RuleScope' evalCtx ctx)
..} =
  PB.Rule
    { $sel:head:Rule :: Required 1 (Message Predicate)
head = FieldType (Required 1 (Message Predicate))
-> Required 1 (Message Predicate)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 1 (Message Predicate))
 -> Required 1 (Message Predicate))
-> FieldType (Required 1 (Message Predicate))
-> Required 1 (Message Predicate)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols
-> Predicate' 'InPredicate 'Representation -> Predicate
predicateToPb ReverseSymbols
s Predicate' 'InPredicate 'Representation
rhead
    , $sel:body:Rule :: Repeated 2 (Message Predicate)
body = FieldType (Repeated 2 (Message Predicate))
-> Repeated 2 (Message Predicate)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 2 (Message Predicate))
 -> Repeated 2 (Message Predicate))
-> FieldType (Repeated 2 (Message Predicate))
-> Repeated 2 (Message Predicate)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols
-> Predicate' 'InPredicate 'Representation -> Predicate
predicateToPb ReverseSymbols
s (Predicate' 'InPredicate 'Representation -> Predicate)
-> [Predicate' 'InPredicate 'Representation] -> [Predicate]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Predicate' 'InPredicate 'Representation]
body
    , $sel:expressions:Rule :: Repeated 3 (Message Expression)
expressions = FieldType (Repeated 3 (Message Expression))
-> Repeated 3 (Message Expression)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 3 (Message Expression))
 -> Repeated 3 (Message Expression))
-> FieldType (Repeated 3 (Message Expression))
-> Repeated 3 (Message Expression)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Expression -> Expression
expressionToPb ReverseSymbols
s (Expression -> Expression) -> [Expression] -> [Expression]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Expression]
expressions
    , $sel:scope:Rule :: Repeated 4 (Message Scope)
scope = FieldType (Repeated 4 (Message Scope))
-> Repeated 4 (Message Scope)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 4 (Message Scope))
 -> Repeated 4 (Message Scope))
-> FieldType (Repeated 4 (Message Scope))
-> Repeated 4 (Message Scope)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> RuleScope -> Scope
scopeToPb ReverseSymbols
s (RuleScope -> Scope) -> [RuleScope] -> [Scope]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Set RuleScope -> [RuleScope]
forall a. Set a -> [a]
Set.toList Set RuleScope
scope
    }

pbToCheck :: Symbols -> PB.Check -> Either String Check
pbToCheck :: Symbols -> Check -> Either String Check
pbToCheck Symbols
s PB.Check{Repeated 1 (Message Rule)
queries :: Repeated 1 (Message Rule)
$sel:queries:Check :: Check -> Repeated 1 (Message Rule)
queries,Optional 2 (Enumeration CheckKind)
kind :: Optional 2 (Enumeration CheckKind)
$sel:kind:Check :: Check -> Optional 2 (Enumeration CheckKind)
kind} = do
  let toCheck :: Rule' evalCtx ctx -> QueryItem' evalCtx ctx
toCheck Rule{[Predicate' 'InPredicate ctx]
body :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Rule' evalCtx ctx -> [Predicate' 'InPredicate ctx]
body :: [Predicate' 'InPredicate ctx]
body,[Expression' ctx]
expressions :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Rule' evalCtx ctx -> [Expression' ctx]
expressions :: [Expression' ctx]
expressions,Set (RuleScope' evalCtx ctx)
scope :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Rule' evalCtx ctx -> Set (RuleScope' evalCtx ctx)
scope :: Set (RuleScope' evalCtx ctx)
scope} = QueryItem{qBody :: [Predicate' 'InPredicate ctx]
qBody = [Predicate' 'InPredicate ctx]
body, qExpressions :: [Expression' ctx]
qExpressions = [Expression' ctx]
expressions, qScope :: Set (RuleScope' evalCtx ctx)
qScope = Set (RuleScope' evalCtx ctx)
scope}
  [Rule]
rules <- (Rule -> Either String Rule) -> [Rule] -> Either String [Rule]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Rule -> Either String Rule
pbToRule Symbols
s) ([Rule] -> Either String [Rule]) -> [Rule] -> Either String [Rule]
forall a b. (a -> b) -> a -> b
$ Repeated 1 (Message Rule) -> FieldType (Repeated 1 (Message Rule))
forall a. HasField a => a -> FieldType a
PB.getField Repeated 1 (Message Rule)
queries
  let cQueries :: Query
cQueries = Rule -> QueryItem' 'Repr 'Representation
forall {evalCtx :: EvaluationContext} {ctx :: DatalogContext}.
Rule' evalCtx ctx -> QueryItem' evalCtx ctx
toCheck (Rule -> QueryItem' 'Repr 'Representation) -> [Rule] -> Query
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Rule]
rules
  let cKind :: CheckKind
cKind = case Field 2 (OptionalField (Last (Enumeration CheckKind)))
-> FieldType
     (Field 2 (OptionalField (Last (Enumeration CheckKind))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 2 (Enumeration CheckKind)
Field 2 (OptionalField (Last (Enumeration CheckKind)))
kind of
        Just CheckKind
PB.CheckAll -> CheckKind
CheckAll
        Just CheckKind
PB.CheckOne -> CheckKind
CheckOne
        Just CheckKind
PB.Reject   -> CheckKind
Reject
        Maybe CheckKind
FieldType (Field 2 (OptionalField (Last (Enumeration CheckKind))))
Nothing          -> CheckKind
CheckOne
  Check -> Either String Check
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Check{Query
CheckKind
cQueries :: Query
cQueries :: Query
cKind :: CheckKind
cKind :: CheckKind
..}

checkToPb :: ReverseSymbols -> Check -> PB.Check
checkToPb :: ReverseSymbols -> Check -> Check
checkToPb ReverseSymbols
s Check{Query
CheckKind
cQueries :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> Query' evalCtx ctx
cKind :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
Check' evalCtx ctx -> CheckKind
cQueries :: Query
cKind :: CheckKind
..} =
  let dummyHead :: Predicate' pof ctx
dummyHead = Text -> [Term' 'NotWithinSet pof ctx] -> Predicate' pof ctx
forall (pof :: PredicateOrFact) (ctx :: DatalogContext).
Text -> [Term' 'NotWithinSet pof ctx] -> Predicate' pof ctx
Predicate Text
"query" []
      toQuery :: QueryItem' 'Repr 'Representation -> Rule
toQuery QueryItem{[Expression]
[Predicate' 'InPredicate 'Representation]
Set RuleScope
qBody :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
QueryItem' evalCtx ctx -> [Predicate' 'InPredicate ctx]
qExpressions :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
QueryItem' evalCtx ctx -> [Expression' ctx]
qScope :: forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
QueryItem' evalCtx ctx -> Set (RuleScope' evalCtx ctx)
qBody :: [Predicate' 'InPredicate 'Representation]
qExpressions :: [Expression]
qScope :: Set RuleScope
..} =
        ReverseSymbols -> Rule -> Rule
ruleToPb ReverseSymbols
s (Rule -> Rule) -> Rule -> Rule
forall a b. (a -> b) -> a -> b
$ Rule { rhead :: Predicate' 'InPredicate 'Representation
rhead = Predicate' 'InPredicate 'Representation
forall {pof :: PredicateOrFact} {ctx :: DatalogContext}.
Predicate' pof ctx
dummyHead
                          , body :: [Predicate' 'InPredicate 'Representation]
body = [Predicate' 'InPredicate 'Representation]
qBody
                          , expressions :: [Expression]
expressions = [Expression]
qExpressions
                          , scope :: Set RuleScope
scope = Set RuleScope
qScope
                          }
      pbKind :: Maybe CheckKind
pbKind = case CheckKind
cKind of
        CheckKind
CheckOne -> Maybe CheckKind
forall a. Maybe a
Nothing
        CheckKind
CheckAll -> CheckKind -> Maybe CheckKind
forall a. a -> Maybe a
Just CheckKind
PB.CheckAll
        CheckKind
Reject   -> CheckKind -> Maybe CheckKind
forall a. a -> Maybe a
Just CheckKind
PB.Reject
   in PB.Check { $sel:queries:Check :: Repeated 1 (Message Rule)
queries = FieldType (Repeated 1 (Message Rule)) -> Repeated 1 (Message Rule)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 1 (Message Rule))
 -> Repeated 1 (Message Rule))
-> FieldType (Repeated 1 (Message Rule))
-> Repeated 1 (Message Rule)
forall a b. (a -> b) -> a -> b
$ QueryItem' 'Repr 'Representation -> Rule
toQuery (QueryItem' 'Repr 'Representation -> Rule) -> Query -> [Rule]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Query
cQueries
                 , $sel:kind:Check :: Optional 2 (Enumeration CheckKind)
kind = FieldType (Field 2 (OptionalField (Last (Enumeration CheckKind))))
-> Field 2 (OptionalField (Last (Enumeration CheckKind)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe CheckKind
FieldType (Field 2 (OptionalField (Last (Enumeration CheckKind))))
pbKind
                 }

pbToScope :: Symbols -> PB.Scope -> Either String RuleScope
pbToScope :: Symbols -> Scope -> Either String RuleScope
pbToScope Symbols
s = \case
  PB.ScType Required 1 (Enumeration ScopeType)
e       -> case Field 1 (RequiredField (Always (Enumeration ScopeType)))
-> FieldType
     (Field 1 (RequiredField (Always (Enumeration ScopeType))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Enumeration ScopeType)
Field 1 (RequiredField (Always (Enumeration ScopeType)))
e of
    FieldType
  (Field 1 (RequiredField (Always (Enumeration ScopeType))))
ScopeType
PB.ScopeAuthority -> RuleScope -> Either String RuleScope
forall a b. b -> Either a b
Right RuleScope
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
RuleScope' evalCtx ctx
OnlyAuthority
    FieldType
  (Field 1 (RequiredField (Always (Enumeration ScopeType))))
ScopeType
PB.ScopePrevious  -> RuleScope -> Either String RuleScope
forall a b. b -> Either a b
Right RuleScope
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
RuleScope' evalCtx ctx
Previous
  PB.ScBlock Required 2 (Value Int64)
pkRef ->
    PublicKey -> RuleScope
BlockIdType 'Repr 'Representation -> RuleScope
forall (evalCtx :: EvaluationContext) (ctx :: DatalogContext).
BlockIdType evalCtx ctx -> RuleScope' evalCtx ctx
BlockId (PublicKey -> RuleScope)
-> Either String PublicKey -> Either String RuleScope
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> PublicKeyRef -> Either String PublicKey
getPublicKey' Symbols
s (Int64 -> PublicKeyRef
PublicKeyRef (Int64 -> PublicKeyRef) -> Int64 -> PublicKeyRef
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Value Int64)))
-> FieldType (Field 2 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Value Int64)
Field 2 (RequiredField (Always (Value Int64)))
pkRef)

scopeToPb :: ReverseSymbols -> RuleScope -> PB.Scope
scopeToPb :: ReverseSymbols -> RuleScope -> Scope
scopeToPb ReverseSymbols
s = \case
  RuleScope
OnlyAuthority -> Required 1 (Enumeration ScopeType) -> Scope
PB.ScType (Required 1 (Enumeration ScopeType) -> Scope)
-> Required 1 (Enumeration ScopeType) -> Scope
forall a b. (a -> b) -> a -> b
$ FieldType
  (Field 1 (RequiredField (Always (Enumeration ScopeType))))
-> Field 1 (RequiredField (Always (Enumeration ScopeType)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration ScopeType))))
ScopeType
PB.ScopeAuthority
  RuleScope
Previous      -> Required 1 (Enumeration ScopeType) -> Scope
PB.ScType (Required 1 (Enumeration ScopeType) -> Scope)
-> Required 1 (Enumeration ScopeType) -> Scope
forall a b. (a -> b) -> a -> b
$ FieldType
  (Field 1 (RequiredField (Always (Enumeration ScopeType))))
-> Field 1 (RequiredField (Always (Enumeration ScopeType)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration ScopeType))))
ScopeType
PB.ScopePrevious
  BlockId BlockIdType 'Repr 'Representation
pk    -> Required 2 (Value Int64) -> Scope
PB.ScBlock (Required 2 (Value Int64) -> Scope)
-> Required 2 (Value Int64) -> Scope
forall a b. (a -> b) -> a -> b
$ FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64))
-> FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> PublicKey -> Int64
getPublicKeyCode ReverseSymbols
s PublicKey
BlockIdType 'Repr 'Representation
pk

pbToPredicate :: Symbols -> PB.Predicate -> Either String (Predicate' 'InPredicate 'Representation)
pbToPredicate :: Symbols
-> Predicate
-> Either String (Predicate' 'InPredicate 'Representation)
pbToPredicate Symbols
s Predicate
pbPredicate = do
  let pbName :: FieldType (Required 1 (Value Int64))
pbName  = Required 1 (Value Int64) -> FieldType (Required 1 (Value Int64))
forall a. HasField a => a -> FieldType a
PB.getField (Required 1 (Value Int64) -> FieldType (Required 1 (Value Int64)))
-> Required 1 (Value Int64) -> FieldType (Required 1 (Value Int64))
forall a b. (a -> b) -> a -> b
$ Predicate -> Required 1 (Value Int64)
PB.name  Predicate
pbPredicate
      pbTerms :: FieldType (Repeated 2 (Message Term))
pbTerms = Repeated 2 (Message Term) -> FieldType (Repeated 2 (Message Term))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 2 (Message Term)
 -> FieldType (Repeated 2 (Message Term)))
-> Repeated 2 (Message Term)
-> FieldType (Repeated 2 (Message Term))
forall a b. (a -> b) -> a -> b
$ Predicate -> Repeated 2 (Message Term)
PB.terms Predicate
pbPredicate
  Text
name <- Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (SymbolRef -> Either String Text)
-> SymbolRef -> Either String Text
forall a b. (a -> b) -> a -> b
$ Int64 -> SymbolRef
SymbolRef Int64
pbName
  [Term]
terms <- (Term -> Either String Term) -> [Term] -> Either String [Term]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Term -> Either String Term
pbToTerm Symbols
s) [Term]
pbTerms
  Predicate' 'InPredicate 'Representation
-> Either String (Predicate' 'InPredicate 'Representation)
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Predicate{[Term]
Text
name :: Text
terms :: [Term]
name :: Text
terms :: [Term]
..}

predicateToPb :: ReverseSymbols -> Predicate -> PB.Predicate
predicateToPb :: ReverseSymbols
-> Predicate' 'InPredicate 'Representation -> Predicate
predicateToPb ReverseSymbols
s Predicate{[Term]
Text
name :: forall (pof :: PredicateOrFact) (ctx :: DatalogContext).
Predicate' pof ctx -> Text
terms :: forall (pof :: PredicateOrFact) (ctx :: DatalogContext).
Predicate' pof ctx -> [Term' 'NotWithinSet pof ctx]
name :: Text
terms :: [Term]
..} =
  PB.Predicate
    { $sel:name:Predicate :: Required 1 (Value Int64)
name  = FieldType (Required 1 (Value Int64)) -> Required 1 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 1 (Value Int64)) -> Required 1 (Value Int64))
-> FieldType (Required 1 (Value Int64)) -> Required 1 (Value Int64)
forall a b. (a -> b) -> a -> b
$ SymbolRef -> Int64
getSymbolRef (SymbolRef -> Int64) -> SymbolRef -> Int64
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s Text
name
    , $sel:terms:Predicate :: Repeated 2 (Message Term)
terms = FieldType (Repeated 2 (Message Term)) -> Repeated 2 (Message Term)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 2 (Message Term))
 -> Repeated 2 (Message Term))
-> FieldType (Repeated 2 (Message Term))
-> Repeated 2 (Message Term)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Term -> Term
termToPb ReverseSymbols
s (Term -> Term) -> [Term] -> [Term]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Term]
terms
    }

pbTimeToUtcTime :: Int64 -> UTCTime
pbTimeToUtcTime :: Int64 -> UTCTime
pbTimeToUtcTime = POSIXTime -> UTCTime
posixSecondsToUTCTime (POSIXTime -> UTCTime) -> (Int64 -> POSIXTime) -> Int64 -> UTCTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> POSIXTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral

pbToTerm :: Symbols -> PB.Term -> Either String Term
pbToTerm :: Symbols -> Term -> Either String Term
pbToTerm Symbols
s = \case
  PB.TermInteger  Required 2 (Value Int64)
f -> Term -> Either String Term
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> Either String Term) -> Term -> Either String Term
forall a b. (a -> b) -> a -> b
$ Int64 -> Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Int64 -> Term' inSet pof ctx
LInteger (Int64 -> Term) -> Int64 -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Field 2 (RequiredField (Always (Value Int64)))) -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (FieldType (Field 2 (RequiredField (Always (Value Int64))))
 -> Int64)
-> FieldType (Field 2 (RequiredField (Always (Value Int64))))
-> Int64
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Value Int64)))
-> FieldType (Field 2 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Value Int64)
Field 2 (RequiredField (Always (Value Int64)))
f
  PB.TermString   Required 3 (Value Int64)
f ->        Text -> Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Text -> Term' inSet pof ctx
LString (Text -> Term) -> Either String Text -> Either String Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (Int64 -> SymbolRef
SymbolRef (Int64 -> SymbolRef) -> Int64 -> SymbolRef
forall a b. (a -> b) -> a -> b
$ Field 3 (RequiredField (Always (Value Int64)))
-> FieldType (Field 3 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 3 (Value Int64)
Field 3 (RequiredField (Always (Value Int64)))
f)
  PB.TermDate     Required 4 (Value Int64)
f -> Term -> Either String Term
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> Either String Term) -> Term -> Either String Term
forall a b. (a -> b) -> a -> b
$ UTCTime -> Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
UTCTime -> Term' inSet pof ctx
LDate    (UTCTime -> Term) -> UTCTime -> Term
forall a b. (a -> b) -> a -> b
$ Int64 -> UTCTime
pbTimeToUtcTime (Int64 -> UTCTime) -> Int64 -> UTCTime
forall a b. (a -> b) -> a -> b
$ Field 4 (RequiredField (Always (Value Int64)))
-> FieldType (Field 4 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 4 (Value Int64)
Field 4 (RequiredField (Always (Value Int64)))
f
  PB.TermBytes    Required 5 (Value ByteString)
f -> Term -> Either String Term
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> Either String Term) -> Term -> Either String Term
forall a b. (a -> b) -> a -> b
$ ByteString -> Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
ByteString -> Term' inSet pof ctx
LBytes   (ByteString -> Term) -> ByteString -> Term
forall a b. (a -> b) -> a -> b
$ Field 5 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 5 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 5 (Value ByteString)
Field 5 (RequiredField (Always (Value ByteString)))
f
  PB.TermBool     Required 6 (Value Bool)
f -> Term -> Either String Term
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term -> Either String Term) -> Term -> Either String Term
forall a b. (a -> b) -> a -> b
$ Bool -> Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Bool -> Term' inSet pof ctx
LBool    (Bool -> Term) -> Bool -> Term
forall a b. (a -> b) -> a -> b
$ Field 6 (RequiredField (Always (Value Bool)))
-> FieldType (Field 6 (RequiredField (Always (Value Bool))))
forall a. HasField a => a -> FieldType a
PB.getField Required 6 (Value Bool)
Field 6 (RequiredField (Always (Value Bool)))
f
  PB.TermVariable Required 1 (Value Int64)
f -> Text -> Term
VariableType 'NotWithinSet 'InPredicate -> Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
VariableType inSet pof -> Term' inSet pof ctx
Variable (Text -> Term) -> Either String Text -> Either String Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (Int64 -> SymbolRef
SymbolRef (Int64 -> SymbolRef) -> Int64 -> SymbolRef
forall a b. (a -> b) -> a -> b
$ Field 1 (RequiredField (Always (Value Int64)))
-> FieldType (Field 1 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Value Int64)
Field 1 (RequiredField (Always (Value Int64)))
f)
  PB.TermTermSet  Required 7 (Message TermSet)
f -> Set (Term' 'WithinSet 'InFact 'Representation) -> Term
SetType 'NotWithinSet 'Representation -> Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
SetType inSet ctx -> Term' inSet pof ctx
TermSet (Set (Term' 'WithinSet 'InFact 'Representation) -> Term)
-> ([Term' 'WithinSet 'InFact 'Representation]
    -> Set (Term' 'WithinSet 'InFact 'Representation))
-> [Term' 'WithinSet 'InFact 'Representation]
-> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Term' 'WithinSet 'InFact 'Representation]
-> Set (Term' 'WithinSet 'InFact 'Representation)
forall a. Ord a => [a] -> Set a
Set.fromList ([Term' 'WithinSet 'InFact 'Representation] -> Term)
-> Either String [Term' 'WithinSet 'InFact 'Representation]
-> Either String Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Term -> Either String (Term' 'WithinSet 'InFact 'Representation))
-> [Term]
-> Either String [Term' 'WithinSet 'InFact 'Representation]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols
-> Term -> Either String (Term' 'WithinSet 'InFact 'Representation)
pbToSetValue Symbols
s) (Repeated 1 (Message Term) -> [Term]
Repeated 1 (Message Term) -> FieldType (Repeated 1 (Message Term))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 1 (Message Term) -> [Term])
-> (TermSet -> Repeated 1 (Message Term)) -> TermSet -> [Term]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermSet -> Repeated 1 (Message Term)
PB.set (TermSet -> [Term]) -> TermSet -> [Term]
forall a b. (a -> b) -> a -> b
$ Field 7 (RequiredField (Always (Message TermSet)))
-> FieldType (Field 7 (RequiredField (Always (Message TermSet))))
forall a. HasField a => a -> FieldType a
PB.getField Required 7 (Message TermSet)
Field 7 (RequiredField (Always (Message TermSet)))
f)
  PB.TermTermArray Required 9 (Message TermArray)
f -> [Value] -> Term
ArrayType 'NotWithinSet 'Representation -> Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
ArrayType inSet ctx -> Term' inSet pof ctx
TermArray ([Value] -> Term) -> Either String [Value] -> Either String Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Term -> Either String Value) -> [Term] -> Either String [Value]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Term -> Either String Value
pbToValue Symbols
s) (Repeated 1 (Message Term) -> [Term]
Repeated 1 (Message Term) -> FieldType (Repeated 1 (Message Term))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 1 (Message Term) -> [Term])
-> (TermArray -> Repeated 1 (Message Term)) -> TermArray -> [Term]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermArray -> Repeated 1 (Message Term)
PB.array (TermArray -> [Term]) -> TermArray -> [Term]
forall a b. (a -> b) -> a -> b
$ Field 9 (RequiredField (Always (Message TermArray)))
-> FieldType (Field 9 (RequiredField (Always (Message TermArray))))
forall a. HasField a => a -> FieldType a
PB.getField Required 9 (Message TermArray)
Field 9 (RequiredField (Always (Message TermArray)))
f)
  PB.TermTermMap Required 10 (Message TermMap)
f -> Map MapKey Value -> Term
MapType 'NotWithinSet 'Representation -> Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
MapType inSet ctx -> Term' inSet pof ctx
TermMap (Map MapKey Value -> Term)
-> ([(MapKey, Value)] -> Map MapKey Value)
-> [(MapKey, Value)]
-> Term
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(MapKey, Value)] -> Map MapKey Value
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(MapKey, Value)] -> Term)
-> Either String [(MapKey, Value)] -> Either String Term
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (MapEntry -> Either String (MapKey, Value))
-> [MapEntry] -> Either String [(MapKey, Value)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> MapEntry -> Either String (MapKey, Value)
pbToMapEntry Symbols
s) (Repeated 1 (Message MapEntry) -> [MapEntry]
Repeated 1 (Message MapEntry)
-> FieldType (Repeated 1 (Message MapEntry))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 1 (Message MapEntry) -> [MapEntry])
-> (TermMap -> Repeated 1 (Message MapEntry))
-> TermMap
-> [MapEntry]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermMap -> Repeated 1 (Message MapEntry)
PB.map (TermMap -> [MapEntry]) -> TermMap -> [MapEntry]
forall a b. (a -> b) -> a -> b
$ Field 10 (RequiredField (Always (Message TermMap)))
-> FieldType (Field 10 (RequiredField (Always (Message TermMap))))
forall a. HasField a => a -> FieldType a
PB.getField Required 10 (Message TermMap)
Field 10 (RequiredField (Always (Message TermMap)))
f)
  PB.TermNull     Required 8 (Message Empty)
_ -> Term -> Either String Term
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Term' inSet pof ctx
LNull

termToPb :: ReverseSymbols -> Term -> PB.Term
termToPb :: ReverseSymbols -> Term -> Term
termToPb ReverseSymbols
s = \case
  Variable VariableType 'NotWithinSet 'InPredicate
n -> Required 1 (Value Int64) -> Term
PB.TermVariable (Required 1 (Value Int64) -> Term)
-> Required 1 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 1 (Value Int64)) -> Required 1 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 1 (Value Int64)) -> Required 1 (Value Int64))
-> FieldType (Required 1 (Value Int64)) -> Required 1 (Value Int64)
forall a b. (a -> b) -> a -> b
$ SymbolRef -> Int64
getSymbolRef (SymbolRef -> Int64) -> SymbolRef -> Int64
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s Text
VariableType 'NotWithinSet 'InPredicate
n
  LInteger Int64
v -> Required 2 (Value Int64) -> Term
PB.TermInteger  (Required 2 (Value Int64) -> Term)
-> Required 2 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64))
-> FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64)
forall a b. (a -> b) -> a -> b
$ Int64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
v
  LString  Text
v -> Required 3 (Value Int64) -> Term
PB.TermString   (Required 3 (Value Int64) -> Term)
-> Required 3 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 3 (Value Int64)) -> Required 3 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 3 (Value Int64)) -> Required 3 (Value Int64))
-> FieldType (Required 3 (Value Int64)) -> Required 3 (Value Int64)
forall a b. (a -> b) -> a -> b
$ SymbolRef -> Int64
getSymbolRef (SymbolRef -> Int64) -> SymbolRef -> Int64
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s Text
v
  LDate    UTCTime
v -> Required 4 (Value Int64) -> Term
PB.TermDate     (Required 4 (Value Int64) -> Term)
-> Required 4 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 4 (Value Int64)) -> Required 4 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 4 (Value Int64)) -> Required 4 (Value Int64))
-> FieldType (Required 4 (Value Int64)) -> Required 4 (Value Int64)
forall a b. (a -> b) -> a -> b
$ POSIXTime -> FieldType (Required 4 (Value Int64))
forall b. Integral b => POSIXTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (POSIXTime -> FieldType (Required 4 (Value Int64)))
-> POSIXTime -> FieldType (Required 4 (Value Int64))
forall a b. (a -> b) -> a -> b
$ UTCTime -> POSIXTime
utcTimeToPOSIXSeconds UTCTime
v
  LBytes   ByteString
v -> Required 5 (Value ByteString) -> Term
PB.TermBytes    (Required 5 (Value ByteString) -> Term)
-> Required 5 (Value ByteString) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Field 5 (RequiredField (Always (Value ByteString))))
-> Field 5 (RequiredField (Always (Value ByteString)))
forall a. HasField a => FieldType a -> a
PB.putField ByteString
FieldType (Field 5 (RequiredField (Always (Value ByteString))))
v
  LBool    Bool
v -> Required 6 (Value Bool) -> Term
PB.TermBool     (Required 6 (Value Bool) -> Term)
-> Required 6 (Value Bool) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Field 6 (RequiredField (Always (Value Bool))))
-> Field 6 (RequiredField (Always (Value Bool)))
forall a. HasField a => FieldType a -> a
PB.putField Bool
FieldType (Field 6 (RequiredField (Always (Value Bool))))
v
  TermSet SetType 'NotWithinSet 'Representation
vs -> Required 7 (Message TermSet) -> Term
PB.TermTermSet  (Required 7 (Message TermSet) -> Term)
-> Required 7 (Message TermSet) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 7 (Message TermSet))
-> Required 7 (Message TermSet)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 7 (Message TermSet))
 -> Required 7 (Message TermSet))
-> FieldType (Required 7 (Message TermSet))
-> Required 7 (Message TermSet)
forall a b. (a -> b) -> a -> b
$ Repeated 1 (Message Term) -> TermSet
PB.TermSet (Repeated 1 (Message Term) -> TermSet)
-> Repeated 1 (Message Term) -> TermSet
forall a b. (a -> b) -> a -> b
$ FieldType (Repeated 1 (Message Term)) -> Repeated 1 (Message Term)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 1 (Message Term))
 -> Repeated 1 (Message Term))
-> FieldType (Repeated 1 (Message Term))
-> Repeated 1 (Message Term)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Term' 'WithinSet 'InFact 'Representation -> Term
setValueToPb ReverseSymbols
s (Term' 'WithinSet 'InFact 'Representation -> Term)
-> [Term' 'WithinSet 'InFact 'Representation] -> [Term]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Set (Term' 'WithinSet 'InFact 'Representation)
-> [Term' 'WithinSet 'InFact 'Representation]
forall a. Set a -> [a]
Set.toList Set (Term' 'WithinSet 'InFact 'Representation)
SetType 'NotWithinSet 'Representation
vs
  TermArray ArrayType 'NotWithinSet 'Representation
vs -> Required 9 (Message TermArray) -> Term
PB.TermTermArray (Required 9 (Message TermArray) -> Term)
-> Required 9 (Message TermArray) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 9 (Message TermArray))
-> Required 9 (Message TermArray)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 9 (Message TermArray))
 -> Required 9 (Message TermArray))
-> FieldType (Required 9 (Message TermArray))
-> Required 9 (Message TermArray)
forall a b. (a -> b) -> a -> b
$ Repeated 1 (Message Term) -> TermArray
PB.TermArray (Repeated 1 (Message Term) -> TermArray)
-> Repeated 1 (Message Term) -> TermArray
forall a b. (a -> b) -> a -> b
$ FieldType (Repeated 1 (Message Term)) -> Repeated 1 (Message Term)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 1 (Message Term))
 -> Repeated 1 (Message Term))
-> FieldType (Repeated 1 (Message Term))
-> Repeated 1 (Message Term)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Value -> Term
valueToPb ReverseSymbols
s (Value -> Term) -> [Value] -> [Term]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Value]
ArrayType 'NotWithinSet 'Representation
vs
  TermMap MapType 'NotWithinSet 'Representation
vs -> Required 10 (Message TermMap) -> Term
PB.TermTermMap (Required 10 (Message TermMap) -> Term)
-> Required 10 (Message TermMap) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 10 (Message TermMap))
-> Required 10 (Message TermMap)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 10 (Message TermMap))
 -> Required 10 (Message TermMap))
-> FieldType (Required 10 (Message TermMap))
-> Required 10 (Message TermMap)
forall a b. (a -> b) -> a -> b
$ Repeated 1 (Message MapEntry) -> TermMap
PB.TermMap (Repeated 1 (Message MapEntry) -> TermMap)
-> Repeated 1 (Message MapEntry) -> TermMap
forall a b. (a -> b) -> a -> b
$ FieldType (Repeated 1 (Message MapEntry))
-> Repeated 1 (Message MapEntry)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 1 (Message MapEntry))
 -> Repeated 1 (Message MapEntry))
-> FieldType (Repeated 1 (Message MapEntry))
-> Repeated 1 (Message MapEntry)
forall a b. (a -> b) -> a -> b
$ (MapKey -> Value -> MapEntry) -> (MapKey, Value) -> MapEntry
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (ReverseSymbols -> MapKey -> Value -> MapEntry
mapEntryToPb ReverseSymbols
s) ((MapKey, Value) -> MapEntry) -> [(MapKey, Value)] -> [MapEntry]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map MapKey Value -> [(MapKey, Value)]
forall k a. Map k a -> [(k, a)]
Map.toList Map MapKey Value
MapType 'NotWithinSet 'Representation
vs
  Term
LNull      -> Required 8 (Message Empty) -> Term
PB.TermNull     (Required 8 (Message Empty) -> Term)
-> Required 8 (Message Empty) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 8 (Message Empty))
-> Required 8 (Message Empty)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 8 (Message Empty))
 -> Required 8 (Message Empty))
-> FieldType (Required 8 (Message Empty))
-> Required 8 (Message Empty)
forall a b. (a -> b) -> a -> b
$ PB.Empty {}

  Antiquote SliceType 'Representation
v -> Void -> Term
forall a. Void -> a
absurd Void
SliceType 'Representation
v

mapEntryToPb :: ReverseSymbols -> MapKey -> Value -> PB.MapEntry
mapEntryToPb :: ReverseSymbols -> MapKey -> Value -> MapEntry
mapEntryToPb ReverseSymbols
s MapKey
k Value
v = PB.MapEntry
  { $sel:key:MapEntry :: Required 1 (Message MapKey)
key = FieldType (Required 1 (Message MapKey))
-> Required 1 (Message MapKey)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 1 (Message MapKey))
 -> Required 1 (Message MapKey))
-> FieldType (Required 1 (Message MapKey))
-> Required 1 (Message MapKey)
forall a b. (a -> b) -> a -> b
$ case MapKey
k of
      IntKey Int64
i    -> Required 1 (Value Int64) -> MapKey
Field 1 (RequiredField (Always (Value Int64)))
-> FieldType (Required 1 (Message MapKey))
PB.MapKeyInt (Field 1 (RequiredField (Always (Value Int64)))
 -> FieldType (Required 1 (Message MapKey)))
-> (Int64 -> Field 1 (RequiredField (Always (Value Int64))))
-> Int64
-> FieldType (Required 1 (Message MapKey))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Field 1 (RequiredField (Always (Value Int64)))
FieldType (Field 1 (RequiredField (Always (Value Int64))))
-> Field 1 (RequiredField (Always (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField (Int64 -> FieldType (Required 1 (Message MapKey)))
-> Int64 -> FieldType (Required 1 (Message MapKey))
forall a b. (a -> b) -> a -> b
$ Int64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
i
      StringKey Text
n -> Required 2 (Value Int64) -> MapKey
Field 2 (RequiredField (Always (Value Int64)))
-> FieldType (Required 1 (Message MapKey))
PB.MapKeyString (Field 2 (RequiredField (Always (Value Int64)))
 -> FieldType (Required 1 (Message MapKey)))
-> (Int64 -> Field 2 (RequiredField (Always (Value Int64))))
-> Int64
-> FieldType (Required 1 (Message MapKey))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Field 2 (RequiredField (Always (Value Int64)))
FieldType (Field 2 (RequiredField (Always (Value Int64))))
-> Field 2 (RequiredField (Always (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField (Int64 -> FieldType (Required 1 (Message MapKey)))
-> Int64 -> FieldType (Required 1 (Message MapKey))
forall a b. (a -> b) -> a -> b
$ SymbolRef -> Int64
getSymbolRef (SymbolRef -> Int64) -> SymbolRef -> Int64
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s Text
n
  , $sel:value:MapEntry :: Required 2 (Message Term)
value = FieldType (Required 2 (Message Term)) -> Required 2 (Message Term)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Message Term))
 -> Required 2 (Message Term))
-> FieldType (Required 2 (Message Term))
-> Required 2 (Message Term)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Value -> Term
valueToPb ReverseSymbols
s Value
v
  }

pbToMapEntry :: Symbols -> PB.MapEntry -> Either String (MapKey, Value)
pbToMapEntry :: Symbols -> MapEntry -> Either String (MapKey, Value)
pbToMapEntry Symbols
s PB.MapEntry{Required 1 (Message MapKey)
$sel:key:MapEntry :: MapEntry -> Required 1 (Message MapKey)
key :: Required 1 (Message MapKey)
key,Required 2 (Message Term)
$sel:value:MapEntry :: MapEntry -> Required 2 (Message Term)
value :: Required 2 (Message Term)
value} = do
  MapKey
k <- case Field 1 (RequiredField (Always (Message MapKey)))
-> FieldType (Field 1 (RequiredField (Always (Message MapKey))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Message MapKey)
Field 1 (RequiredField (Always (Message MapKey)))
key of
         PB.MapKeyInt Required 1 (Value Int64)
i    -> MapKey -> Either String MapKey
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MapKey -> Either String MapKey)
-> (FieldType (Field 1 (RequiredField (Always (Value Int64))))
    -> MapKey)
-> FieldType (Field 1 (RequiredField (Always (Value Int64))))
-> Either String MapKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> MapKey
IntKey (Int64 -> MapKey)
-> (FieldType (Field 1 (RequiredField (Always (Value Int64))))
    -> Int64)
-> FieldType (Field 1 (RequiredField (Always (Value Int64))))
-> MapKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldType (Field 1 (RequiredField (Always (Value Int64)))) -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (FieldType (Field 1 (RequiredField (Always (Value Int64))))
 -> Either String MapKey)
-> FieldType (Field 1 (RequiredField (Always (Value Int64))))
-> Either String MapKey
forall a b. (a -> b) -> a -> b
$ Field 1 (RequiredField (Always (Value Int64)))
-> FieldType (Field 1 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Value Int64)
Field 1 (RequiredField (Always (Value Int64)))
i
         PB.MapKeyString Required 2 (Value Int64)
i -> Text -> MapKey
StringKey (Text -> MapKey) -> Either String Text -> Either String MapKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (Int64 -> SymbolRef
SymbolRef (Int64 -> SymbolRef) -> Int64 -> SymbolRef
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Value Int64)))
-> FieldType (Field 2 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Value Int64)
Field 2 (RequiredField (Always (Value Int64)))
i)
  Value
v <- Symbols -> Term -> Either String Value
pbToValue Symbols
s (Term -> Either String Value) -> Term -> Either String Value
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Message Term)))
-> FieldType (Field 2 (RequiredField (Always (Message Term))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Message Term)
Field 2 (RequiredField (Always (Message Term)))
value
  (MapKey, Value) -> Either String (MapKey, Value)
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MapKey
k, Value
v)

pbToValue :: Symbols -> PB.Term -> Either String Value
pbToValue :: Symbols -> Term -> Either String Value
pbToValue Symbols
s = \case
  PB.TermInteger  Required 2 (Value Int64)
f -> Value -> Either String Value
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Either String Value) -> Value -> Either String Value
forall a b. (a -> b) -> a -> b
$ Int64 -> Value
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Int64 -> Term' inSet pof ctx
LInteger (Int64 -> Value) -> Int64 -> Value
forall a b. (a -> b) -> a -> b
$ FieldType (Field 2 (RequiredField (Always (Value Int64)))) -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (FieldType (Field 2 (RequiredField (Always (Value Int64))))
 -> Int64)
-> FieldType (Field 2 (RequiredField (Always (Value Int64))))
-> Int64
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Value Int64)))
-> FieldType (Field 2 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Value Int64)
Field 2 (RequiredField (Always (Value Int64)))
f
  PB.TermString   Required 3 (Value Int64)
f ->        Text -> Value
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Text -> Term' inSet pof ctx
LString (Text -> Value) -> Either String Text -> Either String Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (Int64 -> SymbolRef
SymbolRef (Int64 -> SymbolRef) -> Int64 -> SymbolRef
forall a b. (a -> b) -> a -> b
$ Field 3 (RequiredField (Always (Value Int64)))
-> FieldType (Field 3 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 3 (Value Int64)
Field 3 (RequiredField (Always (Value Int64)))
f)
  PB.TermDate     Required 4 (Value Int64)
f -> Value -> Either String Value
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Either String Value) -> Value -> Either String Value
forall a b. (a -> b) -> a -> b
$ UTCTime -> Value
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
UTCTime -> Term' inSet pof ctx
LDate    (UTCTime -> Value) -> UTCTime -> Value
forall a b. (a -> b) -> a -> b
$ Int64 -> UTCTime
pbTimeToUtcTime (Int64 -> UTCTime) -> Int64 -> UTCTime
forall a b. (a -> b) -> a -> b
$ Field 4 (RequiredField (Always (Value Int64)))
-> FieldType (Field 4 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 4 (Value Int64)
Field 4 (RequiredField (Always (Value Int64)))
f
  PB.TermBytes    Required 5 (Value ByteString)
f -> Value -> Either String Value
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Either String Value) -> Value -> Either String Value
forall a b. (a -> b) -> a -> b
$ ByteString -> Value
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
ByteString -> Term' inSet pof ctx
LBytes   (ByteString -> Value) -> ByteString -> Value
forall a b. (a -> b) -> a -> b
$ Field 5 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 5 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 5 (Value ByteString)
Field 5 (RequiredField (Always (Value ByteString)))
f
  PB.TermBool     Required 6 (Value Bool)
f -> Value -> Either String Value
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Value -> Either String Value) -> Value -> Either String Value
forall a b. (a -> b) -> a -> b
$ Bool -> Value
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Bool -> Term' inSet pof ctx
LBool    (Bool -> Value) -> Bool -> Value
forall a b. (a -> b) -> a -> b
$ Field 6 (RequiredField (Always (Value Bool)))
-> FieldType (Field 6 (RequiredField (Always (Value Bool))))
forall a. HasField a => a -> FieldType a
PB.getField Required 6 (Value Bool)
Field 6 (RequiredField (Always (Value Bool)))
f
  PB.TermVariable Required 1 (Value Int64)
_ -> String -> Either String Value
forall a b. a -> Either a b
Left String
"Variables can't appear in facts"
  PB.TermTermSet  Required 7 (Message TermSet)
f -> Set (Term' 'WithinSet 'InFact 'Representation) -> Value
SetType 'NotWithinSet 'Representation -> Value
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
SetType inSet ctx -> Term' inSet pof ctx
TermSet (Set (Term' 'WithinSet 'InFact 'Representation) -> Value)
-> ([Term' 'WithinSet 'InFact 'Representation]
    -> Set (Term' 'WithinSet 'InFact 'Representation))
-> [Term' 'WithinSet 'InFact 'Representation]
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Term' 'WithinSet 'InFact 'Representation]
-> Set (Term' 'WithinSet 'InFact 'Representation)
forall a. Ord a => [a] -> Set a
Set.fromList ([Term' 'WithinSet 'InFact 'Representation] -> Value)
-> Either String [Term' 'WithinSet 'InFact 'Representation]
-> Either String Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Term -> Either String (Term' 'WithinSet 'InFact 'Representation))
-> [Term]
-> Either String [Term' 'WithinSet 'InFact 'Representation]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols
-> Term -> Either String (Term' 'WithinSet 'InFact 'Representation)
pbToSetValue Symbols
s) (Repeated 1 (Message Term) -> [Term]
Repeated 1 (Message Term) -> FieldType (Repeated 1 (Message Term))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 1 (Message Term) -> [Term])
-> (TermSet -> Repeated 1 (Message Term)) -> TermSet -> [Term]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermSet -> Repeated 1 (Message Term)
PB.set (TermSet -> [Term]) -> TermSet -> [Term]
forall a b. (a -> b) -> a -> b
$ Field 7 (RequiredField (Always (Message TermSet)))
-> FieldType (Field 7 (RequiredField (Always (Message TermSet))))
forall a. HasField a => a -> FieldType a
PB.getField Required 7 (Message TermSet)
Field 7 (RequiredField (Always (Message TermSet)))
f)
  PB.TermTermArray Required 9 (Message TermArray)
f -> [Value] -> Value
ArrayType 'NotWithinSet 'Representation -> Value
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
ArrayType inSet ctx -> Term' inSet pof ctx
TermArray ([Value] -> Value) -> Either String [Value] -> Either String Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Term -> Either String Value) -> [Term] -> Either String [Value]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Term -> Either String Value
pbToValue Symbols
s) (Repeated 1 (Message Term) -> [Term]
Repeated 1 (Message Term) -> FieldType (Repeated 1 (Message Term))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 1 (Message Term) -> [Term])
-> (TermArray -> Repeated 1 (Message Term)) -> TermArray -> [Term]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermArray -> Repeated 1 (Message Term)
PB.array (TermArray -> [Term]) -> TermArray -> [Term]
forall a b. (a -> b) -> a -> b
$ Field 9 (RequiredField (Always (Message TermArray)))
-> FieldType (Field 9 (RequiredField (Always (Message TermArray))))
forall a. HasField a => a -> FieldType a
PB.getField Required 9 (Message TermArray)
Field 9 (RequiredField (Always (Message TermArray)))
f)
  PB.TermTermMap Required 10 (Message TermMap)
f -> Map MapKey Value -> Value
MapType 'NotWithinSet 'Representation -> Value
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
MapType inSet ctx -> Term' inSet pof ctx
TermMap (Map MapKey Value -> Value)
-> ([(MapKey, Value)] -> Map MapKey Value)
-> [(MapKey, Value)]
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(MapKey, Value)] -> Map MapKey Value
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(MapKey, Value)] -> Value)
-> Either String [(MapKey, Value)] -> Either String Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (MapEntry -> Either String (MapKey, Value))
-> [MapEntry] -> Either String [(MapKey, Value)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> MapEntry -> Either String (MapKey, Value)
pbToMapEntry Symbols
s) (Repeated 1 (Message MapEntry) -> [MapEntry]
Repeated 1 (Message MapEntry)
-> FieldType (Repeated 1 (Message MapEntry))
forall a. HasField a => a -> FieldType a
PB.getField (Repeated 1 (Message MapEntry) -> [MapEntry])
-> (TermMap -> Repeated 1 (Message MapEntry))
-> TermMap
-> [MapEntry]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermMap -> Repeated 1 (Message MapEntry)
PB.map (TermMap -> [MapEntry]) -> TermMap -> [MapEntry]
forall a b. (a -> b) -> a -> b
$ Field 10 (RequiredField (Always (Message TermMap)))
-> FieldType (Field 10 (RequiredField (Always (Message TermMap))))
forall a. HasField a => a -> FieldType a
PB.getField Required 10 (Message TermMap)
Field 10 (RequiredField (Always (Message TermMap)))
f)
  PB.TermNull     Required 8 (Message Empty)
_ -> Value -> Either String Value
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Term' inSet pof ctx
LNull

valueToPb :: ReverseSymbols -> Value -> PB.Term
valueToPb :: ReverseSymbols -> Value -> Term
valueToPb ReverseSymbols
s = \case
  LInteger Int64
v -> Required 2 (Value Int64) -> Term
PB.TermInteger (Required 2 (Value Int64) -> Term)
-> Required 2 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64))
-> FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64)
forall a b. (a -> b) -> a -> b
$ Int64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
v
  LString  Text
v -> Required 3 (Value Int64) -> Term
PB.TermString  (Required 3 (Value Int64) -> Term)
-> Required 3 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 3 (Value Int64)) -> Required 3 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 3 (Value Int64)) -> Required 3 (Value Int64))
-> FieldType (Required 3 (Value Int64)) -> Required 3 (Value Int64)
forall a b. (a -> b) -> a -> b
$ SymbolRef -> Int64
getSymbolRef (SymbolRef -> Int64) -> SymbolRef -> Int64
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s Text
v
  LDate    UTCTime
v -> Required 4 (Value Int64) -> Term
PB.TermDate    (Required 4 (Value Int64) -> Term)
-> Required 4 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 4 (Value Int64)) -> Required 4 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 4 (Value Int64)) -> Required 4 (Value Int64))
-> FieldType (Required 4 (Value Int64)) -> Required 4 (Value Int64)
forall a b. (a -> b) -> a -> b
$ POSIXTime -> FieldType (Required 4 (Value Int64))
forall b. Integral b => POSIXTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (POSIXTime -> FieldType (Required 4 (Value Int64)))
-> POSIXTime -> FieldType (Required 4 (Value Int64))
forall a b. (a -> b) -> a -> b
$ UTCTime -> POSIXTime
utcTimeToPOSIXSeconds UTCTime
v
  LBytes   ByteString
v -> Required 5 (Value ByteString) -> Term
PB.TermBytes   (Required 5 (Value ByteString) -> Term)
-> Required 5 (Value ByteString) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Field 5 (RequiredField (Always (Value ByteString))))
-> Field 5 (RequiredField (Always (Value ByteString)))
forall a. HasField a => FieldType a -> a
PB.putField ByteString
FieldType (Field 5 (RequiredField (Always (Value ByteString))))
v
  LBool    Bool
v -> Required 6 (Value Bool) -> Term
PB.TermBool    (Required 6 (Value Bool) -> Term)
-> Required 6 (Value Bool) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Field 6 (RequiredField (Always (Value Bool))))
-> Field 6 (RequiredField (Always (Value Bool)))
forall a. HasField a => FieldType a -> a
PB.putField Bool
FieldType (Field 6 (RequiredField (Always (Value Bool))))
v
  TermSet SetType 'NotWithinSet 'Representation
vs -> Required 7 (Message TermSet) -> Term
PB.TermTermSet (Required 7 (Message TermSet) -> Term)
-> Required 7 (Message TermSet) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 7 (Message TermSet))
-> Required 7 (Message TermSet)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 7 (Message TermSet))
 -> Required 7 (Message TermSet))
-> FieldType (Required 7 (Message TermSet))
-> Required 7 (Message TermSet)
forall a b. (a -> b) -> a -> b
$ Repeated 1 (Message Term) -> TermSet
PB.TermSet (Repeated 1 (Message Term) -> TermSet)
-> Repeated 1 (Message Term) -> TermSet
forall a b. (a -> b) -> a -> b
$ FieldType (Repeated 1 (Message Term)) -> Repeated 1 (Message Term)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 1 (Message Term))
 -> Repeated 1 (Message Term))
-> FieldType (Repeated 1 (Message Term))
-> Repeated 1 (Message Term)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Term' 'WithinSet 'InFact 'Representation -> Term
setValueToPb ReverseSymbols
s (Term' 'WithinSet 'InFact 'Representation -> Term)
-> [Term' 'WithinSet 'InFact 'Representation] -> [Term]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Set (Term' 'WithinSet 'InFact 'Representation)
-> [Term' 'WithinSet 'InFact 'Representation]
forall a. Set a -> [a]
Set.toList Set (Term' 'WithinSet 'InFact 'Representation)
SetType 'NotWithinSet 'Representation
vs
  TermArray ArrayType 'NotWithinSet 'Representation
vs -> Required 9 (Message TermArray) -> Term
PB.TermTermArray (Required 9 (Message TermArray) -> Term)
-> Required 9 (Message TermArray) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 9 (Message TermArray))
-> Required 9 (Message TermArray)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 9 (Message TermArray))
 -> Required 9 (Message TermArray))
-> FieldType (Required 9 (Message TermArray))
-> Required 9 (Message TermArray)
forall a b. (a -> b) -> a -> b
$ Repeated 1 (Message Term) -> TermArray
PB.TermArray (Repeated 1 (Message Term) -> TermArray)
-> Repeated 1 (Message Term) -> TermArray
forall a b. (a -> b) -> a -> b
$ FieldType (Repeated 1 (Message Term)) -> Repeated 1 (Message Term)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 1 (Message Term))
 -> Repeated 1 (Message Term))
-> FieldType (Repeated 1 (Message Term))
-> Repeated 1 (Message Term)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Value -> Term
valueToPb ReverseSymbols
s (Value -> Term) -> [Value] -> [Term]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Value]
ArrayType 'NotWithinSet 'Representation
vs
  TermMap MapType 'NotWithinSet 'Representation
vs -> Required 10 (Message TermMap) -> Term
PB.TermTermMap (Required 10 (Message TermMap) -> Term)
-> Required 10 (Message TermMap) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 10 (Message TermMap))
-> Required 10 (Message TermMap)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 10 (Message TermMap))
 -> Required 10 (Message TermMap))
-> FieldType (Required 10 (Message TermMap))
-> Required 10 (Message TermMap)
forall a b. (a -> b) -> a -> b
$ Repeated 1 (Message MapEntry) -> TermMap
PB.TermMap (Repeated 1 (Message MapEntry) -> TermMap)
-> Repeated 1 (Message MapEntry) -> TermMap
forall a b. (a -> b) -> a -> b
$ FieldType (Repeated 1 (Message MapEntry))
-> Repeated 1 (Message MapEntry)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 1 (Message MapEntry))
 -> Repeated 1 (Message MapEntry))
-> FieldType (Repeated 1 (Message MapEntry))
-> Repeated 1 (Message MapEntry)
forall a b. (a -> b) -> a -> b
$ (MapKey -> Value -> MapEntry) -> (MapKey, Value) -> MapEntry
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (ReverseSymbols -> MapKey -> Value -> MapEntry
mapEntryToPb ReverseSymbols
s) ((MapKey, Value) -> MapEntry) -> [(MapKey, Value)] -> [MapEntry]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map MapKey Value -> [(MapKey, Value)]
forall k a. Map k a -> [(k, a)]
Map.toList Map MapKey Value
MapType 'NotWithinSet 'Representation
vs
  Value
LNull      -> Required 8 (Message Empty) -> Term
PB.TermNull (Required 8 (Message Empty) -> Term)
-> Required 8 (Message Empty) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Field 8 (RequiredField (Always (Message Empty))))
-> Field 8 (RequiredField (Always (Message Empty)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType (Field 8 (RequiredField (Always (Message Empty))))
Empty
PB.Empty

  Variable VariableType 'NotWithinSet 'InFact
v  -> Void -> Term
forall a. Void -> a
absurd Void
VariableType 'NotWithinSet 'InFact
v
  Antiquote SliceType 'Representation
v -> Void -> Term
forall a. Void -> a
absurd Void
SliceType 'Representation
v

pbToSetValue :: Symbols -> PB.Term -> Either String (Term' 'WithinSet 'InFact 'Representation)
pbToSetValue :: Symbols
-> Term -> Either String (Term' 'WithinSet 'InFact 'Representation)
pbToSetValue Symbols
s = \case
  PB.TermInteger  Required 2 (Value Int64)
f -> Term' 'WithinSet 'InFact 'Representation
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term' 'WithinSet 'InFact 'Representation
 -> Either String (Term' 'WithinSet 'InFact 'Representation))
-> Term' 'WithinSet 'InFact 'Representation
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a b. (a -> b) -> a -> b
$ Int64 -> Term' 'WithinSet 'InFact 'Representation
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Int64 -> Term' inSet pof ctx
LInteger (Int64 -> Term' 'WithinSet 'InFact 'Representation)
-> Int64 -> Term' 'WithinSet 'InFact 'Representation
forall a b. (a -> b) -> a -> b
$ FieldType (Field 2 (RequiredField (Always (Value Int64)))) -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (FieldType (Field 2 (RequiredField (Always (Value Int64))))
 -> Int64)
-> FieldType (Field 2 (RequiredField (Always (Value Int64))))
-> Int64
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Value Int64)))
-> FieldType (Field 2 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Value Int64)
Field 2 (RequiredField (Always (Value Int64)))
f
  PB.TermString   Required 3 (Value Int64)
f ->        Text -> Term' 'WithinSet 'InFact 'Representation
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Text -> Term' inSet pof ctx
LString  (Text -> Term' 'WithinSet 'InFact 'Representation)
-> Either String Text
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (Int64 -> SymbolRef
SymbolRef (Int64 -> SymbolRef) -> Int64 -> SymbolRef
forall a b. (a -> b) -> a -> b
$ Field 3 (RequiredField (Always (Value Int64)))
-> FieldType (Field 3 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 3 (Value Int64)
Field 3 (RequiredField (Always (Value Int64)))
f)
  PB.TermDate     Required 4 (Value Int64)
f -> Term' 'WithinSet 'InFact 'Representation
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term' 'WithinSet 'InFact 'Representation
 -> Either String (Term' 'WithinSet 'InFact 'Representation))
-> Term' 'WithinSet 'InFact 'Representation
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a b. (a -> b) -> a -> b
$ UTCTime -> Term' 'WithinSet 'InFact 'Representation
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
UTCTime -> Term' inSet pof ctx
LDate    (UTCTime -> Term' 'WithinSet 'InFact 'Representation)
-> UTCTime -> Term' 'WithinSet 'InFact 'Representation
forall a b. (a -> b) -> a -> b
$ Int64 -> UTCTime
pbTimeToUtcTime (Int64 -> UTCTime) -> Int64 -> UTCTime
forall a b. (a -> b) -> a -> b
$ Field 4 (RequiredField (Always (Value Int64)))
-> FieldType (Field 4 (RequiredField (Always (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Required 4 (Value Int64)
Field 4 (RequiredField (Always (Value Int64)))
f
  PB.TermBytes    Required 5 (Value ByteString)
f -> Term' 'WithinSet 'InFact 'Representation
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term' 'WithinSet 'InFact 'Representation
 -> Either String (Term' 'WithinSet 'InFact 'Representation))
-> Term' 'WithinSet 'InFact 'Representation
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a b. (a -> b) -> a -> b
$ ByteString -> Term' 'WithinSet 'InFact 'Representation
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
ByteString -> Term' inSet pof ctx
LBytes   (ByteString -> Term' 'WithinSet 'InFact 'Representation)
-> ByteString -> Term' 'WithinSet 'InFact 'Representation
forall a b. (a -> b) -> a -> b
$ Field 5 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 5 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 5 (Value ByteString)
Field 5 (RequiredField (Always (Value ByteString)))
f
  PB.TermBool     Required 6 (Value Bool)
f -> Term' 'WithinSet 'InFact 'Representation
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Term' 'WithinSet 'InFact 'Representation
 -> Either String (Term' 'WithinSet 'InFact 'Representation))
-> Term' 'WithinSet 'InFact 'Representation
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a b. (a -> b) -> a -> b
$ Bool -> Term' 'WithinSet 'InFact 'Representation
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Bool -> Term' inSet pof ctx
LBool    (Bool -> Term' 'WithinSet 'InFact 'Representation)
-> Bool -> Term' 'WithinSet 'InFact 'Representation
forall a b. (a -> b) -> a -> b
$ Field 6 (RequiredField (Always (Value Bool)))
-> FieldType (Field 6 (RequiredField (Always (Value Bool))))
forall a. HasField a => a -> FieldType a
PB.getField Required 6 (Value Bool)
Field 6 (RequiredField (Always (Value Bool)))
f
  PB.TermNull     Required 8 (Message Empty)
_ -> Term' 'WithinSet 'InFact 'Representation
-> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Term' 'WithinSet 'InFact 'Representation
forall (inSet :: IsWithinSet) (pof :: PredicateOrFact)
       (ctx :: DatalogContext).
Term' inSet pof ctx
LNull
  PB.TermVariable Required 1 (Value Int64)
_ -> String -> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a b. a -> Either a b
Left String
"Variables can't appear in facts or sets"
  PB.TermTermSet  Required 7 (Message TermSet)
_ -> String -> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a b. a -> Either a b
Left String
"Sets can't be nested"
  PB.TermTermArray Required 9 (Message TermArray)
_ -> String -> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a b. a -> Either a b
Left String
"Arrays can’t appear in sets"
  PB.TermTermMap Required 10 (Message TermMap)
_ -> String -> Either String (Term' 'WithinSet 'InFact 'Representation)
forall a b. a -> Either a b
Left String
"Maps can’t appear in sets"

setValueToPb :: ReverseSymbols -> Term' 'WithinSet 'InFact 'Representation -> PB.Term
setValueToPb :: ReverseSymbols -> Term' 'WithinSet 'InFact 'Representation -> Term
setValueToPb ReverseSymbols
s = \case
  LInteger Int64
v  -> Required 2 (Value Int64) -> Term
PB.TermInteger (Required 2 (Value Int64) -> Term)
-> Required 2 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64))
-> FieldType (Required 2 (Value Int64)) -> Required 2 (Value Int64)
forall a b. (a -> b) -> a -> b
$ Int64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
v
  LString  Text
v  -> Required 3 (Value Int64) -> Term
PB.TermString  (Required 3 (Value Int64) -> Term)
-> Required 3 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 3 (Value Int64)) -> Required 3 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 3 (Value Int64)) -> Required 3 (Value Int64))
-> FieldType (Required 3 (Value Int64)) -> Required 3 (Value Int64)
forall a b. (a -> b) -> a -> b
$ SymbolRef -> Int64
getSymbolRef (SymbolRef -> Int64) -> SymbolRef -> Int64
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s Text
v
  LDate    UTCTime
v  -> Required 4 (Value Int64) -> Term
PB.TermDate    (Required 4 (Value Int64) -> Term)
-> Required 4 (Value Int64) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 4 (Value Int64)) -> Required 4 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 4 (Value Int64)) -> Required 4 (Value Int64))
-> FieldType (Required 4 (Value Int64)) -> Required 4 (Value Int64)
forall a b. (a -> b) -> a -> b
$ POSIXTime -> FieldType (Required 4 (Value Int64))
forall b. Integral b => POSIXTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (POSIXTime -> FieldType (Required 4 (Value Int64)))
-> POSIXTime -> FieldType (Required 4 (Value Int64))
forall a b. (a -> b) -> a -> b
$ UTCTime -> POSIXTime
utcTimeToPOSIXSeconds UTCTime
v
  LBytes   ByteString
v  -> Required 5 (Value ByteString) -> Term
PB.TermBytes   (Required 5 (Value ByteString) -> Term)
-> Required 5 (Value ByteString) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Field 5 (RequiredField (Always (Value ByteString))))
-> Field 5 (RequiredField (Always (Value ByteString)))
forall a. HasField a => FieldType a -> a
PB.putField ByteString
FieldType (Field 5 (RequiredField (Always (Value ByteString))))
v
  LBool    Bool
v  -> Required 6 (Value Bool) -> Term
PB.TermBool    (Required 6 (Value Bool) -> Term)
-> Required 6 (Value Bool) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Field 6 (RequiredField (Always (Value Bool))))
-> Field 6 (RequiredField (Always (Value Bool)))
forall a. HasField a => FieldType a -> a
PB.putField Bool
FieldType (Field 6 (RequiredField (Always (Value Bool))))
v
  Term' 'WithinSet 'InFact 'Representation
LNull      -> Required 8 (Message Empty) -> Term
PB.TermNull     (Required 8 (Message Empty) -> Term)
-> Required 8 (Message Empty) -> Term
forall a b. (a -> b) -> a -> b
$ FieldType (Required 8 (Message Empty))
-> Required 8 (Message Empty)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 8 (Message Empty))
 -> Required 8 (Message Empty))
-> FieldType (Required 8 (Message Empty))
-> Required 8 (Message Empty)
forall a b. (a -> b) -> a -> b
$ PB.Empty {}

  TermSet   SetType 'WithinSet 'Representation
v -> Void -> Term
forall a. Void -> a
absurd Void
SetType 'WithinSet 'Representation
v
  TermArray ArrayType 'WithinSet 'Representation
v -> Void -> Term
forall a. Void -> a
absurd Void
ArrayType 'WithinSet 'Representation
v
  TermMap   MapType 'WithinSet 'Representation
v -> Void -> Term
forall a. Void -> a
absurd Void
MapType 'WithinSet 'Representation
v
  Variable  VariableType 'WithinSet 'InFact
v -> Void -> Term
forall a. Void -> a
absurd Void
VariableType 'WithinSet 'InFact
v
  Antiquote SliceType 'Representation
v -> Void -> Term
forall a. Void -> a
absurd Void
SliceType 'Representation
v

pbToExpression :: Symbols -> PB.Expression -> Either String Expression
pbToExpression :: Symbols -> Expression -> Either String Expression
pbToExpression Symbols
s PB.Expression{Repeated 1 (Message Op)
ops :: Repeated 1 (Message Op)
$sel:ops:Expression :: Expression -> Repeated 1 (Message Op)
ops} = do
  [Op]
parsedOps <- (Op -> Either String Op) -> [Op] -> Either String [Op]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Op -> Either String Op
pbToOp Symbols
s) ([Op] -> Either String [Op]) -> [Op] -> Either String [Op]
forall a b. (a -> b) -> a -> b
$ Repeated 1 (Message Op) -> FieldType (Repeated 1 (Message Op))
forall a. HasField a => a -> FieldType a
PB.getField Repeated 1 (Message Op)
ops
  [Op] -> Either String Expression
fromStack [Op]
parsedOps

expressionToPb :: ReverseSymbols -> Expression -> PB.Expression
expressionToPb :: ReverseSymbols -> Expression -> Expression
expressionToPb ReverseSymbols
s Expression
e =
  let ops :: [Op]
ops = ReverseSymbols -> Op -> Op
opToPb ReverseSymbols
s (Op -> Op) -> [Op] -> [Op]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Expression -> [Op]
toStack Expression
e
   in PB.Expression { $sel:ops:Expression :: Repeated 1 (Message Op)
ops = FieldType (Repeated 1 (Message Op)) -> Repeated 1 (Message Op)
forall a. HasField a => FieldType a -> a
PB.putField [Op]
FieldType (Repeated 1 (Message Op))
ops }

pbToOp :: Symbols -> PB.Op -> Either String Op
pbToOp :: Symbols -> Op -> Either String Op
pbToOp Symbols
s = \case
  PB.OpVValue Required 1 (Message Term)
v  -> Term -> Op
VOp (Term -> Op) -> Either String Term -> Either String Op
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> Term -> Either String Term
pbToTerm Symbols
s (Field 1 (RequiredField (Always (Message Term)))
-> FieldType (Field 1 (RequiredField (Always (Message Term))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Message Term)
Field 1 (RequiredField (Always (Message Term)))
v)
  PB.OpVUnary Required 2 (Message OpUnary)
v  -> Unary -> Op
UOp (Unary -> Op) -> Either String Unary -> Either String Op
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> OpUnary -> Either String Unary
pbToUnary Symbols
s (Field 2 (RequiredField (Always (Message OpUnary)))
-> FieldType (Field 2 (RequiredField (Always (Message OpUnary))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Message OpUnary)
Field 2 (RequiredField (Always (Message OpUnary)))
v)
  PB.OpVBinary Required 3 (Message OpBinary)
v -> Binary -> Op
BOp (Binary -> Op) -> Either String Binary -> Either String Op
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> OpBinary -> Either String Binary
pbToBinary Symbols
s (Field 3 (RequiredField (Always (Message OpBinary)))
-> FieldType (Field 3 (RequiredField (Always (Message OpBinary))))
forall a. HasField a => a -> FieldType a
PB.getField Required 3 (Message OpBinary)
Field 3 (RequiredField (Always (Message OpBinary)))
v)
  PB.OpVClosure Required 4 (Message OpClosure)
v -> ([Text] -> [Op] -> Op) -> ([Text], [Op]) -> Op
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [Text] -> [Op] -> Op
COp (([Text], [Op]) -> Op)
-> Either String ([Text], [Op]) -> Either String Op
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Symbols -> OpClosure -> Either String ([Text], [Op])
pbToClosure Symbols
s (Field 4 (RequiredField (Always (Message OpClosure)))
-> FieldType (Field 4 (RequiredField (Always (Message OpClosure))))
forall a. HasField a => a -> FieldType a
PB.getField Required 4 (Message OpClosure)
Field 4 (RequiredField (Always (Message OpClosure)))
v)

opToPb :: ReverseSymbols -> Op -> PB.Op
opToPb :: ReverseSymbols -> Op -> Op
opToPb ReverseSymbols
s = \case
  VOp Term
t -> Required 1 (Message Term) -> Op
PB.OpVValue  (Required 1 (Message Term) -> Op)
-> Required 1 (Message Term) -> Op
forall a b. (a -> b) -> a -> b
$ FieldType (Required 1 (Message Term)) -> Required 1 (Message Term)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 1 (Message Term))
 -> Required 1 (Message Term))
-> FieldType (Required 1 (Message Term))
-> Required 1 (Message Term)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Term -> Term
termToPb ReverseSymbols
s Term
t
  UOp Unary
o -> Required 2 (Message OpUnary) -> Op
PB.OpVUnary  (Required 2 (Message OpUnary) -> Op)
-> Required 2 (Message OpUnary) -> Op
forall a b. (a -> b) -> a -> b
$ FieldType (Required 2 (Message OpUnary))
-> Required 2 (Message OpUnary)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Message OpUnary))
 -> Required 2 (Message OpUnary))
-> FieldType (Required 2 (Message OpUnary))
-> Required 2 (Message OpUnary)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Unary -> OpUnary
unaryToPb ReverseSymbols
s Unary
o
  BOp Binary
o -> Required 3 (Message OpBinary) -> Op
PB.OpVBinary (Required 3 (Message OpBinary) -> Op)
-> Required 3 (Message OpBinary) -> Op
forall a b. (a -> b) -> a -> b
$ FieldType (Required 3 (Message OpBinary))
-> Required 3 (Message OpBinary)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 3 (Message OpBinary))
 -> Required 3 (Message OpBinary))
-> FieldType (Required 3 (Message OpBinary))
-> Required 3 (Message OpBinary)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Binary -> OpBinary
binaryToPb ReverseSymbols
s Binary
o
  COp [Text]
p [Op]
os -> Required 4 (Message OpClosure) -> Op
PB.OpVClosure (Required 4 (Message OpClosure) -> Op)
-> Required 4 (Message OpClosure) -> Op
forall a b. (a -> b) -> a -> b
$ FieldType (Required 4 (Message OpClosure))
-> Required 4 (Message OpClosure)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 4 (Message OpClosure))
 -> Required 4 (Message OpClosure))
-> FieldType (Required 4 (Message OpClosure))
-> Required 4 (Message OpClosure)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> [Text] -> [Op] -> OpClosure
closureToPb ReverseSymbols
s [Text]
p [Op]
os

pbToUnary :: Symbols -> PB.OpUnary -> Either String Unary
pbToUnary :: Symbols -> OpUnary -> Either String Unary
pbToUnary Symbols
s PB.OpUnary{Required 1 (Enumeration UnaryKind)
kind :: Required 1 (Enumeration UnaryKind)
$sel:kind:OpUnary :: OpUnary -> Required 1 (Enumeration UnaryKind)
kind,Optional 2 (Value Int64)
ffiName :: Optional 2 (Value Int64)
$sel:ffiName:OpUnary :: OpUnary -> Optional 2 (Value Int64)
ffiName} =
  let noFfi :: b -> Either String b
noFfi = case Field 2 (OptionalField (Last (Value Int64)))
-> FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 2 (Value Int64)
Field 2 (OptionalField (Last (Value Int64)))
ffiName of
                Just Int64
_  -> Either String b -> b -> Either String b
forall a b. a -> b -> a
const (Either String b -> b -> Either String b)
-> Either String b -> b -> Either String b
forall a b. (a -> b) -> a -> b
$ String -> Either String b
forall a b. a -> Either a b
Left String
"FFI named set on a regular operation "
                Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
Nothing -> b -> Either String b
forall a b. b -> Either a b
Right
   in case Field 1 (RequiredField (Always (Enumeration UnaryKind)))
-> FieldType
     (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Enumeration UnaryKind)
Field 1 (RequiredField (Always (Enumeration UnaryKind)))
kind of
        FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.Negate -> Unary -> Either String Unary
forall a. a -> Either String a
noFfi Unary
Negate
        FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.Parens -> Unary -> Either String Unary
forall a. a -> Either String a
noFfi Unary
Parens
        FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.Length -> Unary -> Either String Unary
forall a. a -> Either String a
noFfi Unary
Length
        FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.TypeOf -> Unary -> Either String Unary
forall a. a -> Either String a
noFfi Unary
TypeOf
        FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.UnaryFfi -> do
          Int64
nameIdx <- String -> Maybe Int64 -> Either String Int64
forall b a. b -> Maybe a -> Either b a
maybeToRight String
"Missing extern call name" (Maybe Int64 -> Either String Int64)
-> Maybe Int64 -> Either String Int64
forall a b. (a -> b) -> a -> b
$ Field 2 (OptionalField (Last (Value Int64)))
-> FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 2 (Value Int64)
Field 2 (OptionalField (Last (Value Int64)))
ffiName
          Text
name' <- Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (SymbolRef -> Either String Text)
-> SymbolRef -> Either String Text
forall a b. (a -> b) -> a -> b
$ Int64 -> SymbolRef
SymbolRef Int64
nameIdx
          Unary -> Either String Unary
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Unary -> Either String Unary) -> Unary -> Either String Unary
forall a b. (a -> b) -> a -> b
$ Text -> Unary
UnaryFfi Text
name'

unaryToPb :: ReverseSymbols -> Unary -> PB.OpUnary
unaryToPb :: ReverseSymbols -> Unary -> OpUnary
unaryToPb ReverseSymbols
s = \case
  Unary
Negate -> PB.OpUnary { $sel:kind:OpUnary :: Required 1 (Enumeration UnaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
-> Field 1 (RequiredField (Always (Enumeration UnaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.Negate, $sel:ffiName:OpUnary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Unary
Parens -> PB.OpUnary { $sel:kind:OpUnary :: Required 1 (Enumeration UnaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
-> Field 1 (RequiredField (Always (Enumeration UnaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.Parens, $sel:ffiName:OpUnary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Unary
Length -> PB.OpUnary { $sel:kind:OpUnary :: Required 1 (Enumeration UnaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
-> Field 1 (RequiredField (Always (Enumeration UnaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.Length, $sel:ffiName:OpUnary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Unary
TypeOf -> PB.OpUnary { $sel:kind:OpUnary :: Required 1 (Enumeration UnaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
-> Field 1 (RequiredField (Always (Enumeration UnaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.TypeOf, $sel:ffiName:OpUnary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  UnaryFfi Text
name ->
    PB.OpUnary {
      $sel:kind:OpUnary :: Required 1 (Enumeration UnaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
-> Field 1 (RequiredField (Always (Enumeration UnaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration UnaryKind))))
UnaryKind
PB.UnaryFfi,
      $sel:ffiName:OpUnary :: Optional 2 (Value Int64)
ffiName = Maybe Int64 -> Optional 2 (Value Int64)
FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField (Maybe Int64 -> Optional 2 (Value Int64))
-> (SymbolRef -> Maybe Int64)
-> SymbolRef
-> Optional 2 (Value Int64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Maybe Int64
forall a. a -> Maybe a
Just (Int64 -> Maybe Int64)
-> (SymbolRef -> Int64) -> SymbolRef -> Maybe Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymbolRef -> Int64
getSymbolRef (SymbolRef -> Optional 2 (Value Int64))
-> SymbolRef -> Optional 2 (Value Int64)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s Text
name
    }

pbToBinary :: Symbols -> PB.OpBinary -> Either String Binary
pbToBinary :: Symbols -> OpBinary -> Either String Binary
pbToBinary Symbols
s PB.OpBinary{Required 1 (Enumeration BinaryKind)
kind :: Required 1 (Enumeration BinaryKind)
$sel:kind:OpBinary :: OpBinary -> Required 1 (Enumeration BinaryKind)
kind, Optional 2 (Value Int64)
ffiName :: Optional 2 (Value Int64)
$sel:ffiName:OpBinary :: OpBinary -> Optional 2 (Value Int64)
ffiName} =
  let noFfi :: b -> Either String b
noFfi = case Field 2 (OptionalField (Last (Value Int64)))
-> FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 2 (Value Int64)
Field 2 (OptionalField (Last (Value Int64)))
ffiName of
                Just Int64
_  -> Either String b -> b -> Either String b
forall a b. a -> b -> a
const (Either String b -> b -> Either String b)
-> Either String b -> b -> Either String b
forall a b. (a -> b) -> a -> b
$ String -> Either String b
forall a b. a -> Either a b
Left String
"FFI named set on a regular operation "
                Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
Nothing -> b -> Either String b
forall a b. b -> Either a b
Right
   in case Field 1 (RequiredField (Always (Enumeration BinaryKind)))
-> FieldType
     (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Enumeration BinaryKind)
Field 1 (RequiredField (Always (Enumeration BinaryKind)))
kind of
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.LessThan              -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
LessThan
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.GreaterThan           -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
GreaterThan
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.LessOrEqual           -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
LessOrEqual
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.GreaterOrEqual        -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
GreaterOrEqual
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Equal                 -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Equal
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Contains              -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Contains
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Prefix                -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Prefix
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Suffix                -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Suffix
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Regex                 -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Regex
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Add                   -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Add
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Sub                   -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Sub
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Mul                   -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Mul
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Div                   -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Div
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.And                   -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
And
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Or                    -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Or
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Intersection          -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Intersection
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Union                 -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Union
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.BitwiseAnd            -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
BitwiseAnd
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.BitwiseOr             -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
BitwiseOr
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.BitwiseXor            -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
BitwiseXor
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.NotEqual              -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
NotEqual
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.HeterogeneousEqual    -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
HeterogeneousEqual
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.HeterogeneousNotEqual -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
HeterogeneousNotEqual
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.LazyAnd               -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
LazyAnd
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.LazyOr                -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
LazyOr
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.All                   -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
All
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Any                   -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Any
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Get                   -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Get
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.TryOr                 -> Binary -> Either String Binary
forall a. a -> Either String a
noFfi Binary
Try
        FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.BinaryFfi -> do
          Int64
nameIdx <- String -> Maybe Int64 -> Either String Int64
forall b a. b -> Maybe a -> Either b a
maybeToRight String
"Missing extern call name" (Maybe Int64 -> Either String Int64)
-> Maybe Int64 -> Either String Int64
forall a b. (a -> b) -> a -> b
$ Field 2 (OptionalField (Last (Value Int64)))
-> FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 2 (Value Int64)
Field 2 (OptionalField (Last (Value Int64)))
ffiName
          Text
name' <- Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (SymbolRef -> Either String Text)
-> SymbolRef -> Either String Text
forall a b. (a -> b) -> a -> b
$ Int64 -> SymbolRef
SymbolRef Int64
nameIdx
          Binary -> Either String Binary
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Binary -> Either String Binary) -> Binary -> Either String Binary
forall a b. (a -> b) -> a -> b
$ Text -> Binary
BinaryFfi Text
name'

pbToClosure :: Symbols -> PB.OpClosure -> Either String ([T.Text], [Op])
pbToClosure :: Symbols -> OpClosure -> Either String ([Text], [Op])
pbToClosure Symbols
s PB.OpClosure{Repeated 1 (Value Int64)
Repeated 2 (Message Op)
params :: Repeated 1 (Value Int64)
ops :: Repeated 2 (Message Op)
$sel:params:OpClosure :: OpClosure -> Repeated 1 (Value Int64)
$sel:ops:OpClosure :: OpClosure -> Repeated 2 (Message Op)
..} =
  let getParams :: Repeated 1 (Value Int64) -> Either String [Text]
getParams = (Int64 -> Either String Text) -> [Int64] -> Either String [Text]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> SymbolRef -> Either String Text
getSymbol Symbols
s (SymbolRef -> Either String Text)
-> (Int64 -> SymbolRef) -> Int64 -> Either String Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> SymbolRef
SymbolRef) ([Int64] -> Either String [Text])
-> (Repeated 1 (Value Int64) -> [Int64])
-> Repeated 1 (Value Int64)
-> Either String [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Repeated 1 (Value Int64) -> [Int64]
Repeated 1 (Value Int64) -> FieldType (Repeated 1 (Value Int64))
forall a. HasField a => a -> FieldType a
PB.getField
      getOps :: Repeated 2 (Message Op) -> Either String [Op]
getOps = (Op -> Either String Op) -> [Op] -> Either String [Op]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Symbols -> Op -> Either String Op
pbToOp Symbols
s) ([Op] -> Either String [Op])
-> (Repeated 2 (Message Op) -> [Op])
-> Repeated 2 (Message Op)
-> Either String [Op]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Repeated 2 (Message Op) -> [Op]
Repeated 2 (Message Op) -> FieldType (Repeated 2 (Message Op))
forall a. HasField a => a -> FieldType a
PB.getField
   in (Repeated 1 (Value Int64) -> Either String [Text])
-> (Repeated 2 (Message Op) -> Either String [Op])
-> (Repeated 1 (Value Int64), Repeated 2 (Message Op))
-> Either String ([Text], [Op])
forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> (a, b) -> f (c, d)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse Repeated 1 (Value Int64) -> Either String [Text]
getParams Repeated 2 (Message Op) -> Either String [Op]
getOps (Repeated 1 (Value Int64)
params, Repeated 2 (Message Op)
ops)

closureToPb :: ReverseSymbols -> [T.Text] -> [Op] -> PB.OpClosure
closureToPb :: ReverseSymbols -> [Text] -> [Op] -> OpClosure
closureToPb ReverseSymbols
s [Text]
params' [Op]
ops' =
  let params :: Repeated 1 (Value Int64)
params = FieldType (Repeated 1 (Value Int64)) -> Repeated 1 (Value Int64)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 1 (Value Int64)) -> Repeated 1 (Value Int64))
-> FieldType (Repeated 1 (Value Int64)) -> Repeated 1 (Value Int64)
forall a b. (a -> b) -> a -> b
$ (Text -> Int64) -> [Text] -> [Int64]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SymbolRef -> Int64
getSymbolRef (SymbolRef -> Int64) -> (Text -> SymbolRef) -> Text -> Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s) [Text]
params'
      ops :: Repeated 2 (Message Op)
ops = FieldType (Repeated 2 (Message Op)) -> Repeated 2 (Message Op)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Repeated 2 (Message Op)) -> Repeated 2 (Message Op))
-> FieldType (Repeated 2 (Message Op)) -> Repeated 2 (Message Op)
forall a b. (a -> b) -> a -> b
$ (Op -> Op) -> [Op] -> [Op]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ReverseSymbols -> Op -> Op
opToPb ReverseSymbols
s) [Op]
ops'
   in PB.OpClosure{Repeated 1 (Value Int64)
Repeated 2 (Message Op)
$sel:params:OpClosure :: Repeated 1 (Value Int64)
$sel:ops:OpClosure :: Repeated 2 (Message Op)
params :: Repeated 1 (Value Int64)
ops :: Repeated 2 (Message Op)
..}

binaryToPb :: ReverseSymbols -> Binary -> PB.OpBinary
binaryToPb :: ReverseSymbols -> Binary -> OpBinary
binaryToPb ReverseSymbols
s = \case
  Binary
LessThan       -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.LessThan, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
GreaterThan    -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.GreaterThan, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
LessOrEqual    -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.LessOrEqual, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
GreaterOrEqual -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.GreaterOrEqual, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Equal          -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Equal, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Contains       -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Contains, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Prefix         -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Prefix, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Suffix         -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Suffix, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Regex          -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Regex, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Add            -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Add, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Sub            -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Sub, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Mul            -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Mul, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Div            -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Div, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
And            -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.And, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Or             -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Or, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Intersection   -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Intersection, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Union          -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Union, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
BitwiseAnd     -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.BitwiseAnd, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
BitwiseOr      -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.BitwiseOr, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
BitwiseXor     -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.BitwiseXor, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
NotEqual       -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.NotEqual, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
HeterogeneousEqual -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.HeterogeneousEqual, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
HeterogeneousNotEqual -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.HeterogeneousNotEqual, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
LazyAnd ->PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.LazyAnd, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
LazyOr -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.LazyOr, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Any -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Any, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
All -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.All, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Get -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.Get, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  Binary
Try -> PB.OpBinary { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.TryOr, $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe Int64
FieldType (Field 2 (OptionalField (Last (Value Int64))))
forall a. Maybe a
Nothing }
  BinaryFfi Text
n -> PB.OpBinary
    { $sel:kind:OpBinary :: Required 1 (Enumeration BinaryKind)
kind = FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
-> Field 1 (RequiredField (Always (Enumeration BinaryKind)))
forall a. HasField a => FieldType a -> a
PB.putField FieldType
  (Field 1 (RequiredField (Always (Enumeration BinaryKind))))
BinaryKind
PB.BinaryFfi
    , $sel:ffiName:OpBinary :: Optional 2 (Value Int64)
ffiName = Maybe Int64 -> Optional 2 (Value Int64)
FieldType (Field 2 (OptionalField (Last (Value Int64))))
-> Field 2 (OptionalField (Last (Value Int64)))
forall a. HasField a => FieldType a -> a
PB.putField (Maybe Int64 -> Optional 2 (Value Int64))
-> (SymbolRef -> Maybe Int64)
-> SymbolRef
-> Optional 2 (Value Int64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Maybe Int64
forall a. a -> Maybe a
Just (Int64 -> Maybe Int64)
-> (SymbolRef -> Int64) -> SymbolRef -> Maybe Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SymbolRef -> Int64
getSymbolRef (SymbolRef -> Optional 2 (Value Int64))
-> SymbolRef -> Optional 2 (Value Int64)
forall a b. (a -> b) -> a -> b
$ ReverseSymbols -> Text -> SymbolRef
getSymbolCode ReverseSymbols
s Text
n
    }

pbToThirdPartyBlockRequest :: PB.ThirdPartyBlockRequest -> Either String Crypto.Signature
pbToThirdPartyBlockRequest :: ThirdPartyBlockRequest -> Either String Signature
pbToThirdPartyBlockRequest PB.ThirdPartyBlockRequest{Optional 1 (Message PublicKey)
legacyPk :: Optional 1 (Message PublicKey)
$sel:legacyPk:ThirdPartyBlockRequest :: ThirdPartyBlockRequest -> Optional 1 (Message PublicKey)
legacyPk, Repeated 2 (Message PublicKey)
pkTable :: Repeated 2 (Message PublicKey)
$sel:pkTable:ThirdPartyBlockRequest :: ThirdPartyBlockRequest -> Repeated 2 (Message PublicKey)
pkTable, Required 3 (Value ByteString)
prevSig :: Required 3 (Value ByteString)
$sel:prevSig:ThirdPartyBlockRequest :: ThirdPartyBlockRequest -> Required 3 (Value ByteString)
prevSig} = do
  Bool -> Either String () -> Either String ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Maybe PublicKey -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe PublicKey -> Bool) -> Maybe PublicKey -> Bool
forall a b. (a -> b) -> a -> b
$ Field 1 (OptionalField (Maybe (Message PublicKey)))
-> FieldType (Field 1 (OptionalField (Maybe (Message PublicKey))))
forall a. HasField a => a -> FieldType a
PB.getField Optional 1 (Message PublicKey)
Field 1 (OptionalField (Maybe (Message PublicKey)))
legacyPk) (Either String () -> Either String ())
-> Either String () -> Either String ()
forall a b. (a -> b) -> a -> b
$ String -> Either String ()
forall a b. a -> Either a b
Left String
"Public key provided in third-party block request"
  Bool -> Either String () -> Either String ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([PublicKey] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([PublicKey] -> Bool) -> [PublicKey] -> Bool
forall a b. (a -> b) -> a -> b
$ Repeated 2 (Message PublicKey)
-> FieldType (Repeated 2 (Message PublicKey))
forall a. HasField a => a -> FieldType a
PB.getField Repeated 2 (Message PublicKey)
pkTable) (Either String () -> Either String ())
-> Either String () -> Either String ()
forall a b. (a -> b) -> a -> b
$ String -> Either String ()
forall a b. a -> Either a b
Left String
"Public key table provided in third-party block request"
  Signature -> Either String Signature
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Signature -> Either String Signature)
-> (ByteString -> Signature)
-> ByteString
-> Either String Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Signature
Crypto.signature (ByteString -> Either String Signature)
-> ByteString -> Either String Signature
forall a b. (a -> b) -> a -> b
$ Field 3 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 3 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 3 (Value ByteString)
Field 3 (RequiredField (Always (Value ByteString)))
prevSig

thirdPartyBlockRequestToPb :: Crypto.Signature -> PB.ThirdPartyBlockRequest
thirdPartyBlockRequestToPb :: Signature -> ThirdPartyBlockRequest
thirdPartyBlockRequestToPb Signature
prevSig = PB.ThirdPartyBlockRequest
  { $sel:legacyPk:ThirdPartyBlockRequest :: Optional 1 (Message PublicKey)
legacyPk = FieldType (Field 1 (OptionalField (Maybe (Message PublicKey))))
-> Field 1 (OptionalField (Maybe (Message PublicKey)))
forall a. HasField a => FieldType a -> a
PB.putField Maybe PublicKey
FieldType (Field 1 (OptionalField (Maybe (Message PublicKey))))
forall a. Maybe a
Nothing
  , $sel:pkTable:ThirdPartyBlockRequest :: Repeated 2 (Message PublicKey)
pkTable = FieldType (Repeated 2 (Message PublicKey))
-> Repeated 2 (Message PublicKey)
forall a. HasField a => FieldType a -> a
PB.putField []
  , $sel:prevSig:ThirdPartyBlockRequest :: Required 3 (Value ByteString)
prevSig = FieldType (Required 3 (Value ByteString))
-> Required 3 (Value ByteString)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 3 (Value ByteString))
 -> Required 3 (Value ByteString))
-> FieldType (Required 3 (Value ByteString))
-> Required 3 (Value ByteString)
forall a b. (a -> b) -> a -> b
$ Signature -> ByteString
Crypto.sigBytes Signature
prevSig
  }

pbToThirdPartyBlockContents :: PB.ThirdPartyBlockContents -> Either String (ByteString, Crypto.Signature, Crypto.PublicKey)
pbToThirdPartyBlockContents :: ThirdPartyBlockContents
-> Either String (ByteString, Signature, PublicKey)
pbToThirdPartyBlockContents PB.ThirdPartyBlockContents{Required 1 (Value ByteString)
payload :: Required 1 (Value ByteString)
$sel:payload:ThirdPartyBlockContents :: ThirdPartyBlockContents -> Required 1 (Value ByteString)
payload,Required 2 (Message ExternalSig)
externalSig :: Required 2 (Message ExternalSig)
$sel:externalSig:ThirdPartyBlockContents :: ThirdPartyBlockContents -> Required 2 (Message ExternalSig)
externalSig} = do
  (Signature
sig, PublicKey
pk) <- ExternalSig -> Either String (Signature, PublicKey)
pbToOptionalSignature (ExternalSig -> Either String (Signature, PublicKey))
-> ExternalSig -> Either String (Signature, PublicKey)
forall a b. (a -> b) -> a -> b
$ Field 2 (RequiredField (Always (Message ExternalSig)))
-> FieldType
     (Field 2 (RequiredField (Always (Message ExternalSig))))
forall a. HasField a => a -> FieldType a
PB.getField Required 2 (Message ExternalSig)
Field 2 (RequiredField (Always (Message ExternalSig)))
externalSig
  (ByteString, Signature, PublicKey)
-> Either String (ByteString, Signature, PublicKey)
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ( Field 1 (RequiredField (Always (Value ByteString)))
-> FieldType (Field 1 (RequiredField (Always (Value ByteString))))
forall a. HasField a => a -> FieldType a
PB.getField Required 1 (Value ByteString)
Field 1 (RequiredField (Always (Value ByteString)))
payload
       , Signature
sig
       , PublicKey
pk
       )

thirdPartyBlockContentsToPb :: (ByteString, Crypto.Signature, Crypto.PublicKey) -> PB.ThirdPartyBlockContents
thirdPartyBlockContentsToPb :: (ByteString, Signature, PublicKey) -> ThirdPartyBlockContents
thirdPartyBlockContentsToPb (ByteString
payload, Signature
sig, PublicKey
pk) = PB.ThirdPartyBlockContents
  { $sel:payload:ThirdPartyBlockContents :: Required 1 (Value ByteString)
PB.payload = FieldType (Field 1 (RequiredField (Always (Value ByteString))))
-> Field 1 (RequiredField (Always (Value ByteString)))
forall a. HasField a => FieldType a -> a
PB.putField ByteString
FieldType (Field 1 (RequiredField (Always (Value ByteString))))
payload
  , $sel:externalSig:ThirdPartyBlockContents :: Required 2 (Message ExternalSig)
PB.externalSig = FieldType (Required 2 (Message ExternalSig))
-> Required 2 (Message ExternalSig)
forall a. HasField a => FieldType a -> a
PB.putField (FieldType (Required 2 (Message ExternalSig))
 -> Required 2 (Message ExternalSig))
-> FieldType (Required 2 (Message ExternalSig))
-> Required 2 (Message ExternalSig)
forall a b. (a -> b) -> a -> b
$ (Signature, PublicKey) -> ExternalSig
externalSigToPb (Signature
sig, PublicKey
pk)
  }