From d9031e8767ab0eedbd8dbe677edf5bfdeb6dfedf Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Sat, 9 Mar 2024 14:25:05 +0100 Subject: chore: support json output --- sh.cabal | 1 + src/Process/Shell.hs | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/sh.cabal b/sh.cabal index d5fce58..da19b23 100644 --- a/sh.cabal +++ b/sh.cabal @@ -14,6 +14,7 @@ library default-language: GHC2021 ghc-options: -Wall build-depends: + aeson, base, bytestring, haskell-src-meta, diff --git a/src/Process/Shell.hs b/src/Process/Shell.hs index 87d22df..24bea95 100644 --- a/src/Process/Shell.hs +++ b/src/Process/Shell.hs @@ -9,11 +9,15 @@ module Process.Shell ( sh, Quotable (..), + ExitCodeException (..), + DecodeException (..), ) where +import Control.Exception (Exception, throw) import Control.Monad import Control.Monad.Reader +import Data.Aeson import Data.ByteString qualified as B import Data.ByteString.Lazy qualified as LB import Data.ByteString.Lazy.UTF8 qualified as LB @@ -67,6 +71,14 @@ instance Outputable T.Text where instance Outputable LT.Text where fromLBS = LT.decodeUtf8 +data DecodeException = DecodeException + deriving (Show) + +instance Exception DecodeException + +instance (FromJSON a) => Outputable a where + fromLBS = fromMaybe (throw DecodeException) . decode + class Quotable a where toString :: a -> String default toString :: (Show a) => a -> String -- cgit v1.2.3