clean up read/show abuse

Avoid ever using read to parse a non-haskell formatted input string.

show :: Key is arguably still show abuse, but displaying Keys as filenames
is just too useful to give up.
This commit is contained in:
Joey Hess 2011-11-07 23:21:22 -04:00
parent fdf988be6d
commit b11a63a860
18 changed files with 75 additions and 98 deletions

View file

@ -50,28 +50,27 @@ showLog :: (a -> String) -> Log a -> String
showLog shower = unlines . map showpair . M.toList
where
showpair (k, LogEntry (Date p) v) =
unwords [show k, shower v, tskey ++ show p]
unwords [fromUUID k, shower v, tskey ++ show p]
showpair (k, LogEntry Unknown v) =
unwords [show k, shower v]
unwords [fromUUID k, shower v]
parseLog :: (String -> Maybe a) -> String -> Log a
parseLog parser = M.fromListWith best . catMaybes . map pair . lines
parseLog parser = M.fromListWith best . catMaybes . map parse . lines
where
pair line
parse line
| null ws = Nothing
| otherwise = case parser $ unwords info of
Nothing -> Nothing
Just v -> Just (read u, LogEntry c v)
| otherwise = parser (unwords info) >>= makepair
where
makepair v = Just (toUUID u, LogEntry ts v)
ws = words line
u = head ws
end = last ws
c
ts
| tskey `isPrefixOf` end =
pdate $ tail $ dropWhile (/= '=') end
| otherwise = Unknown
info
| c == Unknown = drop 1 ws
| ts == Unknown = drop 1 ws
| otherwise = drop 1 $ init ws
pdate s = case parseTime defaultTimeLocale "%s%Qs" s of
Nothing -> Unknown