Skip to content

Commit

Permalink
warnLeft
Browse files Browse the repository at this point in the history
  • Loading branch information
csasarak committed May 31, 2024
1 parent 6e6a6d3 commit 7a3162f
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Control/Effect/Diagnostics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ module Control.Effect.Diagnostics (
ToDiagnostic (..),
SomeDiagnostic (..),
module Diagnostic,
warnLeft,
) where

import Control.Algebra as X -- intentionally implicit
import Control.Effect.Lift (Lift)
import Control.Effect.Stack (Stack, context)
import Control.Exception (Exception, IOException, SomeException (..))
import Control.Exception.Extra (safeCatch)
import Data.Functor (($>))
import Data.List.NonEmpty qualified as NE
import Data.Maybe (catMaybes)
import Data.Semigroup (sconcat)
Expand Down Expand Up @@ -157,6 +159,11 @@ fatalText = fatal
fromEither :: (ToDiagnostic err, Has Diagnostics sig m) => Either err a -> m a
fromEither = either fatal pure

-- | Sometimes the only thing to do with the @Left@ part of an @Either a b@ is emit it as a warning.
-- This function does that and eliminates the error value.
warnLeft :: (ToDiagnostic warn, Has Diagnostics sig m) => Either warn a -> m (Maybe a)
warnLeft = either (\e -> warn e $> Nothing) (pure . Just)

-- | Lift an Either result into the Diagnostics effect, given a Show instance for the error type
fromEitherShow :: (Show err, Has Diagnostics sig m) => Either err a -> m a
fromEitherShow = either (fatal . toText . show) pure
Expand Down

0 comments on commit 7a3162f

Please sign in to comment.