Skip to content

Commit

Permalink
Move Notify types to Config namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tbidne committed Jun 7, 2024
1 parent 7cb005a commit a7427e8
Show file tree
Hide file tree
Showing 19 changed files with 201 additions and 162 deletions.
4 changes: 3 additions & 1 deletion shrun.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ library
Shrun.Configuration.Data.FileLogging.FileSizeMode
Shrun.Configuration.Data.MergedConfig
Shrun.Configuration.Data.Notify
Shrun.Configuration.Data.Notify.Action
Shrun.Configuration.Data.Notify.System
Shrun.Configuration.Data.Notify.Timeout
Shrun.Configuration.Data.StripControl
Shrun.Configuration.Data.Truncation
Shrun.Configuration.Data.WithDisabled
Expand All @@ -106,7 +109,6 @@ library
Shrun.Notify.MonadDBus
Shrun.Notify.MonadNotify
Shrun.Notify.MonadNotifySend
Shrun.Notify.Types
Shrun.Prelude
Shrun.ShellT
Shrun.Utils
Expand Down
14 changes: 7 additions & 7 deletions src/Shrun.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import Shrun.Configuration.Data.FileLogging
( FileLogOpened (MkFileLogOpened),
FileLoggingEnv,
)
import Shrun.Configuration.Data.Notify.Action
( NotifyAction
( NotifyAll,
NotifyCommand,
NotifyFinal
),
)
import Shrun.Configuration.Env.Types
( HasAnyError (getAnyError),
HasCommandLogging,
Expand Down Expand Up @@ -64,13 +71,6 @@ import Shrun.Logging.Types
)
import Shrun.Notify qualified as Notify
import Shrun.Notify.MonadNotify (MonadNotify)
import Shrun.Notify.Types
( NotifyAction
( NotifyAll,
NotifyCommand,
NotifyFinal
),
)
import Shrun.Prelude
import Shrun.ShellT (ShellT, runShellT)
import Shrun.Utils qualified as Utils
Expand Down
20 changes: 12 additions & 8 deletions src/Shrun/Configuration/Args/Parsing/Notify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import Shrun.Configuration.Data.Notify
( NotifyArgs,
NotifyP (MkNotifyP, action, system, timeout),
)
import Shrun.Configuration.Data.Notify.Action (NotifyAction)
import Shrun.Configuration.Data.Notify.Action qualified as Action
import Shrun.Configuration.Data.Notify.System (NotifySystemArgs)
import Shrun.Configuration.Data.Notify.System qualified as System
import Shrun.Configuration.Data.Notify.Timeout (NotifyTimeout)
import Shrun.Configuration.Data.Notify.Timeout qualified as Timeout
import Shrun.Configuration.Data.WithDisabled (WithDisabled)
import Shrun.Notify.Types (NotifyAction, NotifySystemArgs, NotifyTimeout)
import Shrun.Notify.Types qualified as Notify
import Shrun.Prelude

