Replace `Option` with `Maybe`

pull/357/head
Taylor Fausak 2021-11-06 16:55:46 +00:00 committed by GitHub
parent b517eef71e
commit eccd2debb0
7 changed files with 38 additions and 62 deletions

View File

@ -79,7 +79,7 @@ data InlineConfigTarget
extractCommentConfigs extractCommentConfigs
:: ExactPrint.Anns :: ExactPrint.Anns
-> TopLevelDeclNameMap -> TopLevelDeclNameMap
-> Either (String, String) (CConfig Option, PerItemConfig) -> Either (String, String) (CConfig Maybe, PerItemConfig)
extractCommentConfigs anns (TopLevelDeclNameMap declNameMap) = do extractCommentConfigs anns (TopLevelDeclNameMap declNameMap) = do
let let
commentLiness = commentLiness =

View File

@ -118,7 +118,7 @@ forwardOptionsSyntaxExtsEnabled = ForwardOptions
} }
-- brittany-next-binding { lconfig_indentPolicy: IndentPolicyLeft, lconfig_cols: 200 } -- brittany-next-binding { lconfig_indentPolicy: IndentPolicyLeft, lconfig_cols: 200 }
cmdlineConfigParser :: CmdParser Identity out (CConfig Option) cmdlineConfigParser :: CmdParser Identity out (CConfig Maybe)
cmdlineConfigParser = do cmdlineConfigParser = do
-- TODO: why does the default not trigger; ind never should be []!! -- TODO: why does the default not trigger; ind never should be []!!
ind <- addFlagReadParams "" ["indent"] "AMOUNT" (flagHelpStr "spaces per indentation level") ind <- addFlagReadParams "" ["indent"] "AMOUNT" (flagHelpStr "spaces per indentation level")
@ -196,10 +196,10 @@ cmdlineConfigParser = do
, _conf_obfuscate = wrapLast $ falseToNothing obfuscate , _conf_obfuscate = wrapLast $ falseToNothing obfuscate
} }
where where
falseToNothing = Option . Bool.bool Nothing (Just True) falseToNothing = Bool.bool Nothing (Just True)
wrapLast :: Option a -> Option (Semigroup.Last a) wrapLast :: Maybe a -> Maybe (Semigroup.Last a)
wrapLast = fmap Semigroup.Last wrapLast = fmap Semigroup.Last
optionConcat :: (Semigroup.Semigroup (f a), Applicative f) => [a] -> Option (f a) optionConcat :: (Semigroup.Semigroup (f a), Applicative f) => [a] -> Maybe (f a)
optionConcat = mconcat . fmap (pure . pure) optionConcat = mconcat . fmap (pure . pure)
-- configParser :: Parser Config -- configParser :: Parser Config
@ -230,7 +230,7 @@ cmdlineConfigParser = do
-- If the second parameter is True and the file does not exist, writes the -- If the second parameter is True and the file does not exist, writes the
-- staticDefaultConfig to the file. -- staticDefaultConfig to the file.
readConfig readConfig
:: MonadIO m => System.IO.FilePath -> MaybeT m (Maybe (CConfig Option)) :: MonadIO m => System.IO.FilePath -> MaybeT m (Maybe (CConfig Maybe))
readConfig path = do readConfig path = do
-- TODO: probably should catch IOErrors and then omit the existence check. -- TODO: probably should catch IOErrors and then omit the existence check.
exists <- liftIO $ System.Directory.doesFileExist path exists <- liftIO $ System.Directory.doesFileExist path
@ -278,7 +278,7 @@ findLocalConfigPath dir = do
-- | Reads specified configs. -- | Reads specified configs.
readConfigs readConfigs
:: CConfig Option -- ^ Explicit options, take highest priority :: CConfig Maybe -- ^ Explicit options, take highest priority
-> [System.IO.FilePath] -- ^ List of config files to load and merge, highest priority first -> [System.IO.FilePath] -- ^ List of config files to load and merge, highest priority first
-> MaybeT IO Config -> MaybeT IO Config
readConfigs cmdlineConfig configPaths = do readConfigs cmdlineConfig configPaths = do
@ -290,7 +290,7 @@ readConfigs cmdlineConfig configPaths = do
-- | Reads provided configs -- | Reads provided configs
-- but also applies the user default configuration (with lowest priority) -- but also applies the user default configuration (with lowest priority)
readConfigsWithUserConfig readConfigsWithUserConfig
:: CConfig Option -- ^ Explicit options, take highest priority :: CConfig Maybe -- ^ Explicit options, take highest priority
-> [System.IO.FilePath] -- ^ List of config files to load and merge, highest priority first -> [System.IO.FilePath] -- ^ List of config files to load and merge, highest priority first
-> MaybeT IO Config -> MaybeT IO Config
readConfigsWithUserConfig cmdlineConfig configPaths = do readConfigsWithUserConfig cmdlineConfig configPaths = do
@ -300,10 +300,9 @@ readConfigsWithUserConfig cmdlineConfig configPaths = do
writeDefaultConfig :: MonadIO m => System.IO.FilePath -> m () writeDefaultConfig :: MonadIO m => System.IO.FilePath -> m ()
writeDefaultConfig path = writeDefaultConfig path =
liftIO $ ByteString.writeFile path $ Data.Yaml.encode $ cMap liftIO $ ByteString.writeFile path $ Data.Yaml.encode $ cMap
(Option . Just . runIdentity) (Just . runIdentity)
staticDefaultConfig staticDefaultConfig
showConfigYaml :: Config -> String showConfigYaml :: Config -> String
showConfigYaml = Data.ByteString.Char8.unpack . Data.Yaml.encode . cMap showConfigYaml = Data.ByteString.Char8.unpack . Data.Yaml.encode . cMap
(\(Identity x) -> Just x) (\(Identity x) -> Just x)

