{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE NoImplicitPrelude         #-}
{-# LANGUAGE OverloadedStrings         #-}
{-# LANGUAGE StrictData                #-}

{-|
Module      : Headroom.Types
Description : Application data types
Copyright   : (c) 2019-2020 Vaclav Svejcar
License     : BSD-3-Clause
Maintainer  : vaclav.svejcar@gmail.com
Stability   : experimental
Portability : POSIX

Module containing most of the data types used by the application.
-}

module Headroom.Types
  ( -- * Error Data Types
    HeadroomError(..)
    -- ** Helper Functions
  , fromHeadroomError
  , toHeadroomError
    -- * Other Data Types
  , TemplateMeta(..)
  , HaddockFieldOffsets(..)
  , CurrentYear(..)
  )
where

import           Data.Default.Class             ( Default(..) )
import           Data.Typeable                  ( cast )
import           RIO


-- | Top-level of the /Headroom/ exception hierarchy.
data HeadroomError = forall e . Exception e => HeadroomError e

instance Show HeadroomError where
  show :: HeadroomError -> String
show (HeadroomError e
he) = e -> String
forall a. Show a => a -> String
show e
he

instance Exception HeadroomError where
  displayException :: HeadroomError -> String
displayException (HeadroomError e
he) = e -> String
forall e. Exception e => e -> String
displayException e
he


-- | Wraps given exception into 'HeadroomError'.
toHeadroomError :: Exception e
                => e
                -- ^ exception to wrap
                -> SomeException
                -- ^ wrapped exception
toHeadroomError :: e -> SomeException
toHeadroomError = HeadroomError -> SomeException
forall e. Exception e => e -> SomeException
toException (HeadroomError -> SomeException)
-> (e -> HeadroomError) -> e -> SomeException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> HeadroomError
forall e. Exception e => e -> HeadroomError
HeadroomError


-- | Unwraps given exception from 'HeadroomError'.
fromHeadroomError :: Exception e
                  => SomeException
                  -- ^ exception to unwrap
                  -> Maybe e
                  -- ^ unwrapped exception
fromHeadroomError :: SomeException -> Maybe e
fromHeadroomError SomeException
e = do
  HeadroomError e
he <- SomeException -> Maybe HeadroomError
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e
  e -> Maybe e
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast e
he


-- | Wraps the value of current year.
newtype CurrentYear = CurrentYear
  { CurrentYear -> Integer
unCurrentYear :: Integer
  -- ^ value of current year
  }
  deriving (CurrentYear -> CurrentYear -> Bool
(CurrentYear -> CurrentYear -> Bool)
-> (CurrentYear -> CurrentYear -> Bool) -> Eq CurrentYear
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CurrentYear -> CurrentYear -> Bool
$c/= :: CurrentYear -> CurrentYear -> Bool
== :: CurrentYear -> CurrentYear -> Bool
$c== :: CurrentYear -> CurrentYear -> Bool
Eq, Int -> CurrentYear -> ShowS
[CurrentYear] -> ShowS
CurrentYear -> String
(Int -> CurrentYear -> ShowS)
-> (CurrentYear -> String)
-> ([CurrentYear] -> ShowS)
-> Show CurrentYear
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CurrentYear] -> ShowS
$cshowList :: [CurrentYear] -> ShowS
show :: CurrentYear -> String
$cshow :: CurrentYear -> String
showsPrec :: Int -> CurrentYear -> ShowS
$cshowsPrec :: Int -> CurrentYear -> ShowS
Show)


-- | Offsets for selected fields extracted from /Haddock module header/.
data HaddockFieldOffsets = HaddockFieldOffsets
  { HaddockFieldOffsets -> Maybe Int
hfoCopyright :: Maybe Int
  -- ^ offset for /Copyright/ field
  }
  deriving (HaddockFieldOffsets -> HaddockFieldOffsets -> Bool
(HaddockFieldOffsets -> HaddockFieldOffsets -> Bool)
-> (HaddockFieldOffsets -> HaddockFieldOffsets -> Bool)
-> Eq HaddockFieldOffsets
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HaddockFieldOffsets -> HaddockFieldOffsets -> Bool
$c/= :: HaddockFieldOffsets -> HaddockFieldOffsets -> Bool
== :: HaddockFieldOffsets -> HaddockFieldOffsets -> Bool
$c== :: HaddockFieldOffsets -> HaddockFieldOffsets -> Bool
Eq, Int -> HaddockFieldOffsets -> ShowS
[HaddockFieldOffsets] -> ShowS
HaddockFieldOffsets -> String
(Int -> HaddockFieldOffsets -> ShowS)
-> (HaddockFieldOffsets -> String)
-> ([HaddockFieldOffsets] -> ShowS)
-> Show HaddockFieldOffsets
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HaddockFieldOffsets] -> ShowS
$cshowList :: [HaddockFieldOffsets] -> ShowS
show :: HaddockFieldOffsets -> String
$cshow :: HaddockFieldOffsets -> String
showsPrec :: Int -> HaddockFieldOffsets -> ShowS
$cshowsPrec :: Int -> HaddockFieldOffsets -> ShowS
Show)

instance Default HaddockFieldOffsets where
  def :: HaddockFieldOffsets
def = HaddockFieldOffsets :: Maybe Int -> HaddockFieldOffsets
HaddockFieldOffsets { hfoCopyright :: Maybe Int
hfoCopyright = Maybe Int
forall a. Maybe a
Nothing }

-- | Metadata parsed from raw /template/, specific for selected /file type/.
data TemplateMeta = HaskellTemplateMeta HaddockFieldOffsets
  deriving (TemplateMeta -> TemplateMeta -> Bool
(TemplateMeta -> TemplateMeta -> Bool)
-> (TemplateMeta -> TemplateMeta -> Bool) -> Eq TemplateMeta
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TemplateMeta -> TemplateMeta -> Bool
$c/= :: TemplateMeta -> TemplateMeta -> Bool
== :: TemplateMeta -> TemplateMeta -> Bool
$c== :: TemplateMeta -> TemplateMeta -> Bool
Eq, Int -> TemplateMeta -> ShowS
[TemplateMeta] -> ShowS
TemplateMeta -> String
(Int -> TemplateMeta -> ShowS)
-> (TemplateMeta -> String)
-> ([TemplateMeta] -> ShowS)
-> Show TemplateMeta
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TemplateMeta] -> ShowS
$cshowList :: [TemplateMeta] -> ShowS
show :: TemplateMeta -> String
$cshow :: TemplateMeta -> String
showsPrec :: Int -> TemplateMeta -> ShowS
$cshowsPrec :: Int -> TemplateMeta -> ShowS
Show)