------------------------------------------------------------------------------
-- |
-- Module      : LiterateX.Types.SourceFormat
-- Description : source format type
-- Copyright   : Copyright (c) 2021 Travis Cardwell
-- License     : MIT
------------------------------------------------------------------------------

{-# LANGUAGE LambdaCase #-}

module LiterateX.Types.SourceFormat
  ( -- * Type
    SourceFormat(..)
    -- * API
  , describe
  , list
  ) where

-- https://hackage.haskell.org/package/ttc
import qualified Data.TTC as TTC

------------------------------------------------------------------------------
-- $Type

-- | Source format
--
-- This sum type defines the supported source formats.
--
-- @since 0.0.1.0
data SourceFormat
  = DoubleDash       -- ^ \-- comments
  | DoubleSlash      -- ^ // comments
  | Hash             -- ^ # comments
  | LispSemicolons   -- ^ Lisp semicolon comments
  | LiterateHaskell  -- ^ literate Haskell
  | Percent          -- ^ % comments
  deriving (SourceFormat
SourceFormat -> SourceFormat -> Bounded SourceFormat
forall a. a -> a -> Bounded a
maxBound :: SourceFormat
$cmaxBound :: SourceFormat
minBound :: SourceFormat
$cminBound :: SourceFormat
Bounded, Int -> SourceFormat
SourceFormat -> Int
SourceFormat -> [SourceFormat]
SourceFormat -> SourceFormat
SourceFormat -> SourceFormat -> [SourceFormat]
SourceFormat -> SourceFormat -> SourceFormat -> [SourceFormat]
(SourceFormat -> SourceFormat)
-> (SourceFormat -> SourceFormat)
-> (Int -> SourceFormat)
-> (SourceFormat -> Int)
-> (SourceFormat -> [SourceFormat])
-> (SourceFormat -> SourceFormat -> [SourceFormat])
-> (SourceFormat -> SourceFormat -> [SourceFormat])
-> (SourceFormat -> SourceFormat -> SourceFormat -> [SourceFormat])
-> Enum SourceFormat
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: SourceFormat -> SourceFormat -> SourceFormat -> [SourceFormat]
$cenumFromThenTo :: SourceFormat -> SourceFormat -> SourceFormat -> [SourceFormat]
enumFromTo :: SourceFormat -> SourceFormat -> [SourceFormat]
$cenumFromTo :: SourceFormat -> SourceFormat -> [SourceFormat]
enumFromThen :: SourceFormat -> SourceFormat -> [SourceFormat]
$cenumFromThen :: SourceFormat -> SourceFormat -> [SourceFormat]
enumFrom :: SourceFormat -> [SourceFormat]
$cenumFrom :: SourceFormat -> [SourceFormat]
fromEnum :: SourceFormat -> Int
$cfromEnum :: SourceFormat -> Int
toEnum :: Int -> SourceFormat
$ctoEnum :: Int -> SourceFormat
pred :: SourceFormat -> SourceFormat
$cpred :: SourceFormat -> SourceFormat
succ :: SourceFormat -> SourceFormat
$csucc :: SourceFormat -> SourceFormat
Enum, SourceFormat -> SourceFormat -> Bool
(SourceFormat -> SourceFormat -> Bool)
-> (SourceFormat -> SourceFormat -> Bool) -> Eq SourceFormat
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SourceFormat -> SourceFormat -> Bool
$c/= :: SourceFormat -> SourceFormat -> Bool
== :: SourceFormat -> SourceFormat -> Bool
$c== :: SourceFormat -> SourceFormat -> Bool
Eq, Eq SourceFormat
Eq SourceFormat
-> (SourceFormat -> SourceFormat -> Ordering)
-> (SourceFormat -> SourceFormat -> Bool)
-> (SourceFormat -> SourceFormat -> Bool)
-> (SourceFormat -> SourceFormat -> Bool)
-> (SourceFormat -> SourceFormat -> Bool)
-> (SourceFormat -> SourceFormat -> SourceFormat)
-> (SourceFormat -> SourceFormat -> SourceFormat)
-> Ord SourceFormat
SourceFormat -> SourceFormat -> Bool
SourceFormat -> SourceFormat -> Ordering
SourceFormat -> SourceFormat -> SourceFormat
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
min :: SourceFormat -> SourceFormat -> SourceFormat
$cmin :: SourceFormat -> SourceFormat -> SourceFormat
max :: SourceFormat -> SourceFormat -> SourceFormat
$cmax :: SourceFormat -> SourceFormat -> SourceFormat
>= :: SourceFormat -> SourceFormat -> Bool
$c>= :: SourceFormat -> SourceFormat -> Bool
> :: SourceFormat -> SourceFormat -> Bool
$c> :: SourceFormat -> SourceFormat -> Bool
<= :: SourceFormat -> SourceFormat -> Bool
$c<= :: SourceFormat -> SourceFormat -> Bool
< :: SourceFormat -> SourceFormat -> Bool
$c< :: SourceFormat -> SourceFormat -> Bool
compare :: SourceFormat -> SourceFormat -> Ordering
$ccompare :: SourceFormat -> SourceFormat -> Ordering
$cp1Ord :: Eq SourceFormat
Ord, Int -> SourceFormat -> ShowS
[SourceFormat] -> ShowS
SourceFormat -> String
(Int -> SourceFormat -> ShowS)
-> (SourceFormat -> String)
-> ([SourceFormat] -> ShowS)
-> Show SourceFormat
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SourceFormat] -> ShowS
$cshowList :: [SourceFormat] -> ShowS
show :: SourceFormat -> String
$cshow :: SourceFormat -> String
showsPrec :: Int -> SourceFormat -> ShowS
$cshowsPrec :: Int -> SourceFormat -> ShowS
Show)

instance TTC.Parse SourceFormat where
  parse :: t -> Either e SourceFormat
parse = String -> Bool -> Bool -> t -> Either e SourceFormat
forall a t e.
(Bounded a, Enum a, Render a, Textual t, Textual e) =>
String -> Bool -> Bool -> t -> Either e a
TTC.parseEnum' String
"source format" Bool
True Bool
False

instance TTC.Render SourceFormat where
  render :: SourceFormat -> t
render = String -> t
forall t. Textual t => String -> t
TTC.fromS (String -> t) -> (SourceFormat -> String) -> SourceFormat -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
    SourceFormat
DoubleDash      -> String
"ddash"
    SourceFormat
DoubleSlash     -> String
"dslash"
    SourceFormat
Hash            -> String
"hash"
    SourceFormat
LispSemicolons  -> String
"lisp"
    SourceFormat
LiterateHaskell -> String
"lhs"
    SourceFormat
Percent         -> String
"percent"

------------------------------------------------------------------------------
-- $API

-- | Get a description of a source format
--
-- @since 0.0.1.0
describe :: SourceFormat -> String
describe :: SourceFormat -> String
describe = \case
    SourceFormat
DoubleDash      -> String
"-- comments"
    SourceFormat
DoubleSlash     -> String
"// comments"
    SourceFormat
Hash            -> String
"# comments"
    SourceFormat
LispSemicolons  -> String
"Lisp semicolon comments"
    SourceFormat
LiterateHaskell -> String
"literate Haskell"
    SourceFormat
Percent         -> String
"% comments"

------------------------------------------------------------------------------

-- | List of all supported source formats
--
-- @since 0.0.1.0
list :: [SourceFormat]
list :: [SourceFormat]
list = [SourceFormat
forall a. Bounded a => a
minBound ..]