-- SPDX-FileCopyrightText: 2020 Tocqueville Group
--
-- SPDX-License-Identifier: LicenseRef-MIT-TQ

-- | Packing utilities.
module Lorentz.Pack
  ( lPackValue
  , lUnpackValue
  , lEncodeValue
  ) where

import Data.Constraint ((\\))

import Lorentz.Constraints
import Michelson.Interpret.Pack
import Michelson.Interpret.Unpack
import Michelson.Typed

lPackValue
  :: forall a.
     (NicePackedValue a)
  => a -> ByteString
lPackValue :: a -> ByteString
lPackValue =
  Value (ToT a) -> ByteString
forall (t :: T). PackedValScope t => Value t -> ByteString
packValue' (Value (ToT a) -> ByteString)
-> (a -> Value (ToT a)) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value (ToT a)
forall a. IsoValue a => a -> Value (ToT a)
toVal ((SingI (ToT a), HasNoOp (ToT a), HasNoBigMap (ToT a)) =>
 a -> ByteString)
-> ((KnownValue a,
     (SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a)),
      FailOnBigMapFound (ContainsBigMap (ToT a))))
    :- (SingI (ToT a), HasNoOp (ToT a), HasNoBigMap (ToT a)))
-> a
-> ByteString
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ (KnownValue a,
 (SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a)),
  FailOnBigMapFound (ContainsBigMap (ToT a))))
:- (SingI (ToT a), HasNoOp (ToT a), HasNoBigMap (ToT a))
forall a. NicePackedValue a :- PackedValScope (ToT a)
nicePackedValueEvi @a

lUnpackValue
  :: forall a.
     (NiceUnpackedValue a)
  => ByteString -> Either UnpackError a
lUnpackValue :: ByteString -> Either UnpackError a
lUnpackValue =
  (Value (ToT a) -> a)
-> Either UnpackError (Value (ToT a)) -> Either UnpackError a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Value (ToT a) -> a
forall a. IsoValue a => Value (ToT a) -> a
fromVal (Either UnpackError (Value (ToT a)) -> Either UnpackError a)
-> (ByteString -> Either UnpackError (Value (ToT a)))
-> ByteString
-> Either UnpackError a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnpackError (Value (ToT a))
forall (t :: T).
UnpackedValScope t =>
ByteString -> Either UnpackError (Value t)
unpackValue' ((PackedValScope (ToT a), ConstantScope (ToT a)) =>
 ByteString -> Either UnpackError a)
-> ((KnownValue a,
     ((SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a)),
       FailOnBigMapFound (ContainsBigMap (ToT a))),
      (SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a)),
       FailOnBigMapFound (ContainsBigMap (ToT a)),
       FailOnContractFound (ContainsContract (ToT a)))))
    :- (PackedValScope (ToT a), ConstantScope (ToT a)))
-> ByteString
-> Either UnpackError a
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ (KnownValue a,
 ((SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a)),
   FailOnBigMapFound (ContainsBigMap (ToT a))),
  (SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a)),
   FailOnBigMapFound (ContainsBigMap (ToT a)),
   FailOnContractFound (ContainsContract (ToT a)))))
:- (PackedValScope (ToT a), ConstantScope (ToT a))
forall a. NiceUnpackedValue a :- UnpackedValScope (ToT a)
niceUnpackedValueEvi @a

lEncodeValue
  :: forall a. (NicePrintedValue a)
  => a -> ByteString
lEncodeValue :: a -> ByteString
lEncodeValue = Value (ToT a) -> ByteString
forall (t :: T). (SingI t, HasNoOp t) => Value t -> ByteString
encodeValue' (Value (ToT a) -> ByteString)
-> (a -> Value (ToT a)) -> a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value (ToT a)
forall a. IsoValue a => a -> Value (ToT a)
toVal ((SingI (ToT a), HasNoOp (ToT a)) => a -> ByteString)
-> ((KnownValue a,
     (SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a))))
    :- (SingI (ToT a), HasNoOp (ToT a)))
-> a
-> ByteString
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ (KnownValue a,
 (SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a))))
:- (SingI (ToT a), HasNoOp (ToT a))
forall a. NicePrintedValue a :- PrintedValScope (ToT a)
nicePrintedValueEvi @a