Permalink
Browse files

Dump original source code on failure

See #10
  • Loading branch information...
1 parent 2fb2c97 commit dafe122baea8c4afb30b7faab399cab080abd5af @jaspervdj committed Jul 31, 2012
Showing with 18 additions and 8 deletions.
  1. +7 −2 src/Main.hs
  2. +11 −6 src/StylishHaskell.hs
View
@@ -11,6 +11,7 @@ import Data.List (intercalate)
import Data.Version (Version(..))
import Prelude hiding (readFile)
import System.Console.CmdArgs
+import System.IO (hPutStrLn, stderr)
import System.IO.Strict (readFile)
@@ -74,7 +75,11 @@ stylishHaskell sa
file :: StylishArgs -> Config -> Maybe FilePath -> IO ()
file sa conf mfp = do
contents <- maybe getContents readFile mfp
- write $ unlines $ runSteps (configLanguageExtensions conf)
- mfp (configSteps conf) $ lines contents
+ let result = runSteps (configLanguageExtensions conf)
+ mfp (configSteps conf) $ lines contents
+
+ case result of
+ Left err -> hPutStrLn stderr err >> write contents
+ Right ok -> write $ unlines ok
where
write = maybe putStr (if inPlace sa then writeFile else const putStr) mfp
View
@@ -6,18 +6,23 @@ module StylishHaskell
--------------------------------------------------------------------------------
+import Control.Applicative ((<$>))
+import Control.Monad (foldM)
+
+
+--------------------------------------------------------------------------------
import StylishHaskell.Config
import StylishHaskell.Parse
import StylishHaskell.Step
--------------------------------------------------------------------------------
-runStep :: Extensions -> Maybe FilePath -> Step -> Lines -> Lines
-runStep exts mfp step ls = case parseModule exts mfp (unlines ls) of
- Left err -> error err -- TODO: maybe return original lines?
- Right module' -> stepFilter step ls module'
+runStep :: Extensions -> Maybe FilePath -> Lines -> Step -> Either String Lines
+runStep exts mfp ls step =
+ stepFilter step ls <$> parseModule exts mfp (unlines ls)
--------------------------------------------------------------------------------
-runSteps :: Extensions -> Maybe FilePath -> [Step] -> Lines -> Lines
-runSteps exts mfp = foldr (flip (.)) id . map (runStep exts mfp)
+runSteps :: Extensions -> Maybe FilePath -> [Step] -> Lines
+ -> Either String Lines
+runSteps exts mfp steps ls = foldM (runStep exts mfp) ls steps

0 comments on commit dafe122

Please sign in to comment.