where
-- System imports.
+import Control.Applicative ( (<$>) )
import Control.Concurrent ( threadDelay )
import Control.Exception ( bracket, throw )
import Control.Monad ( when )
-- This can fail, but we don't purposefully throw any exceptions. If
-- something goes wrong, we would rather log it and keep going.
--
+-- And in fact the only \"error\" that can occur is from
+-- 'parse_xmlfid' if TSN sends us a non-integer XML File ID. But
+-- this is expected from time to time, and is merely unsupported. So
+-- we report any failures as info instead of as errors.
+--
save_document :: Configuration
-> String -- ^ String representation of an XML document
-> IO ()
save_document cfg doc =
case either_path of
- Left err -> report_error err
+ Left err -> report_info err -- Can only be non-integer XML File ID
Right path -> do
already_exists <- doesFileExist path
when already_exists $ do
writeFile path doc
report_info $ "Wrote file: " ++ path ++ "."
where
- -- All the fmaps are because we're working inside a Maybe.
- xmlfid = fmap show (parse_xmlfid doc)
- filename = fmap (++ ".xml") xmlfid
- either_path = fmap ((output_directory cfg) </>) filename
+ -- All the <$> are because we're working inside an Either.
+ xmlfid = show <$> (parse_xmlfid doc)
+ filename = (++ ".xml") <$> xmlfid
+ either_path = ((output_directory cfg) </>) <$> filename
-- | Loop forever, writing the @buffer@ to file whenever a