-- |
-- Module      : Data.TypeID.Error
-- License     : MIT
-- Maintainer  : mmzk1526@outlook.com
-- Portability : GHC
--
-- TypeID Error type.
--
module Data.TypeID.Error
 (
  -- * Data type
  TypeIDError(..)
 ) where

import           Control.Exception
import           Data.Text (Text)

-- | Errors from parsing TypeIDs.
data TypeIDError
  = -- | The prefix longer than 63 characters.
    TypeIDErrorPrefixTooLong Int
    -- | The ID contains an extra underscore separator.
  | TypeIDExtraSeparator
    -- | The ID starts with an underscore separator.
  | TypeIDStartWithUnderscore
    -- | The ID ends with an underscore separator.
  | TypeIDEndWithUnderscore
    -- | The prefix contains an invalid character, namely not lowercase Latin.
  | TypeIDErrorPrefixInvalidChar Char
    -- | From a `Data.KindID.KindID` conversion. The prefix doesn't match with
    -- the expected.
  | TypeIDErrorPrefixMismatch Text Text
    -- | The 'Data.UUID.Types.Internal.UUID' suffix has errors.
  | TypeIDErrorUUIDError
  deriving (TypeIDError -> TypeIDError -> Bool
(TypeIDError -> TypeIDError -> Bool)
-> (TypeIDError -> TypeIDError -> Bool) -> Eq TypeIDError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TypeIDError -> TypeIDError -> Bool
== :: TypeIDError -> TypeIDError -> Bool
$c/= :: TypeIDError -> TypeIDError -> Bool
/= :: TypeIDError -> TypeIDError -> Bool
Eq, Eq TypeIDError
Eq TypeIDError
-> (TypeIDError -> TypeIDError -> Ordering)
-> (TypeIDError -> TypeIDError -> Bool)
-> (TypeIDError -> TypeIDError -> Bool)
-> (TypeIDError -> TypeIDError -> Bool)
-> (TypeIDError -> TypeIDError -> Bool)
-> (TypeIDError -> TypeIDError -> TypeIDError)
-> (TypeIDError -> TypeIDError -> TypeIDError)
-> Ord TypeIDError
TypeIDError -> TypeIDError -> Bool
TypeIDError -> TypeIDError -> Ordering
TypeIDError -> TypeIDError -> TypeIDError
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: TypeIDError -> TypeIDError -> Ordering
compare :: TypeIDError -> TypeIDError -> Ordering
$c< :: TypeIDError -> TypeIDError -> Bool
< :: TypeIDError -> TypeIDError -> Bool
$c<= :: TypeIDError -> TypeIDError -> Bool
<= :: TypeIDError -> TypeIDError -> Bool
$c> :: TypeIDError -> TypeIDError -> Bool
> :: TypeIDError -> TypeIDError -> Bool
$c>= :: TypeIDError -> TypeIDError -> Bool
>= :: TypeIDError -> TypeIDError -> Bool
$cmax :: TypeIDError -> TypeIDError -> TypeIDError
max :: TypeIDError -> TypeIDError -> TypeIDError
$cmin :: TypeIDError -> TypeIDError -> TypeIDError
min :: TypeIDError -> TypeIDError -> TypeIDError
Ord)

instance Show TypeIDError where
  show :: TypeIDError -> String
  show :: TypeIDError -> String
show (TypeIDErrorPrefixTooLong Int
n)
    = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"Prefix with ", Int -> String
forall a. Show a => a -> String
show Int
n, String
" characters is too long!"]
  show TypeIDError
TypeIDExtraSeparator
    = String
"The underscore separator should not be present if the prefix is empty!"
  show TypeIDError
TypeIDStartWithUnderscore
    = String
"The prefix should not start with an underscore!"
  show TypeIDError
TypeIDEndWithUnderscore
    = String
"The prefix should not end with an underscore!"
  show (TypeIDErrorPrefixInvalidChar Char
c)
    = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"Prefix contains invalid character ", Char -> String
forall a. Show a => a -> String
show Char
c, String
"!"]
  show (TypeIDErrorPrefixMismatch Text
expPrefix Text
actPrefix)
    = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ String
"Expected prefix ", Text -> String
forall a. Show a => a -> String
show Text
expPrefix, String
" but got "
             , Text -> String
forall a. Show a => a -> String
show Text
actPrefix, String
"!" ]
  show TypeIDError
TypeIDErrorUUIDError
    = String
"Invalid UUID part!"
  {-# INLINE show #-}

instance Exception TypeIDError