{-# LANGUAGE FlexibleContexts #-}
module Octane.Utility.Embed
( decodeBimap
, decodeMap
, decodeSet
) where
import Data.Function ((&))
import qualified Data.Aeson as Aeson
import qualified Data.Bimap as Bimap
import qualified Data.ByteString as StrictBytes
import qualified Data.Map.Strict as Map
import qualified Data.Maybe as Maybe
import qualified Data.Set as Set
decodeBimap
:: (Aeson.FromJSON (Map.Map b a), Ord a, Ord b)
=> StrictBytes.ByteString -> Bimap.Bimap a b
decodeBimap bytes =
bytes & Aeson.decodeStrict & Maybe.fromMaybe Map.empty & Map.toList &
Bimap.fromList &
Bimap.twist
decodeMap
:: (Aeson.FromJSON (Map.Map a b))
=> StrictBytes.ByteString -> Map.Map a b
decodeMap bytes = bytes & Aeson.decodeStrict & Maybe.fromMaybe Map.empty
decodeSet
:: (Aeson.FromJSON a, Ord a)
=> StrictBytes.ByteString -> Set.Set a
decodeSet bytes = bytes & Aeson.decodeStrict & Maybe.fromMaybe Set.empty