View File

@ -23,7 +23,7 @@ import Data.Data ( Data )
import Data.Coerce ( Coercible, coerce ) import Data.Coerce ( Coercible, coerce )
import Data.Semigroup.Generic import Data.Semigroup.Generic
import Data.Semigroup ( Last, Option ) import Data.Semigroup ( Last )
import Data.CZipWith import Data.CZipWith
@ -215,12 +215,12 @@ deriving instance Show (CForwardOptions Identity)
deriving instance Show (CPreProcessorConfig Identity) deriving instance Show (CPreProcessorConfig Identity)
deriving instance Show (CConfig Identity) deriving instance Show (CConfig Identity)
deriving instance Show (CDebugConfig Option) deriving instance Show (CDebugConfig Maybe)
deriving instance Show (CLayoutConfig Option) deriving instance Show (CLayoutConfig Maybe)
deriving instance Show (CErrorHandlingConfig Option) deriving instance Show (CErrorHandlingConfig Maybe)
deriving instance Show (CForwardOptions Option) deriving instance Show (CForwardOptions Maybe)
deriving instance Show (CPreProcessorConfig Option) deriving instance Show (CPreProcessorConfig Maybe)
deriving instance Show (CConfig Option) deriving instance Show (CConfig Maybe)
deriving instance Data (CDebugConfig Identity) deriving instance Data (CDebugConfig Identity)
deriving instance Data (CLayoutConfig Identity) deriving instance Data (CLayoutConfig Identity)
@ -229,24 +229,24 @@ deriving instance Data (CForwardOptions Identity)
deriving instance Data (CPreProcessorConfig Identity) deriving instance Data (CPreProcessorConfig Identity)
deriving instance Data (CConfig Identity) deriving instance Data (CConfig Identity)
deriving instance Data (CDebugConfig Option) deriving instance Data (CDebugConfig Maybe)
deriving instance Data (CLayoutConfig Option) deriving instance Data (CLayoutConfig Maybe)
deriving instance Data (CErrorHandlingConfig Option) deriving instance Data (CErrorHandlingConfig Maybe)
deriving instance Data (CForwardOptions Option) deriving instance Data (CForwardOptions Maybe)
deriving instance Data (CPreProcessorConfig Option) deriving instance Data (CPreProcessorConfig Maybe)
deriving instance Data (CConfig Option) deriving instance Data (CConfig Maybe)
instance Semigroup.Semigroup (CDebugConfig Option) where instance Semigroup.Semigroup (CDebugConfig Maybe) where
(<>) = gmappend (<>) = gmappend
instance Semigroup.Semigroup (CLayoutConfig Option) where instance Semigroup.Semigroup (CLayoutConfig Maybe) where
(<>) = gmappend (<>) = gmappend
instance Semigroup.Semigroup (CErrorHandlingConfig Option) where instance Semigroup.Semigroup (CErrorHandlingConfig Maybe) where
(<>) = gmappend (<>) = gmappend
instance Semigroup.Semigroup (CForwardOptions Option) where instance Semigroup.Semigroup (CForwardOptions Maybe) where
(<>) = gmappend (<>) = gmappend
instance Semigroup.Semigroup (CPreProcessorConfig Option) where instance Semigroup.Semigroup (CPreProcessorConfig Maybe) where
(<>) = gmappend (<>) = gmappend
instance Semigroup.Semigroup (CConfig Option) where instance Semigroup.Semigroup (CConfig Maybe) where
(<>) = gmappend (<>) = gmappend
instance Semigroup.Semigroup (CDebugConfig Identity) where instance Semigroup.Semigroup (CDebugConfig Identity) where
@ -262,22 +262,22 @@ instance Semigroup.Semigroup (CPreProcessorConfig Identity) where
instance Semigroup.Semigroup (CConfig Identity) where instance Semigroup.Semigroup (CConfig Identity) where
(<>) = gmappend (<>) = gmappend
instance Monoid (CDebugConfig Option) where instance Monoid (CDebugConfig Maybe) where
mempty = gmempty mempty = gmempty
mappend = gmappend mappend = gmappend
instance Monoid (CLayoutConfig Option) where instance Monoid (CLayoutConfig Maybe) where
mempty = gmempty mempty = gmempty
mappend = gmappend mappend = gmappend
instance Monoid (CErrorHandlingConfig Option) where instance Monoid (CErrorHandlingConfig Maybe) where
mempty = gmempty mempty = gmempty
mappend = gmappend mappend = gmappend
instance Monoid (CForwardOptions Option) where instance Monoid (CForwardOptions Maybe) where
mempty = gmempty mempty = gmempty
mappend = gmappend mappend = gmappend
instance Monoid (CPreProcessorConfig Option) where instance Monoid (CPreProcessorConfig Maybe) where
mempty = gmempty mempty = gmempty
mappend = gmappend mappend = gmappend
instance Monoid (CConfig Option) where instance Monoid (CConfig Maybe) where
mempty = gmempty mempty = gmempty
mappend = gmappend mappend = gmappend

