{-# LANGUAGE LambdaCase #-}
module Web.Browser
( openBrowser
, openBrowserWithException
) where
import Control.Exception ( Exception (..), SomeException, try)
import Data.Maybe ( isNothing )
import qualified Web.Browser.OS as OS
openBrowser ::
String
-> IO Bool
openBrowser :: String -> IO Bool
openBrowser String
url =
Maybe SomeException -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe SomeException -> Bool)
-> IO (Maybe SomeException) -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> IO (Maybe SomeException)
forall e. Exception e => String -> IO (Maybe e)
openBrowserWithException String
url :: IO (Maybe SomeException))
openBrowserWithException ::
Exception e
=> String
-> IO (Maybe e)
openBrowserWithException :: forall e. Exception e => String -> IO (Maybe e)
openBrowserWithException String
url = IO () -> IO (Either e ())
forall e a. Exception e => IO a -> IO (Either e a)
try (String -> IO ()
OS.openBrowser String
url) IO (Either e ()) -> (Either e () -> IO (Maybe e)) -> IO (Maybe e)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Left e
e -> Maybe e -> IO (Maybe e)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe e -> IO (Maybe e)) -> Maybe e -> IO (Maybe e)
forall a b. (a -> b) -> a -> b
$ e -> Maybe e
forall a. a -> Maybe a
Just e
e
Right () -> Maybe e -> IO (Maybe e)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe e
forall a. Maybe a
Nothing