module Sos where
import Control.Monad.Except
import Data.ByteString (ByteString)
import System.Exit
import Text.Megaparsec (ParseError)
import qualified Data.ByteString.Char8 as BS
data SosException
= SosRegexException
ByteString
String
| SosCommandParseException
ByteString
ParseError
| SosCommandApplyException
[Either Int ByteString]
[ByteString]
String
instance Show SosException where
show (SosRegexException pattern err) = "Error compiling regex '" ++ BS.unpack pattern ++ "': " ++ err
show (SosCommandParseException template err) = "Error parsing command '" ++ BS.unpack template ++ "': " ++ show err
show (SosCommandApplyException template vars err) = "Error applying template '" ++ reconstruct template ++ "' to " ++ show vars ++ ": " ++ err
where
reconstruct :: [Either Int ByteString] -> String
reconstruct = concatMap
(\case
Left n -> '\\' : show n
Right bs -> BS.unpack bs)
type Sos a = ExceptT SosException IO a
runSos :: Sos a -> IO a
runSos act =
runExceptT act >>= \case
Left err -> do
print err
exitFailure
Right x -> pure x