- tr :: (MIMEType, [String]) -> [ (String, MIMEType) ]
- tr (mime, exts) = [ (ext, mime) | ext <- exts ]
-
-
-outputExtMapAsHS :: ExtMap -> FilePath -> IO ()
-outputExtMapAsHS extMap fpath
- = let hsModule = HsModule undefined modName (Just exports) imports decls
- modName = Module "Network.HTTP.Lucu.MIMEType.DefaultExtensionMap"
- exports = [HsEVar (UnQual (HsIdent "defaultExtensionMap"))]
- imports = [ HsImportDecl undefined (Module "Network.HTTP.Lucu.MIMEType") False Nothing Nothing
- , HsImportDecl undefined (Module "Data.Map") True (Just (Module "M")) Nothing
- , HsImportDecl undefined (Module "Data.Map") False Nothing (Just (False, [HsIAbs (HsIdent "Map")]))
- ]
- decls = [ HsTypeSig undefined [HsIdent "defaultExtensionMap"]
- (HsQualType [] (HsTyApp (HsTyApp (HsTyCon (UnQual (HsIdent "Map")))
- (HsTyCon (UnQual (HsIdent "String"))))
- (HsTyCon (UnQual (HsIdent "MIMEType")))))
- , HsFunBind [HsMatch undefined (HsIdent "defaultExtensionMap")
- [] (HsUnGuardedRhs extMapExp) []]
- ]
- extMapExp = HsApp (HsVar (Qual (Module "M") (HsIdent "fromList"))) (HsList records)
- comment = "{- !!! WARNING !!!\n"
- ++ " This file is automatically generated from data/mime.types.\n"
- ++ " DO NOT EDIT BY HAND OR YOU WILL REGRET -}\n\n"
+ tr ∷ (v, [k]) → [(k, v)]
+ tr (v, ks) = [(k, v) | k ← ks]
+
+ go ∷ Ord k ⇒ Map k v → [(k, v)] → Either (k, v, v) (Map k v)
+ go m [] = Right m
+ go m ((k, v):xs)
+ = case M.insertLookupWithKey' f k v m of
+ (Nothing, m') → go m' xs
+ (Just v0, _ ) → Left (k, v0, v)
+
+ f ∷ k → v → v → v
+ f _ _ = id
+
+-- |@'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 a serialization of
+-- @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
+ ]
+ 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