X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FPicklers.hs;h=3135d6ae194d483f3704755d46063cc513393808;hb=f0425854304197ab5ad47293b27b2e0b188cb844;hp=09c3a3b1000ac983251108afb5e570bdb40eb8c5;hpb=6eb1c7477c2d4d3cace6d1b865a5efbec21300a7;p=dead%2Fhtsn-import.git diff --git a/src/TSN/Picklers.hs b/src/TSN/Picklers.hs index 09c3a3b..3135d6a 100644 --- a/src/TSN/Picklers.hs +++ b/src/TSN/Picklers.hs @@ -1,9 +1,12 @@ +{-# LANGUAGE ScopedTypeVariables #-} + -- | (Un)picklers for data types present in The Sports Network XML -- feed. -- module TSN.Picklers ( pickler_tests, xp_ambiguous_time, + xp_attr_option, xp_date, xp_date_padded, xp_datetime, @@ -270,8 +273,8 @@ xp_datetime = -- -- >>> import Data.Maybe ( fromJust ) -- >>> :{ --- let parse_date :: String -> Maybe UTCTime; --- parse_date = parseTime defaultTimeLocale date_format; +-- let parse_date :: String -> Maybe UTCTime +-- parse_date = parseTime defaultTimeLocale date_format -- :} -- -- >>> let dates = [ "1/" ++ (d : "/1970") | d <- ['1'..'9'] ] @@ -474,9 +477,12 @@ xp_time_stamp :: PU UTCTime xp_time_stamp = (parse_time_stamp, from_time_stamp) `xpWrapMaybe` xpText where + -- | We have to re-pad the time_stamp_format with a leading and + -- trailing space; see the documentation of 'time_stamp_format' + -- for more information. from_time_stamp :: UTCTime -> String from_time_stamp = - formatTime defaultTimeLocale time_stamp_format + formatTime defaultTimeLocale (" " ++ time_stamp_format ++ " ") @@ -566,6 +572,23 @@ xp_early_line_date = fmt = "%A, %B %-d" ++ upper_suffix ++ " (" ++ date_format_padded ++ ")" +-- | This is a replacement for @xpOption xpFoo@ within an 'xpAttr'. +-- There's a bug in +-- newer versions of HXT that prevents us from using the usual +-- 'xpOption' solution, so this is our stopgap. It should work on +-- any type that can be unpickled with a plain read/show. +-- +xp_attr_option :: forall a. (Read a, Show a) => PU (Maybe a) +xp_attr_option = + (to_a, from_a) `xpWrap` xpText + where + to_a :: String -> Maybe a + to_a = readMaybe + + from_a :: Maybe a -> String + from_a Nothing = "" + from_a (Just x) = show x + -- | Create an 'XmlTree' containing only the given text. This is -- useful for testing (un)picklers, where we don't want to have to