packages feed

document-build-0.2.0.0: src/Document/Build/Error/ProcessFailed.hs

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wall #-}

{- | A process that exited with a non-zero exit code.

A 'ProcessFailed' is a product: its fields are named 'commandLine', 'standardOutput' and 'standardError'.
It holds 'CommandLine', so it inherits its names.
-}
module Document.Build.Error.ProcessFailed (
  -- * Type
  ProcessFailed (..),

  -- * Optics
  ProcessFailedLike (..),
  lawNamesProcessFailed,
  InheritProcessFailed,
  inheritProcessFailed,
  inheritsProcessFailed,
) where

import Control.DeepSeq (NFData)
import Control.Lens (Optic, cloneLens, view)
import Data.Data (Data)
import Data.Functor.Const (Const)
import Data.Proxy (Proxy (..))
import Document.Build.CommandLine (CommandLine, CommandLineLike (..), inheritCommandLine)
import Document.Build.Name.CommandLineArguments (CommandLineArgumentsLike (..))
import Document.Build.Name.CommandLineProgram (CommandLineProgramLike (..))
import Document.Build.Name.StandardError (StandardErrorLike (..))
import Document.Build.Name.StandardOutput (StandardOutputLike (..))
import GHC.Generics (Generic)

{- | A process exited with a non-zero exit code: its command line ('commandLine'), standard
output ('standardOutput') and standard error ('standardError').

@since 0.2.0.0
-}
data ProcessFailed
  = ProcessFailed CommandLine String String
  deriving stock (Eq, Ord, Show, Generic, Data)
  deriving anyclass (NFData)

{- | The class of 'ProcessFailed'.

@since 0.2.0.0
-}
class ProcessFailedLike p f x x' where
  processFailed ::
    Optic p f x x' ProcessFailed ProcessFailed

{- | Identity

@since 0.2.0.0
-}
instance (x' ~ ProcessFailed) => ProcessFailedLike p f ProcessFailed x' where
  processFailed =
    id
  {-# INLINE processFailed #-}

{- | Lens: 'commandLine' is the default name of the field of type 'CommandLine'.

@since 0.2.0.0
-}
instance (p ~ (->), Functor f, x' ~ ProcessFailed) => CommandLineLike p f ProcessFailed x' where
  commandLine f (ProcessFailed x1 x2 x3) =
    fmap (\x1' -> ProcessFailed x1' x2 x3) (f x1)
  {-# INLINE commandLine #-}

{- | Lens

@since 0.2.0.0
-}
instance (p ~ (->), Functor f, x' ~ ProcessFailed) => StandardOutputLike p f ProcessFailed x' where
  standardOutput f (ProcessFailed x1 x2 x3) =
    fmap (\x2' -> ProcessFailed x1 x2' x3) (f x2)
  {-# INLINE standardOutput #-}

{- | Lens

@since 0.2.0.0
-}
instance (p ~ (->), Functor f, x' ~ ProcessFailed) => StandardErrorLike p f ProcessFailed x' where
  standardError f (ProcessFailed x1 x2 x3) =
    fmap (ProcessFailed x1 x2) (f x3)
  {-# INLINE standardError #-}

{- | Lens: through 'commandLine', a lens, then 'CommandLine''s 'commandLineProgram', a lens.

@since 0.2.0.0
-}
instance (p ~ (->), Functor f, x' ~ ProcessFailed) => CommandLineProgramLike p f ProcessFailed x' where
  commandLineProgram =
    commandLine . commandLineProgram
  {-# INLINE commandLineProgram #-}

{- | Lens: through 'commandLine', a lens, then 'CommandLine''s 'commandLineArguments', a lens.

@since 0.2.0.0
-}
instance (p ~ (->), Functor f, x' ~ ProcessFailed) => CommandLineArgumentsLike p f ProcessFailed x' where
  commandLineArguments =
    commandLine . commandLineArguments
  {-# INLINE commandLineArguments #-}

{- | Rebuild a value using only the name of each field.

Laws:

> lawNamesProcessFailed == id

@since 0.2.0.0
-}
lawNamesProcessFailed :: ProcessFailed -> ProcessFailed
lawNamesProcessFailed s =
  ProcessFailed
    (view (cloneLens commandLine) s)
    (view (cloneLens standardOutput) s)
    (view (cloneLens standardError) s)

{- | The classes that a data type holding 'ProcessFailed' inherits.

@since 0.2.0.0
-}
type InheritProcessFailed s =
  ( ProcessFailedLike (->) (Const ()) s s
  , CommandLineLike (->) (Const ()) s s
  , StandardOutputLike (->) (Const ()) s s
  , StandardErrorLike (->) (Const ()) s s
  , CommandLineProgramLike (->) (Const ()) s s
  , CommandLineArgumentsLike (->) (Const ()) s s
  )

{- | Check, where it is applied, that @s@ inherits every name of 'ProcessFailed'.

@since 0.2.0.0
-}
inheritProcessFailed :: (InheritProcessFailed s) => proxy s -> ()
inheritProcessFailed _ =
  ()

{- | 'ProcessFailed' inherits every name of 'CommandLine'.

@since 0.2.0.0
-}
inheritsProcessFailed :: [()]
inheritsProcessFailed =
  let p = Proxy :: Proxy ProcessFailed
   in [inheritCommandLine p]