22
\$\begingroup\$

In the game Factorio there are string-encoded representations of blueprints that I wish to decode.

The implementation according to their wiki is the following:

This is a technical description of the blueprint string format, used to share blueprints with other users.

A blueprint string is a json representation of the blueprint, encoded using base 64 with the version byte in front (0 in vanilla 0.15) and then compressed with zlib deflate. So to get the json representation of a blueprint from a blueprint string, skip the first byte, base64 decode the string, and finally decompress using zlib deflate.

Please keep in mind that I am very new to Haskell, having only played around with it maybe once earlier in my life, though I do have some functional programming experience.

The list of dependencies is the following:

  • base >= 4.7 && < 5
  • bytestring
  • base64-bytestring
  • zlib

The Main.hs file:

module Main where

import Lib
import System.Environment

main :: IO ()
main = do
    args <- getArgs
    if null args
        then putStrLn "Usage: factorio-exe <blueprint-string>"
        else putStrLn (blueprintJson (head args))

The Lib.hs file:

module Lib
    ( blueprintJson
    ) where

import Codec.Compression.Zlib
import qualified Data.ByteString.Char8 as C
import qualified Data.ByteString.Lazy.Char8 as L
import Data.ByteString.Base64

blueprintJson :: [Char] -> [Char]
blueprintJson str = bpDeflate (bpDecode str)

bpDecode :: [Char] -> C.ByteString
bpDecode str = let (Right decoded) = decode (C.pack (tail str)) in decoded

bpDeflate :: C.ByteString -> [Char]
bpDeflate bstr = L.unpack (decompress (L.fromStrict bstr))

An example blueprint string to test with is the following:

0eNrtXOtu2zYUfpVCPzcroHjRxSgG9BXa/imGwpBtxiEqS5ou6bIiD7AH2YvtSaZL7MgSKR7KcpwG+xPXrvmJPOc7V5L+Ya2jkqeZiAtr+cMSmyTOreXvP6xc7OIwqj8rHlJuLS1R8L21sOJwX7/LQhFZjwtLxFv+p7V0Hhf6Ick6SZOs6AzD0mH3IivK6pPjyPYb9i7jPO6MJkajM77tjKWPXxcWjwtRCN4uuHnzsIrL/Zpn1YqO41ORcrtIqscnZbytcNMkr4Ylcf3QCsp2nBu2sB6qf7EbVj1iKzK+ab9A6yn2kPEJsgTP68JJAIgOwNcAUPjaXOXSXAkwMxCaEhhLgF3dkpFmyd6QhgMQ/wmhmk5lBkWWRK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mBg5SOwp1YZbMXvur5+dyr+nOriGlkrtUh8DpoMPEyrgvM4OZS0Jd5FQS9oq/gu6R2H826gE2pwDdVBruKMq732xZy8XrjtjDYlCG6Zow2jQ6ACg1MFeq9aoX++kIK9U0DEPTyO5pSrbovWa0eVKFO1ab1GD7OW4jqur+6SnWgMqxUmaO/Ja0+keTNuQEG6nOrF4LhHU1yMn3Nr4M17mtKEPde2M28wixrMRpJ5grsTaN0SiDwXksgsK8T2rV+xsD1swmu37uu63dmcf0fZnX9g6b/oPQITKM3knnQr4vW3JedX9JcWFG45tFw0/qwS3LPs7z1k76HHeYRv2LQ43/aQXQ9

Do note that when decoding that there may appear characters with which the Windows Command Prompt may have issues displaying.

\$\endgroup\$
17
\$\begingroup\$

There's not much to say, to be honest. The type signatures are all there, which is a plus.

Communicate possible errors and make functions total

However, bpDecode has two possible runtime error sources: tail (which is partial) and Right _ = decode, since the latter can return a Left. Therefore bpDecode is partial too. In Haskell, we try to keep the partial functions to a minimum. bpDecode can fail, but it's type does not communicate that to us.

Instead, bpDecode should return Either String C.ByteString as decode. We can use String to communicate another possible error, namely an empty String:

bpDecode :: [Char] -> Either String C.ByteString
bpDecode "" = Left "bpDecode: Empty String"
bpDecode s  = decode . C.pack . tail $ s

Alternatively, if you don't care about empty string case, we can use drop 1 instead of tail. The former always returns a list, even if our original list was empty:

bpDecode :: [Char] -> Either String C.ByteString
bpDecode = decode . C.pack . drop 1

Some libraries provide a "I know it's correct" function and you can too:

bpDecode' :: [Char] -> C.ByteString
bpDecode' s = let (Right d) = bpDecode s in d

but that's an aside. However, now bpDecode is total: for any input it will produce an output.

Use hlint to find possible eta-reductions

While not necessary, this could be a good time to learn about hlint. It will report common improvements on your code. In this case, you can write bpDeflate as

bpDeflate :: C.ByteString -> [Char]
bpDeflate = L.unpack . decompress . L.fromStrict

and blueprintJson as

blueprintJson :: [Char] -> [Char]
blueprintJson = bpDeflate . bpDecode

Try to stay in a single character stream type

Haskell has 5 types that work with strings. ByteString (lazy and strict), Text (lazy and strict) and String (aka [Char]). If possible try to stick to one of them and don't switch between them all the time. For example, all your functions can be written with lazy ByteStrings:

import Codec.Compression.Zlib
import qualified Data.ByteString.Lazy.Char8 as L
import Data.ByteString.Base64.Lazy

blueprintJson :: L.ByteString -> Either String L.ByteString
blueprintJson = fmap bpDeflate . bpDecode

bpDecode :: L.ByteString -> Either String L.ByteString
bpDecode = decode . L.drop 1

bpDeflate :: L.ByteString -> L.ByteString
bpDeflate = decompress

Note that I changed blueprintJson's type, since we also changed bpDecode's type. In case you're not familiar with fmap: in this context you can think of fmap as

fmap f (Right r) = Right (f r)
fmap _ (Left  l) = Left l

Regardless, the string type depends of course on your later use case. Since blueprintJson hints that you will decode JSON, you will likely use aeson later, which uses lazy ByteStrings as well.

Given those functions above, we could now write blueprintJson as

blueprintJson :: L.ByteString -> Either String L.ByteString
blueprintJson = fmap decompress . decode . L.drop 1

blueprintJson' :: L.ByteString -> L.ByteString
blueprintJson' = fromRight . blueprintJson

fromRight :: Either a b -> b
fromRight e = -- left as an exercise

Prefer pattern matches instead of boolean guards

In main, you check whether the list is null. That still enables us to accide

main :: IO ()
main = do
    args <- getArgs
    if null args
        then putStrLn (blueprintJson (head args)) -- woops!
        else putStrLn "Usage: factorio-exe <blueprint-string>"

If you pattern match you cannot accidentally use head on an empty list:

main :: IO ()
main = do
    args <- getArgs
    case args of
      [b] -> putStrLn (blueprintJson b) 
      _   -> putStrLn "Usage: factorio-exe <blueprint-string>"
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.