summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2017-12-20 10:12:52 +0300
committerdefanor <defanor@uberspace.net>2017-12-20 10:12:52 +0300
commitb2435e1e37b9def481761a00c704d280ef2deb96 (patch)
tree165aa7466248700d9820eaabb3e507ac06225822
parent3350f07b88a4628d83abaf9eaea2f0dfc2e44edf (diff)
downloadcoalpit-b2435e1e37b9def481761a00c704d280ef2deb96.zip
coalpit-b2435e1e37b9def481761a00c704d280ef2deb96.tar.gz
coalpit-b2435e1e37b9def481761a00c704d280ef2deb96.tar.bz2
Remove Coalpit.IO
Better to focus on printing and parsing here, at least for now; besides, pipes are nice, but some may prefer other methods. The code is moved into an example instead.
-rw-r--r--Coalpit.hs2
-rw-r--r--README.md4
-rw-r--r--coalpit.cabal5
-rw-r--r--examples/Basic.hs (renamed from Example.hs)0
-rw-r--r--examples/Pipes.hs (renamed from Coalpit/IO.hs)65
5 files changed, 28 insertions, 48 deletions
diff --git a/Coalpit.hs b/Coalpit.hs
index fda836a..6eb7f06 100644
--- a/Coalpit.hs
+++ b/Coalpit.hs
@@ -59,9 +59,7 @@ Then, in a shell:
module Coalpit ( module Coalpit.Core
, module Coalpit.DSV
- , module Coalpit.IO
) where
import Coalpit.Core
import Coalpit.DSV
-import Coalpit.IO
diff --git a/README.md b/README.md
index f9e3d7f..cb248ad 100644
--- a/README.md
+++ b/README.md
@@ -23,8 +23,8 @@ Not production-ready yet, merely a prototype.
## Example
-An example is available in `Example.hs`. Given the following Haskell
-value:
+An example is available in `examples/Basic.hs`. Given the following
+Haskell value:
```haskell
Input { something = Nothing
diff --git a/coalpit.cabal b/coalpit.cabal
index 6fa969f..207ee64 100644
--- a/coalpit.cabal
+++ b/coalpit.cabal
@@ -14,8 +14,9 @@ maintainer: defanor@uberspace.net
category: Console
build-type: Simple
extra-source-files: ChangeLog.md
- , Example.hs
, README.md
+ , examples/Basic.hs
+ , examples/Pipes.hs
cabal-version: >=1.10
tested-with: GHC == 8.0.1, GHC == 8.0.2, GHC == 8.2.1
bug-reports: https://github.com/defanor/coalpit/issues
@@ -27,14 +28,12 @@ library
exposed-modules: Coalpit
, Coalpit.Core
, Coalpit.DSV
- , Coalpit.IO
, Coalpit.Parsing
build-depends: base >= 4.9 && < 5
, megaparsec >= 6.2 && < 7
, scientific >= 0.3 && < 1
, time >= 1.6 && < 2
, network-uri >= 2.6 && < 3
- , pipes >= 4.3 && < 5
default-language: Haskell2010
ghc-options: -Wall
diff --git a/Example.hs b/examples/Basic.hs
index a08730d..a08730d 100644
--- a/Example.hs
+++ b/examples/Basic.hs
diff --git a/Coalpit/IO.hs b/examples/Pipes.hs
index c33fd98..7b50f93 100644
--- a/Coalpit/IO.hs
+++ b/examples/Pipes.hs
@@ -1,35 +1,7 @@
-{- |
-Module : Coalpit.IO
-Description : Helper IO functions
-Maintainer : defanor <defanor@uberspace.net>
-Stability : unstable
-Portability : non-portable (uses GHC extensions)
+{-# LANGUAGE RankNTypes, ScopedTypeVariables, DeriveGeneric,
+ DeriveAnyClass #-}
-These are basic utility functions for pipes-based IO.
-
-An example:
-
-@
-\{\-\# LANGUAGE DeriveGeneric, DeriveAnyClass \#\-\}
-import GHC.Generics
-import Pipes.Prelude as PP
-import Coalpit
-
-data Args = Args { arg1 :: Maybe Int, arg2 :: Double }
- deriving (Generic, Coalpit)
-data Input = Input Double deriving (Generic, Coalpit)
-data Output = Foo Double | Bar deriving (Generic, Coalpit)
-
-main :: IO ()
-main = run' $ \a -> PP.mapM $ \(Input i) ->
- pure $ Foo $ maybe (arg2 a) fromIntegral (arg1 a) + i
-@
-
--}
-
-{-# LANGUAGE RankNTypes, ScopedTypeVariables #-}
-
-module Coalpit.IO (run, run', handleErrors) where
+module Coalpit.IO (runMain, runMain', handleErrors) where
import Data.Proxy (Proxy(..))
import System.Environment (getProgName, getArgs)
@@ -41,6 +13,7 @@ import Control.Monad (mapM_, forever)
import qualified Pipes.Prelude as PP
import Coalpit.Core (Coalpit, fromArgs, defOpt, usage)
import Coalpit.DSV (readDSV, showDSV)
+import GHC.Generics
-- | Runs a given action on each 'Left' value, embedding that action's
-- result into the data stream.
@@ -56,13 +29,13 @@ handleErrors e = forever $ do
-- | Runs a given 'Pipe' between input producer and output consumer.
-- Prints an error and usage instructions if it fails to parse the
-- arguments, and passes the input through 'handleErrors'.
-run :: forall m a i o. (MonadIO m, Coalpit a, Coalpit i, Coalpit o)
- => (String -> m [i])
- -- ^ An action to run on error (see 'handleErrors').
- -> (a -> Pipe i o m ())
- -- ^ Main function.
- -> m ()
-run e f = do
+runMain :: forall m a i o. (MonadIO m, Coalpit a, Coalpit i, Coalpit o)
+ => (String -> m [i])
+ -- ^ An action to run on error (see 'handleErrors').
+ -> (a -> Pipe i o m ())
+ -- ^ Main function.
+ -> m ()
+runMain e f = do
pn <- liftIO getProgName
let u = Prelude.concat ["Usage: ", pn, " ", usage defOpt (Proxy :: Proxy a)]
args <- liftIO getArgs
@@ -75,9 +48,19 @@ run e f = do
>-> PP.map (showDSV defOpt)
>-> PP.stdoutLn
--- | Same as 'run', but just prints errors into 'stderr'.
-run' :: forall m a i o. (MonadIO m, Coalpit a, Coalpit i, Coalpit o)
+-- | Same as 'runMain', but just prints errors into 'stderr'.
+runMain' :: forall m a i o. (MonadIO m, Coalpit a, Coalpit i, Coalpit o)
=> (a -> Pipe i o m ())
-- ^ Main function.
-> m ()
-run' = run (\e -> liftIO $ hPutStrLn stderr e >> pure [])
+runMain' = runMain (\e -> liftIO $ hPutStrLn stderr e >> pure [])
+
+
+data Args = Args { arg1 :: Maybe Int, arg2 :: Double }
+ deriving (Generic, Coalpit)
+data Input = Input Double deriving (Generic, Coalpit)
+data Output = Foo Double | Bar deriving (Generic, Coalpit)
+
+main :: IO ()
+main = runMain' $ \a -> PP.mapM $ \(Input i) ->
+ pure $ Foo $ maybe (arg2 a) fromIntegral (arg1 a) + i