Move code to Internal module and add tests

This commit is contained in:
Alexander Kobjolke 2023-08-15 19:47:37 +02:00
parent 2ac76cb650
commit ace38dcebc
6 changed files with 89 additions and 15 deletions

View file

@ -9,27 +9,22 @@ import System.IO (hPutStrLn, stderr)
import Control.Exception qualified as Exception
import System.IO.Error qualified as IOError
import Data.Text qualified as Text
import Data.Text.IO qualified as TextIO
parseArgs :: [String] -> Either String FilePath
parseArgs args = case args of
[] -> Left "No filename given!"
[arg] -> Right arg
_ -> Left "Only a single file is supported"
import HCat.Internal (parseArgs)
runHCat :: IO ()
runHCat = do
fileNameOrError <- parseArgs <$> Env.getArgs
fileName <- eitherToError fileNameOrError
TextIO.readFile fileName >>= TextIO.putStr
fileNameOrError <- parseArgs <$> Env.getArgs
fileName <- eitherToError fileNameOrError
TextIO.readFile fileName >>= TextIO.putStr
eitherToError :: Show a => Either a b -> IO b
eitherToError = either (Exception.throwIO . IOError.userError . show) return
handleError :: IOError -> IO ()
handleError e = do
hPutStrLn stderr $ "I ran into an issue: " <> show e
hPutStrLn stderr $ "I ran into an issue: " <> show e
defaultMain :: IO ()
defaultMain = Exception.catch runHCat handleError

8
src/HCat/Internal.hs Normal file
View file

@ -0,0 +1,8 @@
-- | Internal module in order to facilitate testability.
module HCat.Internal where
parseArgs :: [String] -> Either String FilePath
parseArgs args = case args of
[] -> Left "No filename given!"
[arg] -> Right arg
_ -> Left "Only a single file is supported"