View File

@ -51,27 +51,15 @@ aesonDecodeOptionsBrittany = Aeson.defaultOptions
instance FromJSON (type Maybe) where\ instance FromJSON (type Maybe) where\
parseJSON = Aeson.genericParseJSON aesonDecodeOptionsBrittany;\ parseJSON = Aeson.genericParseJSON aesonDecodeOptionsBrittany;\
{-# NOINLINE parseJSON #-} {-# NOINLINE parseJSON #-}
#define makeFromJSONOption(type)\
instance FromJSON (type Option) where\
parseJSON = fmap (cMap Option) . parseJSON;\
{-# NOINLINE parseJSON #-}
#define makeToJSONMaybe(type)\ #define makeToJSONMaybe(type)\
instance ToJSON (type Maybe) where\ instance ToJSON (type Maybe) where\
toJSON = Aeson.genericToJSON aesonDecodeOptionsBrittany;\ toJSON = Aeson.genericToJSON aesonDecodeOptionsBrittany;\
{-# NOINLINE toJSON #-};\ {-# NOINLINE toJSON #-};\
toEncoding = Aeson.genericToEncoding aesonDecodeOptionsBrittany;\ toEncoding = Aeson.genericToEncoding aesonDecodeOptionsBrittany;\
{-# NOINLINE toEncoding #-} {-# NOINLINE toEncoding #-}
#define makeToJSONOption(type)\
instance ToJSON (type Option) where\
toJSON = toJSON . cMap getOption;\
{-# NOINLINE toJSON #-};\
toEncoding = toEncoding . cMap getOption;\
{-# NOINLINE toEncoding #-}
makeFromJSONOption(CDebugConfig)
makeFromJSONMaybe(CDebugConfig) makeFromJSONMaybe(CDebugConfig)
makeToJSONOption(CDebugConfig)
makeToJSONMaybe(CDebugConfig) makeToJSONMaybe(CDebugConfig)
makeFromJSON(IndentPolicy) makeFromJSON(IndentPolicy)
@ -85,28 +73,18 @@ makeToJSON(CPPMode)
makeFromJSON(ExactPrintFallbackMode) makeFromJSON(ExactPrintFallbackMode)
makeToJSON(ExactPrintFallbackMode) makeToJSON(ExactPrintFallbackMode)
makeFromJSONOption(CLayoutConfig)
makeFromJSONMaybe(CLayoutConfig) makeFromJSONMaybe(CLayoutConfig)
makeToJSONOption(CLayoutConfig)
makeToJSONMaybe(CLayoutConfig) makeToJSONMaybe(CLayoutConfig)
makeFromJSONOption(CErrorHandlingConfig)
makeFromJSONMaybe(CErrorHandlingConfig) makeFromJSONMaybe(CErrorHandlingConfig)
makeToJSONOption(CErrorHandlingConfig)
makeToJSONMaybe(CErrorHandlingConfig) makeToJSONMaybe(CErrorHandlingConfig)
makeFromJSONOption(CForwardOptions)
makeFromJSONMaybe(CForwardOptions) makeFromJSONMaybe(CForwardOptions)
makeToJSONOption(CForwardOptions)
makeToJSONMaybe(CForwardOptions) makeToJSONMaybe(CForwardOptions)
makeFromJSONOption(CPreProcessorConfig)
makeFromJSONMaybe(CPreProcessorConfig) makeFromJSONMaybe(CPreProcessorConfig)
makeToJSONOption(CPreProcessorConfig)
makeToJSONMaybe(CPreProcessorConfig) makeToJSONMaybe(CPreProcessorConfig)
makeFromJSONOption(CConfig)
makeToJSONOption(CConfig)
makeToJSONMaybe(CConfig) makeToJSONMaybe(CConfig)
-- This custom instance ensures the "omitNothingFields" behaviour not only for -- This custom instance ensures the "omitNothingFields" behaviour not only for

View File

@ -32,7 +32,6 @@ import Data.Char as E ( Char )
import Data.Either as E ( Either(..) ) import Data.Either as E ( Either(..) )
import Data.IORef as E ( IORef ) import Data.IORef as E ( IORef )
import Data.Maybe as E ( Maybe(..) ) import Data.Maybe as E ( Maybe(..) )
import Data.Semigroup as E ( Option(..) )
import Data.Monoid as E ( Endo(..) import Data.Monoid as E ( Endo(..)
, All(..) , All(..)
, Any(..) , Any(..)

View File

@ -29,8 +29,8 @@ import Data.Generics.Uniplate.Direct as Uniplate
data PerItemConfig = PerItemConfig data PerItemConfig = PerItemConfig
{ _icd_perBinding :: Map String (CConfig Option) { _icd_perBinding :: Map String (CConfig Maybe)
, _icd_perKey :: Map ExactPrint.Types.AnnKey (CConfig Option) , _icd_perKey :: Map ExactPrint.Types.AnnKey (CConfig Maybe)
} }
deriving Data.Data.Data deriving Data.Data.Data

View File

@ -79,9 +79,9 @@ showOutputable = GHC.showPpr GHC.unsafeGlobalDynFlags
fromMaybeIdentity :: Identity a -> Maybe a -> Identity a fromMaybeIdentity :: Identity a -> Maybe a -> Identity a
fromMaybeIdentity x y = Data.Coerce.coerce $ fromMaybe (Data.Coerce.coerce x) y fromMaybeIdentity x y = Data.Coerce.coerce $ fromMaybe (Data.Coerce.coerce x) y
fromOptionIdentity :: Identity a -> Option a -> Identity a fromOptionIdentity :: Identity a -> Maybe a -> Identity a
fromOptionIdentity x y = fromOptionIdentity x y =
Data.Coerce.coerce $ fromMaybe (Data.Coerce.coerce x) $ getOption y Data.Coerce.coerce $ fromMaybe (Data.Coerce.coerce x) y
-- maximum monoid over N+0 -- maximum monoid over N+0
-- or more than N, because Num is allowed. -- or more than N, because Num is allowed.