Created
April 27, 2023 07:26
-
-
Save andreabedini/8ab4ff9ecac342353ba70f623e7a9bd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matchMaybe :: forall a. TomlBiMap a AnyValue -> Key -> TomlCodec (Maybe a) | |
matchMaybe Toml.BiMap {forward, backward} key = Toml.Codec input output | |
where | |
input :: Toml.TomlEnv (Maybe a) | |
input toml = case HashMap.lookup key (Toml.tomlPairs toml) of | |
Nothing -> pure Nothing | |
Just anyVal -> pure <$> Toml.whenLeftBiMapError key (backward anyVal) pure | |
output :: Maybe a -> Toml.TomlState (Maybe a) | |
output Nothing = pure Nothing | |
output (Just a) = do | |
anyVal <- Toml.eitherToTomlState $ forward a | |
Just a <$ modify (insertKeyAnyVal key anyVal) | |
-- | |
-- Using match | |
-- | |
-- >>> Toml.decode (match Toml._ZonedTime "time") "" | |
-- Left [KeyNotFound ("time" :| [])] | |
-- | |
-- >>> Toml.decode (match Toml._ZonedTime "time") "time = 1" | |
-- Left [BiMapError ("time" :| []) (WrongValue (MatchError {valueExpected = TZoned, valueActual = Integer 1}))] | |
-- | |
-- >>> Toml.decode (match Toml._ZonedTime "time") "time = \"some text\"" | |
-- Left [BiMapError ("time" :| []) (WrongValue (MatchError {valueExpected = TZoned, valueActual = Text "some text"}))] | |
-- | |
-- >>> Toml.decode (match Toml._ZonedTime "time") "time = '2022-08-22T10:38:45Z'" | |
-- Left [BiMapError ("time" :| []) (WrongValue (MatchError {valueExpected = TZoned, valueActual = Text "2022-08-22T10:38:45Z"}))] | |
-- | |
-- >>> Toml.decode (match Toml._ZonedTime "time") "time = 2022-08-22T10:38:45Z" | |
-- Right 2022-08-22 10:38:45 +0000 | |
-- | |
-- Using match and dioptional | |
-- | |
-- >>> Toml.decode (Toml.dioptional $ match Toml._ZonedTime "time") "" | |
-- Right Nothing | |
-- | |
-- >>> Toml.decode (Toml.dioptional $ match Toml._ZonedTime "time") "time = 1" | |
-- Right Nothing | |
-- | |
-- >>> Toml.decode (Toml.dioptional $ match Toml._ZonedTime "time") "time = \"some text\"" | |
-- Right Nothing | |
-- | |
-- >>> Toml.decode (Toml.dioptional $ match Toml._ZonedTime "time") "time = '2022-08-22T10:38:45Z'" | |
-- Right Nothing | |
-- | |
-- >>> Toml.decode (Toml.dioptional $ match Toml._ZonedTime "time") "time = 2022-08-22T10:38:45Z" | |
-- Right (Just 2022-08-22 10:38:45 +0000) | |
-- | |
-- Using matchMaybe | |
-- | |
-- >>> Toml.decode (matchMaybe Toml._ZonedTime "time") "" | |
-- Right Nothing | |
-- | |
-- >>> Toml.decode (matchMaybe Toml._ZonedTime "time") "time = 1" | |
-- Left [BiMapError ("time" :| []) (WrongValue (MatchError {valueExpected = TZoned, valueActual = Integer 1}))] | |
-- | |
-- >>> Toml.decode (matchMaybe Toml._ZonedTime "time") "time = \"some text\"" | |
-- Left [BiMapError ("time" :| []) (WrongValue (MatchError {valueExpected = TZoned, valueActual = Text "some text"}))] | |
-- | |
-- >>> Toml.decode (matchMaybe Toml._ZonedTime "time") "time = '2022-08-22T10:38:45Z'" | |
-- Left [BiMapError ("time" :| []) (WrongValue (MatchError {valueExpected = TZoned, valueActual = Text "2022-08-22T10:38:45Z"}))] | |
-- | |
-- >>> Toml.decode (matchMaybe Toml._ZonedTime "time") "time = 2022-08-22T10:38:45Z" | |
-- Right (Just 2022-08-22 10:38:45 +0000) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment