module Michelson.Interpret.Unpack
( UnpackError (..)
, unpackInstr'
, unpackUValue'
, unpackValue'
) where
import Prelude hiding (EQ, Ordering(..), get)
import qualified Data.ByteString as BS
import Data.Constraint (Dict(..))
import Fmt (pretty)
import Michelson.Typed (UnpackedValScope)
import qualified Michelson.Typed as T
import Michelson.Untyped
import qualified Michelson.Untyped as U
import Morley.Micheline.Binary (eitherDecodeExpression)
import Morley.Micheline.Class (FromExpression(..))
import Util.Binary
unpackValue'
:: forall t. (UnpackedValScope t)
=> ByteString -> Either UnpackError (T.Value t)
unpackValue' :: ByteString -> Either UnpackError (Value t)
unpackValue' = FromExpression (Value t) =>
ByteString -> Either UnpackError (Value t)
forall t. FromExpression t => ByteString -> Either UnpackError t
unpackImpl @(T.Value t)
where
_reallyNeedThisConstraint :: Dict (UnpackedValScope t)
_reallyNeedThisConstraint = UnpackedValScope t => Dict (UnpackedValScope t)
forall (a :: Constraint). a => Dict a
Dict @(UnpackedValScope t)
unpackInstr' :: ByteString -> Either UnpackError [ExpandedOp]
unpackInstr' :: ByteString -> Either UnpackError [ExpandedOp]
unpackInstr' = FromExpression [ExpandedOp] =>
ByteString -> Either UnpackError [ExpandedOp]
forall t. FromExpression t => ByteString -> Either UnpackError t
unpackImpl @([ExpandedOp])
unpackUValue' :: ByteString -> Either UnpackError U.Value
unpackUValue' :: ByteString -> Either UnpackError Value
unpackUValue' = FromExpression Value => ByteString -> Either UnpackError Value
forall t. FromExpression t => ByteString -> Either UnpackError t
unpackImpl @U.Value
unpackImpl :: forall t. (FromExpression t)
=> ByteString
-> Either UnpackError t
unpackImpl :: ByteString -> Either UnpackError t
unpackImpl ByteString
bs = do
(Word8
tag, ByteString
bs') <- UnpackError
-> Maybe (Word8, ByteString)
-> Either UnpackError (Word8, ByteString)
forall l r. l -> Maybe r -> Either l r
maybeToRight (Text -> UnpackError
UnpackError Text
"Empty bytes") (ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs)
Bool -> Either UnpackError () -> Either UnpackError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Word8
tag Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8
0x05) (Either UnpackError () -> Either UnpackError ())
-> (Text -> Either UnpackError ()) -> Text -> Either UnpackError ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnpackError -> Either UnpackError ()
forall a b. a -> Either a b
Left (UnpackError -> Either UnpackError ())
-> (Text -> UnpackError) -> Text -> Either UnpackError ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> UnpackError
UnpackError (Text -> Either UnpackError ()) -> Text -> Either UnpackError ()
forall a b. (a -> b) -> a -> b
$
Text
"Unexpected tag: '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (Word8 -> Text
forall b a. (Show a, IsString b) => a -> b
show Word8
tag) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'. '0x05' tag expected."
Expression
expr <- ByteString -> Either UnpackError Expression
eitherDecodeExpression ByteString
bs'
(FromExpressionError -> Either UnpackError t)
-> (t -> Either UnpackError t)
-> Either FromExpressionError t
-> Either UnpackError t
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (UnpackError -> Either UnpackError t
forall a b. a -> Either a b
Left (UnpackError -> Either UnpackError t)
-> (FromExpressionError -> UnpackError)
-> FromExpressionError
-> Either UnpackError t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> UnpackError
UnpackError (Text -> UnpackError)
-> (FromExpressionError -> Text)
-> FromExpressionError
-> UnpackError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FromExpressionError -> Text
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) t -> Either UnpackError t
forall a b. b -> Either a b
Right (Either FromExpressionError t -> Either UnpackError t)
-> Either FromExpressionError t -> Either UnpackError t
forall a b. (a -> b) -> a -> b
$ Expression -> Either FromExpressionError t
forall a.
FromExpression a =>
Expression -> Either FromExpressionError a
fromExpression @t Expression
expr