Skip to content

Instantly share code, notes, and snippets.

@tlaitinen
Last active July 29, 2020 12:35
Show Gist options
  • Save tlaitinen/5487076 to your computer and use it in GitHub Desktop.
Save tlaitinen/5487076 to your computer and use it in GitHub Desktop.
Additional Web.PathPieces.PathPiece instances to deserialize Int32, Word32, Word64, Double, Bool, TimeOfDay, UTCTime and ZonedTime
safeRead :: forall a. Read a => Text -> Maybe a
safeRead s = case (reads $ T.unpack s) of
[(v,_)] -> Just v
_ -> Nothing
instance PathPiece Int32 where
fromPathPiece s =
case Data.Text.Read.decimal s of
Right (i, _) -> Just i
Left _ -> Nothing
toPathPiece = T.pack . show
instance PathPiece Word32 where
fromPathPiece s =
case Data.Text.Read.decimal s of
Right (i, _) -> Just i
Left _ -> Nothing
toPathPiece = T.pack . show
instance PathPiece Word64 where
fromPathPiece s =
case Data.Text.Read.decimal s of
Right (i, _) -> Just i
Left _ -> Nothing
toPathPiece = T.pack . show
instance PathPiece Double where
fromPathPiece s =
case Data.Text.Read.double s of
Right (i, _) -> Just i
Left _ -> Nothing
toPathPiece = T.pack . show
instance PathPiece Bool where
fromPathPiece "true" = Just True
fromPathPiece "false" = Just False
fromPathPiece "True" = Just True
fromPathPiece "False" = Just False
fromPathPiece _ = Nothing
toPathPiece = T.pack . show
instance PathPiece TimeOfDay where
fromPathPiece = safeRead
toPathPiece = T.pack . show
instance PathPiece UTCTime where
fromPathPiece = safeRead
toPathPiece = T.pack . show
instance PathPiece ZonedTime where
fromPathPiece = safeRead
toPathPiece = T.pack . show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment