Skip to content

Commit

Permalink
More Default instances
Browse files Browse the repository at this point in the history
  • Loading branch information
tbidne committed May 25, 2024
1 parent c3a0878 commit da6486b
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 121 deletions.
96 changes: 2 additions & 94 deletions src/Shrun/Configuration/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,105 +12,13 @@ import Shrun.Configuration.Args.Parsing
coreConfig
),
)
import Shrun.Configuration.Data.CommandLogging
( CommandLoggingP
( MkCommandLoggingP,
pollInterval,
readSize,
reportReadErrors
),
)
import Shrun.Configuration.Data.CommonLogging
( CommonLoggingP (MkCommonLoggingP, keyHide),
)
import Shrun.Configuration.Data.ConsoleLogging
( ConsoleLoggingP
( MkConsoleLoggingP,
commandLogging,
commandNameTrunc,
lineTrunc,
stripControl,
timerFormat
),
)
import Shrun.Configuration.Data.Core
( CoreConfigP
( MkCoreConfigP,
commandLogging,
commonLogging,
consoleLogging,
fileLogging,
init,
notify,
timeout
),
)
import Shrun.Configuration.Data.FileLogging
( FileLogInitP
( MkFileLogInitP,
mode,
path,
sizeMode
),
FileLoggingP
( MkFileLoggingP,
commandNameTrunc,
deleteOnSuccess,
file,
lineTrunc,
stripControl
),
)
import Shrun.Configuration.Data.Notify
( NotifyP (MkNotifyP, action, system, timeout),
)
import Shrun.Configuration.Default (Default (def))
import Shrun.Prelude

defaultArgs :: NESeq Text -> Args
defaultArgs commands =
MkArgs
{ configPath = mempty,
coreConfig =
MkCoreConfigP
{ timeout = mempty,
init = mempty,
commonLogging =
MkCommonLoggingP
{ keyHide = mempty
},
consoleLogging =
MkConsoleLoggingP
{ commandLogging = mempty,
commandNameTrunc = mempty,
lineTrunc = mempty,
stripControl = mempty,
timerFormat = mempty
},
commandLogging =
MkCommandLoggingP
{ pollInterval = mempty,
readSize = mempty,
reportReadErrors = mempty
},
fileLogging =
MkFileLoggingP
{ file =
MkFileLogInitP
{ path = mempty,
mode = mempty,
sizeMode = mempty
},
commandNameTrunc = mempty,
deleteOnSuccess = mempty,
lineTrunc = mempty,
stripControl = mempty
},
notify =
MkNotifyP
{ action = mempty,
system = mempty,
timeout = mempty
}
},
coreConfig = def,
commands
}
14 changes: 14 additions & 0 deletions src/Shrun/Configuration/Data/CommandLogging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ deriving stock instance Eq (CommandLoggingP ConfigPhaseMerged)

deriving stock instance Show (CommandLoggingP ConfigPhaseMerged)

instance
( Default (ConfigPhaseF p PollInterval),
Default (ConfigPhaseF p ReadSize),
Default (SwitchF p ReportReadErrorsSwitch)
) =>
Default (CommandLoggingP p)
where
def =
MkCommandLoggingP
{ pollInterval = def,
readSize = def,
reportReadErrors = def
}

-- | Merges args and toml configs.
mergeCommandLogging ::
CommandLoggingArgs ->
Expand Down
6 changes: 6 additions & 0 deletions src/Shrun/Configuration/Data/CommonLogging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ deriving stock instance Eq (CommonLoggingP ConfigPhaseMerged)

deriving stock instance Show (CommonLoggingP ConfigPhaseMerged)

instance
(Default (ConfigPhaseF p KeyHideSwitch)) =>
Default (CommonLoggingP p)
where
def = MkCommonLoggingP def

-- | Merges args and toml configs.
mergeCommonLogging ::
CommonLoggingArgs ->
Expand Down
18 changes: 18 additions & 0 deletions src/Shrun/Configuration/Data/ConsoleLogging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@ deriving stock instance Eq (ConsoleLoggingP ConfigPhaseMerged)

deriving stock instance Show (ConsoleLoggingP ConfigPhaseMerged)

instance
( Default (SwitchF p ConsoleLogCmdSwitch),
Default (ConfigPhaseMaybeF p (Truncation TruncCommandName)),
Default (LineTruncF p),
Default (ConfigPhaseF p ConsoleLogStripControl),
Default (ConfigPhaseF p TimerFormat)
) =>
Default (ConsoleLoggingP p)
where
def =
MkConsoleLoggingP
{ commandLogging = def,
commandNameTrunc = def,
lineTrunc = def,
stripControl = def,
timerFormat = def
}

-- | Merges args and toml configs.
mergeConsoleLogging ::
( HasCallStack,
Expand Down
23 changes: 23 additions & 0 deletions src/Shrun/Configuration/Data/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Shrun.Configuration.Data.FileLogging qualified as FileLogging
import Shrun.Configuration.Data.Notify (NotifyP, mergeNotifyLogging)
import Shrun.Configuration.Data.Notify qualified as Notify
import Shrun.Configuration.Data.WithDisabled ((<>??))
import Shrun.Configuration.Default (Default (def))
import Shrun.Notify.MonadDBus (MonadDBus)
import Shrun.Prelude

Expand Down Expand Up @@ -399,6 +400,28 @@ withCoreEnv merged onCoreConfigEnv = do
}
in onCoreConfigEnv coreConfigEnv

instance
( Default (ConfigPhaseMaybeF p Text),
Default (ConfigPhaseMaybeF p Timeout),
Default (TomlOptF p (CommonLoggingP p)),
Default (TomlOptF p (CommandLoggingP p)),
Default (TomlOptF p (ConsoleLoggingP p)),
Default (ArgsOnlyDetF p (FileLoggingP p)),
Default (ArgsOnlyDetF p (NotifyP p))
) =>
Default (CoreConfigP p)
where
def =
MkCoreConfigP
{ init = def,
timeout = def,
commonLogging = def,
commandLogging = def,
consoleLogging = def,
fileLogging = def,
notify = def
}

defaultToml :: CoreConfigToml
defaultToml =
MkCoreConfigP
Expand Down
29 changes: 28 additions & 1 deletion src/Shrun/Configuration/Data/FileLogging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import Shrun.Configuration.Data.WithDisabled
(<>??),
)
import Shrun.Configuration.Data.WithDisabled qualified as WD
import Shrun.Configuration.Default (Default (..))
import Shrun.Configuration.Default (Default (def))
import Shrun.Logging.Types (FileLog)
import Shrun.Prelude

Expand Down Expand Up @@ -205,6 +205,15 @@ deriving stock instance Eq FileLogInitMerged

deriving stock instance Show FileLogInitMerged

-- Only Default instance is for Args, since others require the Path.
instance Default FileLogInitArgs where
def =
MkFileLogInitP
{ path = def,
mode = def,
sizeMode = def
}

instance DecodeTOML FileLogInitToml where
tomlDecoder =
MkFileLogInitP
Expand Down Expand Up @@ -447,6 +456,24 @@ deriving stock instance Eq (FileLoggingP ConfigPhaseMerged)

deriving stock instance Show (FileLoggingP ConfigPhaseMerged)

instance
( Default (FileLogFileF p),
Default (ConfigPhaseMaybeF p (Truncation TruncCommandName)),
Default (SwitchF p DeleteOnSuccessSwitch),
Default (LineTruncF p),
Default (ConfigPhaseF p FileLogStripControl)
) =>
Default (FileLoggingP p)
where
def =
MkFileLoggingP
{ file = def,
commandNameTrunc = def,
deleteOnSuccess = def,
lineTrunc = def,
stripControl = def
}

