{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module System.Linux.Proc.Errors
  ( ProcError (..)
  , renderProcError
  ) where

import           Data.Text (Text)
import qualified Data.Text as Text


data ProcError
  = ProcReadError !FilePath !Text
  | ProcParseError !FilePath !Text
  | ProcMemInfoKeyError !Text
  deriving (ProcError -> ProcError -> Bool
(ProcError -> ProcError -> Bool)
-> (ProcError -> ProcError -> Bool) -> Eq ProcError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ProcError -> ProcError -> Bool
$c/= :: ProcError -> ProcError -> Bool
== :: ProcError -> ProcError -> Bool
$c== :: ProcError -> ProcError -> Bool
Eq, Int -> ProcError -> ShowS
[ProcError] -> ShowS
ProcError -> String
(Int -> ProcError -> ShowS)
-> (ProcError -> String)
-> ([ProcError] -> ShowS)
-> Show ProcError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ProcError] -> ShowS
$cshowList :: [ProcError] -> ShowS
show :: ProcError -> String
$cshow :: ProcError -> String
showsPrec :: Int -> ProcError -> ShowS
$cshowsPrec :: Int -> ProcError -> ShowS
Show)

renderProcError :: ProcError -> Text
renderProcError :: ProcError -> Text
renderProcError = \case
  ProcReadError String
fp Text
msg -> [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
    [ Text
"Error reading '", String -> Text
Text.pack String
fp, Text
"': ", Text
msg ]
  ProcParseError String
fp Text
msg -> [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
    [ Text
"Parser error on file '", String -> Text
Text.pack String
fp, Text
": ", Text
msg ]
  ProcMemInfoKeyError Text
key -> [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat
    [ Text
"MemInfo: Key not found: '", Text
key, Text
"'" ]