salak-toml 0.2.8 → 0.2.9
raw patch · 5 files changed
+74/−72 lines, 5 filesdep ~salakPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: salak
API changes (from Hackage documentation)
- Salak.Load.Toml: TOML :: TOML
- Salak.Load.Toml: data TOML
- Salak.Load.Toml: instance Salak.HasLoad Salak.Load.Toml.TOML
- Salak.Load.Toml: loadTOML :: Reload -> TOML -> SourcePack -> SourcePack
- Salak.Load.Toml: loadToml :: MonadIO m => FilePath -> SourcePackT m ()
- Salak.Load.Toml: toS :: Piece -> Selector
- Salak.Load.Toml: toSs :: Key -> [Selector]
+ Salak.Toml: TOML :: TOML
+ Salak.Toml: data TOML
+ Salak.Toml: instance Salak.HasLoad Salak.Toml.TOML
+ Salak.Toml: loadToml :: MonadIO m => FilePath -> LoadSalakT m ()
Files
- README.md +1/−1
- salak-toml.cabal +7/−5
- src/Salak/Load/Toml.hs +0/−61
- src/Salak/Toml.hs +63/−0
- test/Spec.hs +3/−5
README.md view
@@ -1,1 +1,1 @@-# salak-yaml+# salak-toml
salak-toml.cabal view
@@ -1,6 +1,6 @@ cabal-version: 1.12 name: salak-toml-version: 0.2.8+version: 0.2.9 license: BSD3 license-file: LICENSE copyright: (c) 2018 Daniel YU@@ -16,15 +16,16 @@ library exposed-modules:- Salak.Load.Toml+ Salak.Toml hs-source-dirs: src other-modules: Paths_salak_toml default-language: Haskell2010+ ghc-options: -Wall -fno-warn-orphans -fno-warn-missing-signatures build-depends: base >=4.10 && <5, mtl >=2.2.2 && <2.3,- salak ==0.2.8,+ salak ==0.2.9, text >=1.2.3.1 && <1.3, time >=1.8.0.2 && <1.9, tomland ==1.0.*,@@ -35,15 +36,16 @@ main-is: Spec.hs hs-source-dirs: test src other-modules:- Salak.Load.Toml+ Salak.Toml Paths_salak_toml default-language: Haskell2010+ ghc-options: -Wall -fno-warn-orphans -fno-warn-missing-signatures build-depends: QuickCheck >=2.12.6.1 && <2.14, base >=4.10 && <5, hspec ==2.*, mtl >=2.2.2 && <2.3,- salak ==0.2.8,+ salak ==0.2.9, text >=1.2.3.1 && <1.3, time >=1.8.0.2 && <1.9, tomland ==1.0.*,
− src/Salak/Load/Toml.hs
@@ -1,61 +0,0 @@-{-# LANGUAGE GADTs #-}-{-# LANGUAGE RecordWildCards #-}-{-# LANGUAGE TupleSections #-}-module Salak.Load.Toml where--import Control.Monad (foldM, (>=>))-import Control.Monad.IO.Class (MonadIO, liftIO)-import Control.Monad.State-import qualified Data.HashMap.Strict as HM-import qualified Data.List.NonEmpty as N-import qualified Data.Text.IO as IO-import Data.Time-import Salak-import Salak.Load-import Toml hiding (TOML)-import qualified Toml as T--data TOML = TOML--instance HasLoad TOML where- loaders _ = (, loadToml) <$> ["toml", "tml"]--toSs :: Key -> [Selector]-toSs (Key ps) = toS <$> N.toList ps--toS :: Piece -> Selector-toS = SStr . unPiece--loadTOML :: Reload -> T.TOML -> SourcePack -> SourcePack-loadTOML name v sp = loadFile name sp $ go v- where- go T.TOML{..} i s = HM.foldlWithKey' (g1 i) (return s) tomlPairs- >>= h2 i tomlTables- g1 i ms k v' = ms >>= updateSources (toSs k) (f1 i v')- g2 i ms _ v' = ms >>= f2 i v'- f1 :: Monad m => Priority -> AnyValue -> Source -> m Source- f1 i (AnyValue (Array b)) = \s -> foldM (\s' (ix,x) -> updateSource (SNum ix) (f1 i $ AnyValue x) s') s $ zip [0..] b- f1 i (AnyValue (Bool b)) = return . insertSource (VBool i b)- f1 i (AnyValue (Integer b)) = return . insertSource (VNum i $ fromIntegral b)- f1 i (AnyValue (Double b)) = return . insertSource (VNum i $ realToFrac b)- f1 i (AnyValue (Text b)) = return . insertSource (VStr i b)- f1 i (AnyValue (Local b)) = return . insertSource (VLTime i b)- f1 i (AnyValue (Day b)) = return . insertSource (VDay i b)- f1 i (AnyValue (Hours b)) = return . insertSource (VHour i b)- f1 i (AnyValue (Zoned (ZonedTime a b))) = return . insertSource (VZTime i b a)- -- f1 i _ = return- f2 :: Monad m => Priority -> PrefixTree T.TOML -> Source -> m Source- f2 i (Leaf k toml) = updateSources (toSs k) (go toml i)- f2 i (Branch k v' tomap) = updateSources (toSs k) (h1 i v' >=> h2 i tomap)- h1 :: Monad m => Priority -> Maybe T.TOML -> Source -> m Source- h1 i (Just t) = go t i- h1 _ _ = return- h2 :: Monad m => Priority -> PrefixMap T.TOML -> Source -> m Source- h2 i m s = HM.foldlWithKey' (g2 i) (return s) m--loadToml :: MonadIO m => FilePath -> SourcePackT m ()-loadToml file = do- re <- liftIO $ parse <$> IO.readFile file- modify $ \sp ->case re of- Left e -> addErr' (show e) sp- Right a -> loadTOML (defReload file $ loadToml file) a sp
+ src/Salak/Toml.hs view
@@ -0,0 +1,63 @@+{-# LANGUAGE GADTs #-}+{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE TupleSections #-}+module Salak.Toml(+ TOML(..)+ , loadToml+ ) where++import Control.Monad (foldM, (>=>))+import Control.Monad.IO.Class (MonadIO, liftIO)+import Control.Monad.Writer+import qualified Data.HashMap.Strict as HM+import qualified Data.List.NonEmpty as N+import qualified Data.Text.IO as IO+import Data.Time+import Salak+import Salak.Load+import Toml hiding (TOML)+import qualified Toml as T++data TOML = TOML++instance HasLoad TOML where+ loaders _ = (, loadToml) <$> ["toml", "tml"]++toSs :: Key -> [Selector]+toSs (Key ps) = toS <$> N.toList ps++toS :: Piece -> Selector+toS = SStr . unPiece++loadTOML :: Monad m => T.TOML -> Priority -> Source -> WriterT [String] m Source+loadTOML T.TOML{..} i = foldPairs i tomlPairs+ >=> foldTables i tomlTables+ >=> foldTableArrays i tomlTableArrays+foldToml go m s = HM.foldlWithKey' (\ms k v -> ms >>= go k v) (return s) m+foldPairs i = foldToml (\k -> updateSources (toSs k) . insertAnyValue i)+foldTables i = foldToml (const go)+ where+ go (Leaf k toml) = updateSources (toSs k) (loadTOML toml i)+ go (Branch k v' tomap) = updateSources (toSs k) (maybe return (`loadTOML` i) v' >=> foldTables i tomap)+foldTableArrays i = foldToml (\k v -> updateSources (toSs k) (foldArray (N.toList v) (`loadTOML` i)))++insertAnyValue :: Monad m => Priority -> AnyValue -> Source -> m Source+insertAnyValue i (AnyValue (Array b)) = foldArray b (insertAnyValue i . AnyValue)+insertAnyValue i (AnyValue (Bool b)) = return . insertSource (VBool i b)+insertAnyValue i (AnyValue (Integer b)) = return . insertSource (VNum i $ fromIntegral b)+insertAnyValue i (AnyValue (Double b)) = return . insertSource (VNum i $ realToFrac b)+insertAnyValue i (AnyValue (Text b)) = return . insertSource (newVStr b i)+insertAnyValue i (AnyValue (Local b)) = return . insertSource (VLTime i b)+insertAnyValue i (AnyValue (Day b)) = return . insertSource (VDay i b)+insertAnyValue i (AnyValue (Hours b)) = return . insertSource (VHour i b)+insertAnyValue i (AnyValue (Zoned (ZonedTime a b))) = return . insertSource (VZTime i b a)++foldArray :: Monad m => [a] -> (a -> Source -> m Source) -> Source -> m Source+foldArray a g s = foldM (\s' (ix,x) -> updateSource (SNum ix) (g x) s') s $ zip [0..] a++loadToml :: MonadIO m => FilePath -> LoadSalakT m ()+loadToml file = loadFile file $ \i s -> do+ re <- liftIO (parse <$> IO.readFile file)+ case re of+ Left e -> tell [show e] >> return s+ Right a -> loadTOML a i s
test/Spec.hs view
@@ -7,13 +7,11 @@ module Main where import Control.Monad.Reader-import Control.Monad.Writer-import Data.Either import Data.List (intercalate)-import Data.Text (Text, pack, unpack)+import Data.Text (Text, pack) import GHC.Generics import Salak-import Salak.Load.Toml+import Salak.Toml import Test.Hspec import Test.QuickCheck @@ -21,7 +19,7 @@ spec :: Spec spec = do- describe "Salak.Load.Toml" tomlProperty+ describe "Salak.Toml" tomlProperty newtype SKey = SKey { unKey :: Text } deriving Show