-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Notify types to Config namespace
- Loading branch information
Showing
19 changed files
with
201 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.