From f0e14e5d638fb4a5c1ca7fd19df8c11071734a82 Mon Sep 17 00:00:00 2001 From: Yvan Sraka Date: Mon, 22 May 2023 20:24:31 +0200 Subject: [PATCH] [WIP] Fix #11: improve parsing --- app/Foliage/Meta.hs | 19 ++++++++++++++++++- foliage.cabal | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/Foliage/Meta.hs b/app/Foliage/Meta.hs index b5642d8..5f6c91d 100644 --- a/app/Foliage/Meta.hs +++ b/app/Foliage/Meta.hs @@ -32,6 +32,8 @@ where import Control.Applicative ((<|>)) import Control.Monad (void) +import Control.Monad.State (modify) +import Data.HashMap.Strict qualified as HashMap import Data.List (sortOn) import Data.Maybe (fromMaybe) import Data.Ord (Down (Down)) @@ -121,10 +123,25 @@ data PackageVersionSpec = PackageVersionSpec deriving (Show, Eq, Generic) deriving anyclass (Binary, Hashable, NFData) +matchMaybe :: forall a. Toml.TomlBiMap a Toml.AnyValue -> Toml.Key -> TomlCodec (Maybe a) +matchMaybe bimap@(Toml.BiMap {}) 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 (Toml.backward bimap anyVal) pure + + output :: Maybe a -> Toml.TomlState (Maybe a) + output Nothing = pure Nothing + output (Just a) = do + anyVal <- Toml.eitherToTomlState $ Toml.forward bimap a + Just a <$ modify (Toml.insertKeyAnyVal key anyVal) + sourceMetaCodec :: TomlCodec PackageVersionSpec sourceMetaCodec = PackageVersionSpec - <$> Toml.dioptional (timeCodec "timestamp") + -- TODO: unsure how to initialize Toml.BiMap ... with Toml._ZonedTime? + <$> matchMaybe Toml.BiMap {} "timestamp" .= packageVersionTimestamp <*> packageSourceCodec .= packageVersionSource diff --git a/foliage.cabal b/foliage.cabal index 8c45823..48641a7 100644 --- a/foliage.cabal +++ b/foliage.cabal @@ -58,6 +58,7 @@ executable foliage ed25519, filepath >=1.4.2.1 && <1.5, hackage-security >=0.6.2.1 && <0.7, + mtl, network-uri ^>=2.6.4.1, optparse-applicative >=0.17.0.0 && <0.18, shake >=0.19.6 && <0.20, @@ -67,5 +68,6 @@ executable foliage time >=1.9.3 && <1.13, time-compat >=1.9.6.1 && <1.10, tomland >=1.3.3.1 && <1.4, + unordered-containers, with-utf8 >=1.0.2.3 && <1.1, zlib >=0.6.2.3 && <0.7