serokell-util 0.5.3 → 0.6.0
raw patch · 6 files changed
+51/−261 lines, 6 filesdep −acid-statedep ~aesondep ~basedep ~formatting
Dependencies removed: acid-state
Dependency ranges changed: aeson, base, formatting, log-warper, universum
Files
- serokell-util.cabal +37/−37
- src/Serokell/AcidState.hs +0/−10
- src/Serokell/AcidState/ExtendedState.hs +0/−79
- src/Serokell/AcidState/Instances.hs +0/−70
- src/Serokell/AcidState/Util.hs +0/−56
- src/Serokell/Util/Lens.hs +14/−9
serokell-util.cabal view
@@ -1,5 +1,5 @@ name: serokell-util-version: 0.5.3+version: 0.6.0 synopsis: General-purpose functions by Serokell homepage: https://github.com/serokell/serokell-util license: MIT@@ -7,7 +7,7 @@ category: Utils author: Serokell maintainer: Serokell <hi@serokell.io>-copyright: 2016 Serokell+copyright: 2016-2018 Serokell build-type: Simple description: Serokell-util is a library consisting of functions, which are not included in standard libraries, but are useful for@@ -15,36 +15,34 @@ found that in new projects we need to use some utility functions from existing projects and don't want to copy-paste them.--- extra-source-files:-cabal-version: >=1.10+cabal-version: >=2.0 library- exposed-modules: Serokell.AcidState- Serokell.AcidState.ExtendedState- Serokell.AcidState.Instances- Serokell.AcidState.Util- Serokell.Arbitrary+ hs-source-dirs: src+ exposed-modules: Serokell.Arbitrary Serokell.Aeson.Options Serokell.Data.Memory.Units Serokell.Data.Variant+ Serokell.Util- Serokell.Util.ANSI- Serokell.Util.Base- Serokell.Util.Base16- Serokell.Util.Base64- Serokell.Util.Bench- Serokell.Util.Common- Serokell.Util.Concurrent- Serokell.Util.I18N- Serokell.Util.Parse- Serokell.Util.OptParse- Serokell.Util.Exceptions- Serokell.Util.Lens- Serokell.Util.StaticAssert- Serokell.Util.Text- Serokell.Util.Time- Serokell.Util.Trace- Serokell.Util.Verify+ Serokell.Util.ANSI+ Serokell.Util.Base+ Serokell.Util.Base16+ Serokell.Util.Base64+ Serokell.Util.Bench+ Serokell.Util.Common+ Serokell.Util.Concurrent+ Serokell.Util.I18N+ Serokell.Util.Parse+ Serokell.Util.OptParse+ Serokell.Util.Exceptions+ Serokell.Util.Lens+ Serokell.Util.StaticAssert+ Serokell.Util.Text+ Serokell.Util.Time+ Serokell.Util.Trace+ Serokell.Util.Verify+ other-modules: Serokell.Data.Variant.Class Serokell.Data.Variant.Helpers Serokell.Data.Variant.Serialization@@ -52,11 +50,10 @@ Serokell.Util.Parse.Base64 Serokell.Util.Parse.Common Serokell.Util.Parse.Network- build-depends: QuickCheck >= 2.8.1- , acid-state- , aeson >= 0.11.0.0 && < 1.2.0.0++ build-depends: base >= 4.9 && < 5+ , aeson >= 1.0 && < 1.4 , ansi-terminal- , base >= 4.8 && < 5 , base16-bytestring , base64-bytestring , bytestring@@ -67,16 +64,16 @@ , exceptions , extra , filepath- , formatting >= 6.2.0+ , formatting >= 6.2.0 && < 6.3 , hashable >= 1.2.4.0 , lens- , log-warper >= 1.7.1+ , log-warper ^>= 1.8.5 , monad-control , mtl , optparse-applicative , parsec+ , QuickCheck >= 2.8.1 , quickcheck-instances- , safecopy >= 0.9.0.1 , scientific , semigroups , stm >= 2.4.4@@ -85,13 +82,14 @@ , text-format , time-units , transformers- , universum >= 0.8.0+ , universum ^>= 1.0.2 , unordered-containers >= 0.2.7.0 , vector , yaml- hs-source-dirs: src- default-language: Haskell2010+ ghc-options: -Wall -fno-warn-orphans++ default-language: Haskell2010 default-extensions: OverloadedStrings RecordWildCards DeriveDataTypeable@@ -108,7 +106,9 @@ Test.Serokell.Util.VerifySpec Spec type: exitcode-stdio-1.0- build-depends: aeson >= 0.11.0.0 && < 1.2.0.0++ build-tool-depends: hspec-discover:hspec-discover+ build-depends: aeson >= 1.0 && < 1.4 , base >=4.8 , bytestring , hspec >= 2.1.10
− src/Serokell/AcidState.hs
@@ -1,10 +0,0 @@--- | Re-export Serokell.AcidState.* modules.--module Serokell.AcidState- (- module Exports- ) where--import Serokell.AcidState.ExtendedState as Exports-import Serokell.AcidState.Instances ()-import Serokell.AcidState.Util as Exports
− src/Serokell/AcidState/ExtendedState.hs
@@ -1,79 +0,0 @@-{-# LANGUAGE TypeFamilies #-}---- | The idea of ExtendedState is to store information about location--- of the state (either FilePath or memory).--module Serokell.AcidState.ExtendedState- ( ExtendedState (..)- , closeExtendedState- , extendedStateToAcid- , openLocalExtendedState- , openMemoryExtendedState- , queryExtended- , tidyExtendedState- , updateExtended- ) where--import Control.Monad.Extra (whenM)-import Control.Monad.Trans (MonadIO (liftIO))-import Data.Acid (AcidState, EventResult, EventState, IsAcidic,- QueryEvent, UpdateEvent, closeAcidState,- openLocalStateFrom)-import Data.Acid.Advanced (query', update')-import Data.Acid.Memory (openMemoryState)-import Data.Typeable (Typeable)--import System.Directory (doesDirectoryExist, removeDirectoryRecursive)--import Serokell.AcidState.Util (tidyLocalState)---- | ExtendedState is like usual AcidState, but also stores--- information about FilePath (unless it's in memory).-data ExtendedState st- = ESLocal (AcidState st)- FilePath- | ESMemory (AcidState st)---- | Convert ExtendedState to AcidState.-extendedStateToAcid :: ExtendedState st -> AcidState st-extendedStateToAcid (ESLocal s _) = s-extendedStateToAcid (ESMemory s) = s---- | Like query', but works on ExtendedState.-queryExtended- :: (EventState event ~ st, QueryEvent event, MonadIO m)- => ExtendedState st -> event -> m (EventResult event)-queryExtended st = query' (extendedStateToAcid st)---- | Like update', but works on ExtendedState.-updateExtended- :: (EventState event ~ st, UpdateEvent event, MonadIO m)- => ExtendedState st -> event -> m (EventResult event)-updateExtended st = update' (extendedStateToAcid st)---- | Like openLocalStateFrom, but returns ExtendedState and operates--- in MonadIO.-openLocalExtendedState- :: (IsAcidic st, Typeable st, MonadIO m)- => Bool -> FilePath -> st -> m (ExtendedState st)-openLocalExtendedState deleteIfExists fp st = do- whenM ((deleteIfExists &&) <$> liftIO (doesDirectoryExist fp)) $- liftIO $ removeDirectoryRecursive fp- liftIO $ flip ESLocal fp <$> openLocalStateFrom fp st---- | Like openMemoryState, but returns ExtendedState and operates in--- MonadIO.-openMemoryExtendedState- :: (IsAcidic st, Typeable st, MonadIO m)- => st -> m (ExtendedState st)-openMemoryExtendedState st = liftIO $ ESMemory <$> openMemoryState st---- | Like closeAcidState, but operates on ExtendedState and in--- MonadIO.-closeExtendedState :: MonadIO m => ExtendedState st -> m ()-closeExtendedState = liftIO . closeAcidState . extendedStateToAcid---- | Like tidyLocalState, but operates on ExtendedState.-tidyExtendedState :: MonadIO m => ExtendedState st -> m ()-tidyExtendedState (ESLocal st fp) = tidyLocalState st fp-tidyExtendedState (ESMemory _) = return ()
− src/Serokell/AcidState/Instances.hs
@@ -1,70 +0,0 @@-{-# LANGUAGE CPP #-}---- | Helper instances for acid-state to remove boilerplate and introduce--- redundant instances for other data types.--module Serokell.AcidState.Instances where--import Control.Exception (throw)-import Control.Monad.Catch (MonadThrow (throwM))--import qualified Data.Time.Units as Time-import Data.Acid (Query, Update)-import Data.Hashable (Hashable)-import Data.HashMap.Strict (HashMap)-import qualified Data.HashMap.Strict as HM hiding (HashMap)-import Data.HashSet (HashSet)-import qualified Data.HashSet as HS hiding (HashSet)-import Data.SafeCopy (SafeCopy (..), contain, safeGet,- safePut)--#if !MIN_VERSION_safecopy(0,9,3)-import qualified Data.List.NonEmpty as NE-#endif---- | Usually Queries shouldn't throw anything. This is a dirty hack.-instance MonadThrow (Query s) where- throwM = throw--instance MonadThrow (Update s) where- throwM = throw--instance (Eq a, Hashable a, SafeCopy a) => SafeCopy (HashSet a) where- putCopy = contain . safePut . HS.toList- getCopy = contain $ HS.fromList <$> safeGet--instance (Eq a, Hashable a, SafeCopy a, SafeCopy b) => SafeCopy (HashMap a b) where- putCopy = contain . safePut . HM.toList- getCopy = contain $ HM.fromList <$> safeGet--#if !MIN_VERSION_safecopy(0,9,3)-instance SafeCopy a => SafeCopy (NE.NonEmpty a) where- getCopy = contain $ do- xs <- safeGet- case NE.nonEmpty xs of- Nothing -> fail "getCopy@NonEmpty: list can't be empty"- Just xx -> return xx- putCopy = contain . safePut . NE.toList- errorTypeName _ = "NonEmpty"-#endif--#define SAFECOPY_TIME(T, TS) \- instance SafeCopy T where { \- getCopy = contain (fromInteger <$> safeGet); \- putCopy = contain . safePut . toInteger; \- errorTypeName _ = TS } \--SAFECOPY_TIME(Time.Fortnight, "Fortnight")-SAFECOPY_TIME(Time.Week, "Week")-SAFECOPY_TIME(Time.Day, "Day")-SAFECOPY_TIME(Time.Hour, "Hour")-SAFECOPY_TIME(Time.Minute, "Minute")-SAFECOPY_TIME(Time.Second, "Second")-SAFECOPY_TIME(Time.Millisecond, "Millisecond")-SAFECOPY_TIME(Time.Microsecond, "Microsecond")-SAFECOPY_TIME(Time.Nanosecond, "Nanosecond")-SAFECOPY_TIME(Time.Picosecond, "Picosecond")-SAFECOPY_TIME(Time.Femtosecond, "Femtosecond")-SAFECOPY_TIME(Time.Attosecond, "Attosecond")--#undef SAFECOPY_TIME
− src/Serokell/AcidState/Util.hs
@@ -1,56 +0,0 @@--- | Some useful functions to work with Data.Acid.--module Serokell.AcidState.Util- (- -- | Simple helpers- exceptStateToUpdate- , exceptStateToUpdateGeneric- , readerToQuery- , stateToUpdate-- -- | Utilities- , createAndDiscardArchive- , tidyLocalState- ) where--import Control.Exception (Exception, throw)-import Control.Monad.Except (ExceptT, runExceptT)-import Control.Monad.Reader (Reader, asks, runReader)-import Control.Monad.State (State, runState, state)-import Control.Monad.Trans (MonadIO (liftIO))-import Data.Acid (AcidState, Query, Update, createArchive,- createCheckpoint)-import System.Directory (removeDirectoryRecursive)-import System.FilePath ((</>))--readerToQuery :: Reader s a -> Query s a-readerToQuery = asks . runReader--stateToUpdate :: State s a -> Update s a-stateToUpdate = state . runState--exceptStateToUpdate- :: (Exception e)- => ExceptT e (State s) a -> Update s a-exceptStateToUpdate = exceptStateToUpdateGeneric id--exceptStateToUpdateGeneric- :: (Exception exc)- => (e -> exc) -> ExceptT e (State s) a -> Update s a-exceptStateToUpdateGeneric toException u =- state $- runState $- do res <- runExceptT u- either (throw . toException) return res---- | Archive unnecessary data (see createArchive docs for details) and--- discard it. Works for local state.-createAndDiscardArchive :: MonadIO m => AcidState st -> FilePath -> m ()-createAndDiscardArchive st path =- liftIO $ createArchive st >> removeDirectoryRecursive (path </> "Archive")---- | Apply all updates and remove all data from local state which is--- unnecessary for state restoration.-tidyLocalState :: MonadIO m => AcidState st -> FilePath -> m ()-tidyLocalState st path =- liftIO (createCheckpoint st) >> createAndDiscardArchive st path
src/Serokell/Util/Lens.hs view
@@ -12,14 +12,12 @@ , listL ) where -import qualified Control.Lens as L-import Control.Monad.Reader (MonadReader, Reader, ReaderT, reader,- runReader)-import Control.Monad.State (MonadState, State, StateT, get, runState,- state)-import Control.Monad.Trans.Except (ExceptT, mapExceptT)-import GHC.Exts (IsList (..))-import System.Wlog (LoggerName, LoggerNameBox (..))+import qualified Control.Lens as L+import Control.Monad.Reader (MonadReader, Reader, ReaderT, reader, runReader)+import Control.Monad.State (MonadState, State, StateT, get, runState, state)+import Control.Monad.Trans.Except (ExceptT, mapExceptT)+import GHC.Exts (IsList (..))+import System.Wlog (LoggerName, LoggerNameBox (..)) -- I don't know how to call these operators @@ -78,5 +76,12 @@ magnify' l = reader . runReader . L.magnify l -- | This isomorphism can be used to convert to or from an instance of 'IsList'.-listL :: IsList l => L.Iso' l [Item l]+--+-- Note that this function is quite general but doesn't allow to switch+-- container - in most cases such behavious eliminates need in specifing+-- container type manually.+listL+ :: (IsList (t a), IsList (t b))+ => L.Iso (t a) (t b) [Item (t a)] [Item (t b)] listL = L.iso toList fromList+