Skip to content

Commit

Permalink
Define readOrFail as a general function
Browse files Browse the repository at this point in the history
  • Loading branch information
toku-sa-n committed Sep 14, 2022
1 parent c585270 commit 4330ed4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions hindent.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ library
HIndent.Pretty.Imports.Sort
HIndent.Pretty.ModuleDeclaration
HIndent.Pretty.Pragma
HIndent.Read
HIndent.RelocateComments
HIndent.SrcSpan
HIndent.Types
Expand Down
5 changes: 1 addition & 4 deletions src/HIndent/CabalFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import GHC.Driver.Session (impliedXFlags,
import qualified GHC.Driver.Session as GLP
import qualified GHC.LanguageExtensions.Type as GLP
import qualified HIndent.ExtensionConversion as EC
import HIndent.Read
import Language.Haskell.Extension
import System.Directory
import System.FilePath
import Text.Read

data Stanza =
MkStanza
Expand Down Expand Up @@ -166,9 +166,6 @@ getCabalExtensionsForSourcePath srcpath = do
implicitExtensions :: GLP.Language -> [Extension]
implicitExtensions =
fmap (EnableExtension . readOrFail . show) . languageExtensions . Just
where
readOrFail x =
fromMaybe (error $ "Unsupported extension: " ++ show x) $ readMaybe x

extensionImplies :: Extension -> [Extension]
extensionImplies (EnableExtension e) =
Expand Down
9 changes: 1 addition & 8 deletions src/HIndent/ExtensionConversion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@ module HIndent.ExtensionConversion
, convertExtension
) where

import Data.Maybe
import qualified GHC.LanguageExtensions as GLP
import HIndent.Read
import qualified Language.Haskell.Extension as Cabal
import Text.Read

gleExtensionToCabalExtension :: GLP.Extension -> Cabal.Extension
gleExtensionToCabalExtension = readOrFail . show
where
readOrFail x =
fromMaybe (error $ "Unsupported extension: " ++ show x) $ readMaybe x

uniqueExtensions :: [Cabal.Extension] -> [GLP.Extension]
uniqueExtensions [] = []
uniqueExtensions ((Cabal.EnableExtension e):xs) =
convertExtension e : uniqueExtensions xs
uniqueExtensions ((Cabal.DisableExtension e):xs) =
uniqueExtensions $ filter (/= readOrFail (show e)) xs
where
readOrFail x =
fromMaybe (error $ "Unsupported extension: " ++ show x) $ readMaybe x
uniqueExtensions ((Cabal.UnknownExtension s):_) =
error $ "Unknown extension: " ++ s

Expand Down
14 changes: 14 additions & 0 deletions src/HIndent/Read.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module HIndent.Read
( readOrFail
) where

import Data.Maybe
import GHC.Stack
import Text.Read

-- | `read` but dumps a stack trace if reading a value fails.
readOrFail ::
HasCallStack
=> Read a =>
String -> a
readOrFail x = fromMaybe (error $ "`read` failed: " ++ x) $ readMaybe x

0 comments on commit 4330ed4

Please sign in to comment.