notifyParser :: Parser NotifyArgs
Expand All @@ -34,11 +38,11 @@ notifyActionParser = Utils.withDisabledParser mainParser "notify-action"
where
mainParser =
OA.optional
$ OA.option (Notify.parseNotifyAction OA.str)
$ OA.option (Action.parseNotifyAction OA.str)
$ mconcat
[ OA.long "notify-action",
Utils.mkHelp helpTxt,
OA.metavar Notify.notifyActionStr
OA.metavar Action.notifyActionStr
]
helpTxt =
mconcat
Expand All @@ -53,11 +57,11 @@ notifySystemParser = Utils.withDisabledParser mainParser "notify-system"
where
mainParser =
OA.optional
$ OA.option (Notify.parseNotifySystem OA.str)
$ OA.option (System.parseNotifySystem OA.str)
$ mconcat
[ OA.long "notify-system",
Utils.mkHelp helpTxt,
OA.metavar Notify.notifySystemStr
OA.metavar System.notifySystemStr
]
helpTxt =
mconcat
Expand All @@ -70,11 +74,11 @@ notifyTimeoutParser = Utils.withDisabledParser mainParser "notify-timeout"
where
mainParser =
OA.optional
$ OA.option (Notify.parseNotifyTimeout OA.str)
$ OA.option (Timeout.parseNotifyTimeout OA.str)
$ mconcat
[ OA.long "notify-timeout",
Utils.mkHelp helpTxt,
OA.metavar Notify.notifyTimeoutStr
OA.metavar Timeout.notifyTimeoutStr
]
helpTxt =
mconcat
Expand Down
24 changes: 14 additions & 10 deletions src/Shrun/Configuration/Data/Notify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,31 @@ import Shrun.Configuration.Data.ConfigPhase
),
ConfigPhaseF,
)
import Shrun.Configuration.Data.WithDisabled
( WithDisabled (Disabled, With, Without),
(<>?),
(<>?.),
import Shrun.Configuration.Data.Notify.Action
( NotifyAction,
)
import Shrun.Configuration.Data.WithDisabled qualified as WD
import Shrun.Configuration.Default (Default, def)
import Shrun.Notify.MonadDBus (MonadDBus (connectSession))
import Shrun.Notify.Types
import Shrun.Configuration.Data.Notify.System
( LinuxNotifySystemMismatch (LinuxNotifySystemMismatchAppleScript),
NotifyAction,
NotifySystemEnv,
NotifySystemP (AppleScript, DBus, NotifySend),
NotifyTimeout,
OsxNotifySystemMismatch
( OsxNotifySystemMismatchDBus,
OsxNotifySystemMismatchNotifySend
),
displayNotifySystem,
mergeNotifySystem,
)
import Shrun.Configuration.Data.Notify.Timeout
( NotifyTimeout,
)
import Shrun.Configuration.Data.WithDisabled
( WithDisabled (Disabled, With, Without),
(<>?),
(<>?.),
)
import Shrun.Configuration.Data.WithDisabled qualified as WD
import Shrun.Configuration.Default (Default, def)
import Shrun.Notify.MonadDBus (MonadDBus (connectSession))
import Shrun.Prelude

-- See NOTE: [Args vs. Toml mandatory fields]
Expand Down
45 changes: 45 additions & 0 deletions src/Shrun/Configuration/Data/Notify/Action.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- | Provides type for notifications.
module Shrun.Configuration.Data.Notify.Action
( NotifyAction (..),
parseNotifyAction,
notifyActionStr,
)
where

import Data.String (IsString)
import Data.Text qualified as T
import Shrun.Prelude

-- | Determines for which actions we should send notifications.
data NotifyAction
= -- | Send a notification after all commands are completed.
NotifyFinal
| -- | Send notifications when each command completes.
NotifyCommand
| -- | NotifyFinal and NotifyCommand.
NotifyAll
deriving stock (Eq, Show)

instance DecodeTOML NotifyAction where
tomlDecoder = parseNotifyAction tomlDecoder

-- | Parses 'NotifyAction'.
parseNotifyAction :: (MonadFail m) => m Text -> m NotifyAction
parseNotifyAction getTxt =
getTxt >>= \case
"final" -> pure NotifyFinal
"command" -> pure NotifyCommand
"all" -> pure NotifyAll
other ->
fail
$ mconcat
[ "Unrecognized notify action: '",
T.unpack other,
"'. Expected one of ",
notifyActionStr
]
{-# INLINEABLE parseNotifyAction #-}

-- | Available 'NotifyAction' strings.
notifyActionStr :: (IsString a) => a
notifyActionStr = "(final |command | all)"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{-# LANGUAGE UndecidableInstances #-}

-- | Provides type for notifications.
module Shrun.Notify.Types
module Shrun.Configuration.Data.Notify.System
( -- * Notify system
NotifySystemP (..),
NotifySystemArgs,
Expand All @@ -16,28 +16,15 @@ module Shrun.Notify.Types
DBusF,
mergeNotifySystem,

-- * Notify actions
NotifyAction (..),
parseNotifyAction,
notifyActionStr,

-- * Notify timeout
NotifyTimeout (..),
parseNotifyTimeout,
notifyTimeoutStr,

-- * Exceptions
OsxNotifySystemMismatch (..),
LinuxNotifySystemMismatch (..),
)
where

import DBus.Client (Client)
import Data.Bits (toIntegralSized)
import Data.String (IsString)
import Data.Text qualified as T
import Data.Word (Word16)
import GHC.Num (Num (fromInteger))
import Shrun.Configuration.Data.ConfigPhase
( ConfigPhase
( ConfigPhaseArgs,
Expand All @@ -51,42 +38,6 @@ import Shrun.Configuration.Data.WithDisabled
)
import Shrun.Configuration.Default (Default (def))
import Shrun.Prelude
import Shrun.Utils qualified as U
import TOML (Value (Integer, String))

-- | Determines for which actions we should send notifications.
data NotifyAction
= -- | Send a notification after all commands are completed.
NotifyFinal
| -- | Send notifications when each command completes.
NotifyCommand
| -- | NotifyFinal and NotifyCommand.
NotifyAll
deriving stock (Eq, Show)

instance DecodeTOML NotifyAction where
tomlDecoder = parseNotifyAction tomlDecoder

-- | Parses 'NotifyAction'.
parseNotifyAction :: (MonadFail m) => m Text -> m NotifyAction
parseNotifyAction getTxt =
getTxt >>= \case
"final" -> pure NotifyFinal
"command" -> pure NotifyCommand
"all" -> pure NotifyAll
other ->
fail
$ mconcat
[ "Unrecognized notify action: '",
T.unpack other,
"'. Expected one of ",
notifyActionStr
]
{-# INLINEABLE parseNotifyAction #-}

-- | Available 'NotifyAction' strings.
notifyActionStr :: (IsString a) => a
notifyActionStr = "(final |command | all)"

-- | Maps DBus to its phased param.
type DBusF :: ConfigPhase -> Type
Expand Down Expand Up @@ -185,48 +136,6 @@ instance (DBusF p ~ ()) => Default (NotifySystemP p) where
def = DBus ()
#endif

-- | Determines notification timeout.
data NotifyTimeout
= -- | Times out after the given seconds.
NotifyTimeoutSeconds Word16
| -- | Never times out.
NotifyTimeoutNever
deriving stock (Eq, Show)

instance Default NotifyTimeout where
def = NotifyTimeoutSeconds 10

instance FromInteger NotifyTimeout where
afromInteger = NotifyTimeoutSeconds . fromInteger

-- DecodeTOML instance does not reuse parseNotifyTimeout as we want to
-- enforce the integer type.

instance DecodeTOML NotifyTimeout where
tomlDecoder = makeDecoder $ \case
String "never" -> pure NotifyTimeoutNever
String bad -> invalidValue strErr (String bad)
Integer i -> case toIntegralSized i of
Just i' -> pure $ NotifyTimeoutSeconds i'
Nothing -> invalidValue tooLargeErr (Integer i)
badTy -> typeMismatch badTy
where
tooLargeErr = "Timeout integer too large. Max is: " <> showt maxW16
strErr = "Unexpected timeout. Only valid string is 'never'."
maxW16 = maxBound @Word16

-- | Parses 'NotifyTimeout'.
parseNotifyTimeout :: (MonadFail m) => m Text -> m NotifyTimeout
parseNotifyTimeout getTxt =
getTxt >>= \case
"never" -> pure NotifyTimeoutNever
other -> NotifyTimeoutSeconds <$> U.readStripUnderscores other
{-# INLINEABLE parseNotifyTimeout #-}

-- | Available 'NotifyTimeout' strings.
notifyTimeoutStr :: (IsString a) => a
notifyTimeoutStr = "(never | NATURAL)"

data OsxNotifySystemMismatch
= OsxNotifySystemMismatchDBus
| OsxNotifySystemMismatchNotifySend
Expand Down
58 changes: 58 additions & 0 deletions src/Shrun/Configuration/Data/Notify/Timeout.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- | Provides type for notifications.
module Shrun.Configuration.Data.Notify.Timeout
( NotifyTimeout (..),
parseNotifyTimeout,
notifyTimeoutStr,
)
where

import Data.Bits (toIntegralSized)
import Data.String (IsString)
import Data.Word (Word16)
import GHC.Num (Num (fromInteger))
import Shrun.Configuration.Default (Default (def))
import Shrun.Prelude
import Shrun.Utils qualified as U
import TOML (Value (Integer, String))

-- | Determines notification timeout.
data NotifyTimeout
= -- | Times out after the given seconds.
NotifyTimeoutSeconds Word16
| -- | Never times out.
NotifyTimeoutNever
deriving stock (Eq, Show)

instance Default NotifyTimeout where
def = NotifyTimeoutSeconds 10

instance FromInteger NotifyTimeout where
afromInteger = NotifyTimeoutSeconds . fromInteger

-- DecodeTOML instance does not reuse parseNotifyTimeout as we want to
-- enforce the integer type.

instance DecodeTOML NotifyTimeout where
tomlDecoder = makeDecoder $ \case
String "never" -> pure NotifyTimeoutNever
String bad -> invalidValue strErr (String bad)
Integer i -> case toIntegralSized i of
Just i' -> pure $ NotifyTimeoutSeconds i'
Nothing -> invalidValue tooLargeErr (Integer i)
badTy -> typeMismatch badTy
where
tooLargeErr = "Timeout integer too large. Max is: " <> showt maxW16
strErr = "Unexpected timeout. Only valid string is 'never'."
maxW16 = maxBound @Word16

-- | Parses 'NotifyTimeout'.
parseNotifyTimeout :: (MonadFail m) => m Text -> m NotifyTimeout
parseNotifyTimeout getTxt =
getTxt >>= \case
"never" -> pure NotifyTimeoutNever
other -> NotifyTimeoutSeconds <$> U.readStripUnderscores other
{-# INLINEABLE parseNotifyTimeout #-}

-- | Available 'NotifyTimeout' strings.
notifyTimeoutStr :: (IsString a) => a
notifyTimeoutStr = "(never | NATURAL)"
2 changes: 1 addition & 1 deletion src/Shrun/Notify/MonadAppleScript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ where

import Data.Text qualified as T
import Effects.Process.Typed qualified as P
import Shrun.Configuration.Data.Notify.System (NotifySystemP (AppleScript))
import Shrun.Notify.MonadNotify
( NotifyException (MkNotifyException),
ShrunNote,
exitFailureToStderr,
)
import Shrun.Notify.Types (NotifySystemP (AppleScript))
import Shrun.Prelude

-- | Effect for apple script.
Expand Down
Loading

0 comments on commit a7427e8

Please sign in to comment.