--- |@'serializeExtMap' extMap moduleName variableName@ generates a
--- Haskell source code which contains the following things:
---
--- * A definition of module named @moduleName@.
---
--- * @variableName :: 'ExtMap'@ whose content is the serialised
--- @extMap@.
---
--- The module "Network.HTTP.Lucu.MIMEType.DefaultExtensionMap" is
--- surely generated using this function.
-serializeExtMap ∷ ExtMap → String → String → String
-serializeExtMap extMap moduleName variableName
- = let hsModule = Module (⊥) (ModuleName moduleName) modPragma
- Nothing (Just exports) imports decls
- modPragma = [ LanguagePragma (⊥) [ name (show OverloadedStrings) ] ]
- exports = [ EVar (UnQual (name variableName)) ]
- imports = [ ImportDecl (⊥) (ModuleName "Network.HTTP.Lucu.MIMEType")
- False False Nothing Nothing Nothing
- , ImportDecl (⊥) (ModuleName "Network.HTTP.Lucu.MIMEType.Guess")
- False False Nothing Nothing Nothing
- , ImportDecl (⊥) (ModuleName "Data.Ascii")
- False False Nothing Nothing (Just (False, []))
- , ImportDecl (⊥) (ModuleName "Data.Map")
- True False Nothing (Just (ModuleName "M")) Nothing
- ]
- decls = [ TypeSig (⊥) [name variableName]
- (TyCon (UnQual (name "ExtMap")))
- , nameBind (⊥) (name variableName) extMapExp
- , InlineSig (⊥) False AlwaysActive (UnQual (name variableName))
- ]
- comment = concat [ "{- !!! WARNING !!!\n"
- , " This file is automatically generated.\n"
- , " DO NOT EDIT BY HAND OR YOU WILL REGRET -}\n\n"
- ]
- extMapExp = qvar (ModuleName "M") (name "fromList") `app` listE records
- in
- comment ⧺ prettyPrint hsModule ⧺ "\n"
- where
- records ∷ [Exp]
- records = map record $ M.assocs extMap
-
- record ∷ (Text, MIMEType) → Exp
- record (ext, mime)
- = tuple [ strE (T.unpack ext)
- , function "parseMIMEType" `app` strE (mimeToString mime)
- ]
-
- mimeToString ∷ MIMEType → String
- mimeToString = A.toString ∘ A.fromAsciiBuilder ∘ printMIMEType