module Data.Text.PgpWordlist.Internal.Convert where
import qualified Data.Text.PgpWordlist.Internal.AltList as Alt
import Data.Text.PgpWordlist.Internal.Types
import Data.Text.PgpWordlist.Internal.Words
import Data.Text.PgpWordlist.Internal.Word8Bimap
import qualified Data.ByteString.Lazy as BSL
import Data.Text (Text)
import qualified Data.Text as T
import Data.Word
toText :: BSL.ByteString -> Text
toText = T.intercalate " "
. Alt.toList
. Alt.bimap (unEvenWord . toEvenWord) (unOddWord . toOddWord)
. Alt.fromList
. BSL.unpack
fromText :: Text -> Either TranslationError BSL.ByteString
fromText = fmap (BSL.pack . Alt.toList)
. Alt.bitraverse fromEvenWord fromOddWord
. Alt.fromList
. T.words
toEvenWord :: Word8 -> EvenWord
toEvenWord = lookupL evenMap
toOddWord :: Word8 -> OddWord
toOddWord = lookupL oddMap
fromEvenWord :: Text -> Either TranslationError Word8
fromEvenWord word = case lookupR evenMap (EvenWord word) of
Just i -> Right i
Nothing -> Left (case lookupR oddMap (OddWord word) of
Just j -> BadParity word j
Nothing -> BadWord word)
fromOddWord :: Text -> Either TranslationError Word8
fromOddWord word = case lookupR oddMap (OddWord word) of
Just i -> Right i
Nothing -> Left (case lookupR evenMap (EvenWord word) of
Just j -> BadParity word j
Nothing -> BadWord word)
evenMap :: Word8Bimap EvenWord
evenMap = unsafeConstruct (map pick12 wordList)
where
pick12 :: (a,b,c) -> (a,b)
pick12 (i,e,_) = (i,e)
oddMap :: Word8Bimap OddWord
oddMap = unsafeConstruct (map pick13 wordList)
where
pick13 :: (a,b,c) -> (a,c)
pick13 (i,_,o) = (i,o)