]> gitweb.michael.orlitzky.com - dead/htsn.git/blobdiff - src/Main.hs
Return an Either from parse_xmlfid instead of a Maybe for better error reporting.
[dead/htsn.git] / src / Main.hs
index f36e2ba502d743e2d9d9f2665d366df385730a16..4ebada07e858fb53d2ca983503ced14201a1fd22 100644 (file)
@@ -55,29 +55,45 @@ import TSN.FeedHosts ( FeedHosts(..) )
 import TSN.Xml ( parse_xmlfid )
 
 
--- | Warning! This does not automatically append a newline. The output
--- is displayed/logged as-is, for, you know, debug purposes.
+-- | Display and log debug information. WARNING! This does not
+--   automatically append a newline. The output is displayed/logged
+--   as-is, for, you know, debug purposes.
 report_debug :: String -> IO ()
 report_debug s = do
   display_debug s
   log_debug s
 
+
+-- | Display and log an error condition. This will prefix the error
+--   with "ERROR: " when displaying (but not logging) it so that it
+--   stands out.
+--
 report_error :: String -> IO ()
 report_error s = do
   display_error $ "ERROR: " ++ s
   log_error s
 
+
+-- | Display and log an informational (status) message.
 report_info :: String -> IO ()
 report_info s = do
   display_info s
   log_info s
 
--- | Warning! This does not automatically append a newline.
+
+-- | A special case of report_debug for reporting the two bits of data
+--   that we sent to TSN: the username and password.
+--
 report_sent :: String -> IO ()
 report_sent s = do
   display_sent s
   log_debug s
 
+
+-- | Display and log a warning. This will prefix the warning with
+--   "WARNING: " when displaying (but not logging) it so that it
+--   stands out.
+--
 report_warning :: String -> IO ()
 report_warning s = do
   display_warning $ "WARNING: " ++ s
@@ -103,10 +119,9 @@ recv_line h = do
 --
 save_document :: Configuration -> String -> IO ()
 save_document cfg doc =
-  case maybe_path of
-    Nothing ->
-      report_error "Document missing XML_File_ID element."
-    Just path -> do
+  case either_path of
+    Left err -> report_error err
+    Right path -> do
       already_exists <- doesFileExist path
       when already_exists $ do
         let msg = "File " ++ path ++ " already exists, overwriting."
@@ -114,9 +129,10 @@ save_document cfg doc =
       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
-    maybe_path = fmap ((output_directory cfg) </>) filename
+    either_path = fmap ((output_directory cfg) </>) filename
 
 
 -- | Loop forever, writing the buffer to file whenever a </message>