X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fhtsn-import.git;a=blobdiff_plain;f=src%2FTSN%2FPicklers.hs;fp=src%2FTSN%2FPicklers.hs;h=4f0020b6ad167123c10a66224a0b5092f09b8489;hp=3135d6ae194d483f3704755d46063cc513393808;hb=7b33eb87f59d46ec97d619f7521c067d8a6b9308;hpb=3c1e37ee3ead276288ac167b6b8f75f4060ca1b2 diff --git a/src/TSN/Picklers.hs b/src/TSN/Picklers.hs index 3135d6a..4f0020b 100644 --- a/src/TSN/Picklers.hs +++ b/src/TSN/Picklers.hs @@ -14,6 +14,7 @@ module TSN.Picklers ( xp_earnings, xp_fracpart_only_double, xp_gamedate, + xp_tba_int, xp_tba_time, xp_time, xp_time_dots, @@ -451,6 +452,46 @@ xp_tba_time = from_time (Just t) = formatTime defaultTimeLocale time_format t +-- | (Un)pickle a 'Int', allowing for a value of \"TBA\" (which gets +-- translated to 'Nothing'). +-- +-- /Examples/: +-- +-- A failed parse will return 'Nothing': +-- +-- >>> let tn = text_node "YO" +-- >>> unpickleDoc xp_tba_int tn +-- Just Nothing +-- +-- And so will parsing a \"TBA\": +-- +-- >>> let tn = text_node "TBA" +-- >>> unpickleDoc xp_tba_int tn +-- Just Nothing +-- +-- But re-pickling 'Nothing' gives only \"TBA\": +-- +-- >>> pickleDoc xp_tba_int Nothing +-- NTree (XTag "/" []) [NTree (XText "TBA") []] +-- +-- A normal integer is also parsed successfully, of course: +-- +-- >>> let tn = text_node "110" +-- >>> unpickleDoc xp_tba_int tn +-- Just (Just 110) +-- +xp_tba_int :: PU (Maybe Int) +xp_tba_int = + (to_int, from_int) `xpWrap` xpText + where + to_int :: String -> Maybe Int + to_int = readMaybe + + from_int :: Maybe Int -> String + from_int Nothing = "TBA" + from_int (Just t) = show t + + -- | (Un)pickle the \ element format to/from a 'UTCTime'. -- The time_stamp elements look something like,