summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2012-08-09 07:52:39 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2012-08-09 07:52:39 -0700
commit10cdd59fa8664caf5ac73dc06ee7076a4bbe69bd (patch)
tree34976c4cc39da3646399f30f84d500b45944a295
parent244dae8da8d4bfbf3d5d7e22eacba6092789d841 (diff)
Added safeRead to Text.Pandoc.Shared.
-rw-r--r--src/Text/Pandoc/Shared.hs13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index f3ec43a06..9f615867c 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -65,7 +65,9 @@ module Text.Pandoc.Shared (
readDataFile,
-- * Error handling
err,
- warn
+ warn,
+ -- * Safe read
+ safeRead
) where
import Text.Pandoc.Definition
@@ -489,3 +491,12 @@ warn :: String -> IO ()
warn msg = do
name <- getProgName
UTF8.hPutStrLn stderr $ name ++ ": " ++ msg
+
+--
+-- Safe read
+--
+
+safeRead :: (Monad m, Read a) => String -> m a
+safeRead s = case reads s of
+ (d,[]):_ -> return d
+ _ -> fail $ "Could not read `" ++ s ++ "'"