]> gitweb.michael.orlitzky.com - dead/htsn.git/blobdiff - src/TSN/Xml.hs
Return an Either from parse_xmlfid instead of a Maybe for better error reporting.
[dead/htsn.git] / src / TSN / Xml.hs
index 4c1123f0c71f5aa4b20794031b262a862363ccc7..9d83f58ba448c0a63a048c3329a28ea657b1234a 100644 (file)
@@ -6,7 +6,7 @@ module TSN.Xml (
   xml_tests )
 where
 
-import Data.Maybe ( listToMaybe, mapMaybe )
+import Data.Either.Utils ( maybeToEither )
 import Test.Tasty ( TestTree, testGroup )
 import Test.Tasty.HUnit ( (@?=), Assertion, testCase )
 import Text.Read ( readMaybe )
@@ -19,11 +19,32 @@ import Text.XML.HXT.Core (
   runLA,
   xreadDoc )
 
+
 -- | A tiny parser written in HXT to extract the "XML_File_ID" element
---   from a document.
-parse_xmlfid :: String -> Maybe Integer
-parse_xmlfid =
-  listToMaybe . mapMaybe readMaybe . parse
+--   from a document. If we fail to parse an XML_File_ID, we return
+--   the reason wrapped in a 'Left' constructor. The reason should be
+--   one of two things:
+--
+--     1. No XML_File_ID elements were found.
+--
+--     2. An XML_File_ID element was found, but it could not be read
+--        into an Integer.
+--
+--   We use an Either rather than a Maybe because we do expect some
+--   non-integer XML_File_IDs. In the examples, you will see
+--   NHL_DepthChart_XML.XML with an XML_File_ID of "49618.61" and
+--   CFL_Boxscore_XML1.xml with an XML_File_ID of "R28916". According
+--   to Brijesh Patel of TSN, these are special category files and not
+--   part of the usual feed.
+--
+--   We want to report them differently, "just in case."
+--
+parse_xmlfid :: String -- ^ The XML Document
+             -> Either String Integer
+parse_xmlfid doc =
+  case parse_results of
+    []    -> Left "No XML_File_ID elements found."
+    (x:_) -> x
   where
     parse :: String -> [String]
     parse =
@@ -33,6 +54,14 @@ parse_xmlfid =
                >>> getChildren
                >>> getText)
 
+    read_either_integer :: String -> Either String Integer
+    read_either_integer s =
+      let msg = "Could not parse XML_File_ID" ++ s ++ " as an integer."
+      in
+        maybeToEither msg (readMaybe s)
+
+    elements = parse doc
+    parse_results = map read_either_integer elements
 
 
 -- * Tasty Tests
@@ -53,5 +82,7 @@ xml_file_id_tests =
     check xmlfid = do
       xml <- readFile ("test/xml/" ++ xmlfid ++ ".xml")
       let actual = parse_xmlfid xml
-      let expected = readMaybe xmlfid
+      -- The maybeToEither should always succeed here, so the error
+      -- message goes unused.
+      let expected = maybeToEither "derp" (readMaybe xmlfid)
       actual @?= expected