module Cardano.Wasm.NumberConversion
  ( integerToNatural
  )
where

import Cardano.Wasm.ExceptionHandling (throwError)

import Numeric.Natural (Natural)

integerToNatural :: Integer -> IO Natural
integerToNatural :: Integer -> IO Natural
integerToNatural Integer
i
  | Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = String -> IO Natural
forall (m :: * -> *) a.
(HasCallStack, MonadThrow m) =>
String -> m a
throwError (String -> IO Natural) -> String -> IO Natural
forall a b. (a -> b) -> a -> b
$ String
"Expected natural number, but got a negative value: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
i
  | Bool
otherwise = Natural -> IO Natural
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Natural -> IO Natural) -> Natural -> IO Natural
forall a b. (a -> b) -> a -> b
$ Integer -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i