@@ -1,26 +1,51 @@
1−module Metadata.Libraries (libraries, addPrelude) where
1+module Metadata.Libraries (interfaces, addPrelude) where
22
3−import Control.Applicative ((<$>),(<*>))
43import qualified Data.Map as Map
5−import Data.List (inits)
6−import Text.JSON
7−import Metadata.Load as Load
4+import qualified Control.Exception as E
5+import qualified Paths_Elm as Path
6+import System.Directory
7+import System.Exit
8+import System.FilePath
9+import System.IO.Unsafe (unsafePerformIO)
810import SourceSyntax.Module
11+import Data.Binary
12+
913
1014addPrelude :: Module t v -> Module t v
1115addPrelude (Module name exs ims stmts) = Module name exs (customIms ++ ims) stmts
12− where customIms = concatMap addModule prelude
16+ where
17+ customIms = concatMap addModule prelude
1318
14− addModule (n, method) = case lookup n ims of
15− Nothing -> [(n, method)]
16− Just (As m) -> [(n, method)]
17− Just _ -> []
19+ addModule (n, method) = case lookup n ims of
20+ Nothing -> [(n, method)]
21+ Just (As m) -> [(n, method)]
22+ Just _ -> []
1823
24+prelude :: [(String, ImportMethod)]
1925prelude = text : map (\n -> (n, Hiding [])) modules
2026 where
2127 text = ("Text", Hiding ["link", "color", "height"])
2228 modules = [ "Prelude", "Signal", "List", "Maybe", "Time"
2329 , "Graphics.Element", "Color", "Graphics.Collage" ]
2430
25−libraries :: Map.Map String (Map.Map String String)
26−libraries = Map.empty
31+{-# NOINLINE interfaces #-}
32+interfaces :: Interfaces
33+interfaces =
34+ unsafePerformIO (safeReadDocs =<< Path.getDataFileName "interfaces.data")
35+
36+safeReadDocs :: FilePath -> IO Interfaces
37+safeReadDocs name =
38+ E.catch (readDocs name) $ \err -> do
39+ putStrLn $ unlines [ "Error reading types for standard library!"
40+ , " " ++ show (err :: IOError)
41+ , "The file should be located here: " ++ name
42+ , " If you are using a stable version of Elm,"
43+ , " please report an issue at github.com/evancz/Elm"
44+ , " and specify your versions of Elm and your OS" ]
45+ exitFailure
46+
47+readDocs :: FilePath -> IO Interfaces
48+readDocs name = do
49+ exists <- doesFileExist name
50+ if exists then decodeFile name
51+ else ioError . userError $ "File Not Found"