-- | Merges args and toml configs.
mergeFileLogging ::
( HasCallStack,
Expand Down
4 changes: 2 additions & 2 deletions src/Shrun/Configuration/Data/MergedConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Shrun.Configuration.Data.MergedConfig
where

import Shrun.Configuration.Data.Core (CoreConfigMerged)
import Shrun.Configuration.Data.Core qualified as CoreConfig
import Shrun.Configuration.Default (Default (def))
import Shrun.Data.Command (CommandP1)
import Shrun.Prelude

Expand Down Expand Up @@ -55,6 +55,6 @@ instance
defaultMergedConfig :: NESeq CommandP1 -> MergedConfig
defaultMergedConfig commands =
MkMergedConfig
{ coreConfig = CoreConfig.defaultMerged,
{ coreConfig = def,
commands
}
16 changes: 13 additions & 3 deletions src/Shrun/Configuration/Data/Notify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Shrun.Configuration.Data.WithDisabled
(<>?.),
)
import Shrun.Configuration.Data.WithDisabled qualified as WD
import Shrun.Configuration.Default (Default, def)
import Shrun.Notify.MonadDBus (MonadDBus (connectSession))
import Shrun.Notify.Types
( LinuxNotifySystemMismatch (LinuxNotifySystemMismatchAppleScript),
Expand Down Expand Up @@ -133,6 +134,15 @@ deriving stock instance Eq (NotifyP ConfigPhaseMerged)

deriving stock instance Show (NotifyP ConfigPhaseMerged)

-- Only Default instance is for Args, since others require the action.
instance Default NotifyArgs where
def =
MkNotifyP
{ system = def,
action = def,
timeout = def
}

-- | Merges args and toml configs.
mergeNotifyLogging ::
NotifyArgs ->
Expand All @@ -141,7 +151,7 @@ mergeNotifyLogging ::
mergeNotifyLogging args mToml =
mAction <&> \action ->
let toml :: NotifyToml
toml = fromMaybe (mkDefaultToml action) mToml
toml = fromMaybe (defaultNotifyToml action) mToml
in MkNotifyP
{ action,
system =
Expand Down Expand Up @@ -206,8 +216,8 @@ mkNotify notifyToml systemP2 =
timeout = notifyToml ^. #timeout
}

mkDefaultToml :: NotifyAction -> NotifyToml
mkDefaultToml action =
defaultNotifyToml :: NotifyAction -> NotifyToml
defaultNotifyToml action =
MkNotifyP
{ system = Nothing,
action = action,
Expand Down
3 changes: 3 additions & 0 deletions src/Shrun/Configuration/Data/WithDisabled.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ instance Semigroup (WithDisabled a) where
instance Monoid (WithDisabled a) where
mempty = Without

instance Default (WithDisabled a) where
def = Without

-- | 'With' -> 'Just', o/w -> 'Nothing'.
toMaybe :: WithDisabled a -> Maybe a
toMaybe (With x) = Just x
Expand Down
5 changes: 5 additions & 0 deletions src/Shrun/Configuration/Default.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ module Shrun.Configuration.Default
)
where

import Shrun.Prelude

-- | For types with a default value. In general, instances should be "simple"
-- i.e. no instances for aggregate TTG types (e.g. FileLogging) as complexity
-- jumps quickly.
class Default a where
def :: a

instance Default (Maybe a) where
def = Nothing
25 changes: 8 additions & 17 deletions src/Shrun/Configuration/Toml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Shrun.Configuration.Toml
( Toml (..),
defaultToml,
)
where

Expand All @@ -20,6 +19,7 @@ import Shrun.Configuration.Data.Core
CoreConfigToml,
)
import Shrun.Configuration.Data.Core.Timeout (Timeout)
import Shrun.Configuration.Default (Default (def))
import Shrun.Configuration.Toml.Legend (KeyVal)
import Shrun.Prelude

Expand Down Expand Up @@ -48,6 +48,13 @@ instance
fmap (MkToml _coreConfig) (f _legend)
{-# INLINE labelOptic #-}

instance Default Toml where
def =
MkToml
{ coreConfig = def,
legend = def
}

instance DecodeTOML Toml where
tomlDecoder = do
timeout <- decodeTimeout
Expand All @@ -74,22 +81,6 @@ instance DecodeTOML Toml where
legend
}

defaultToml :: Toml
defaultToml =
MkToml
{ coreConfig =
MkCoreConfigP
{ timeout = Nothing,
init = Nothing,
commonLogging = Nothing,
consoleLogging = Nothing,
commandLogging = Nothing,
fileLogging = Nothing,
notify = Nothing
},
legend = Nothing
}

decodeTimeout :: Decoder (Maybe Timeout)
decodeTimeout = getFieldOptWith tomlDecoder "timeout"

Expand Down
Loading

0 comments on commit da6486b

Please sign in to comment.