module Data.Convertible.Utils(boundedConversion,
convertVia
)
where
import Data.Convertible.Base
import Data.Typeable
boundedConversion :: (Bounded b, Show a, Show b, Convertible a Integer,
Convertible b Integer,
Typeable a, Typeable b) =>
(a -> ConvertResult b)
-> a
-> ConvertResult b
boundedConversion :: (a -> ConvertResult b) -> a -> ConvertResult b
boundedConversion a -> ConvertResult b
func a
inp =
do b
result <- a -> ConvertResult b
func a
inp
let smallest :: b
smallest = b -> b -> b
forall a. a -> a -> a
asTypeOf b
forall a. Bounded a => a
minBound b
result
let biggest :: b
biggest = b -> b -> b
forall a. a -> a -> a
asTypeOf b
forall a. Bounded a => a
maxBound b
result
let smallest' :: Integer
smallest' = b -> Integer
forall a b. Convertible a b => a -> b
convert b
smallest :: Integer
let biggest' :: Integer
biggest' = b -> Integer
forall a b. Convertible a b => a -> b
convert b
biggest :: Integer
let inp' :: Integer
inp' = a -> Integer
forall a b. Convertible a b => a -> b
convert a
inp :: Integer
if Integer
inp' Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
smallest' Bool -> Bool -> Bool
|| Integer
inp' Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
biggest'
then String -> a -> ConvertResult b
forall a b.
(Show a, Typeable a, Typeable b) =>
String -> a -> ConvertResult b
convError (String
"Input value outside of bounds: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (b, b) -> String
forall a. Show a => a -> String
show (b
smallest, b
biggest))
a
inp
else b -> ConvertResult b
forall (m :: * -> *) a. Monad m => a -> m a
return b
result
convertVia :: (Convertible a b, Convertible b c) =>
b
-> a
-> ConvertResult c
convertVia :: b -> a -> ConvertResult c
convertVia b
dummy a
inp =
do b
r1 <- a -> ConvertResult b
forall a b. Convertible a b => a -> ConvertResult b
safeConvert a
inp
b -> ConvertResult c
forall a b. Convertible a b => a -> ConvertResult b
safeConvert (b -> b -> b
forall a. a -> a -> a
asTypeOf b
r1 b
dummy)