module Data.Diagram.Parser
( DiagramFormat(..)
, readDiagram
)
where
import Control.Monad.Except (ExceptT (..))
import qualified Data.ByteString.Lazy as B
import Data.ByteString.Extra as B (safeReadFile)
import Command.Errors (ErrorCode, ErrorTriplet (..))
import Data.Diagram (Diagram)
import Data.Diagram.Parser.Dot (parseDiagramDot)
import Data.Diagram.Parser.Mermaid (parseDiagramMermaid)
import Data.Either.Extra (mapLeft)
import Data.ExprPair (ExprPair)
import Data.Location (Location (..))
data DiagramFormat = Mermaid
| Dot
deriving (DiagramFormat -> DiagramFormat -> Bool
(DiagramFormat -> DiagramFormat -> Bool)
-> (DiagramFormat -> DiagramFormat -> Bool) -> Eq DiagramFormat
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DiagramFormat -> DiagramFormat -> Bool
== :: DiagramFormat -> DiagramFormat -> Bool
$c/= :: DiagramFormat -> DiagramFormat -> Bool
/= :: DiagramFormat -> DiagramFormat -> Bool
Eq, Int -> DiagramFormat -> ShowS
[DiagramFormat] -> ShowS
DiagramFormat -> String
(Int -> DiagramFormat -> ShowS)
-> (DiagramFormat -> String)
-> ([DiagramFormat] -> ShowS)
-> Show DiagramFormat
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DiagramFormat -> ShowS
showsPrec :: Int -> DiagramFormat -> ShowS
$cshow :: DiagramFormat -> String
show :: DiagramFormat -> String
$cshowList :: [DiagramFormat] -> ShowS
showList :: [DiagramFormat] -> ShowS
Show)
readDiagram :: FilePath
-> DiagramFormat
-> ExprPair
-> ExceptT ErrorTriplet IO Diagram
readDiagram :: String
-> DiagramFormat -> ExprPair -> ExceptT ErrorTriplet IO Diagram
readDiagram String
fp DiagramFormat
format ExprPair
exprP = IO (Either ErrorTriplet Diagram) -> ExceptT ErrorTriplet IO Diagram
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (IO (Either ErrorTriplet Diagram)
-> ExceptT ErrorTriplet IO Diagram)
-> IO (Either ErrorTriplet Diagram)
-> ExceptT ErrorTriplet IO Diagram
forall a b. (a -> b) -> a -> b
$ do
contentEither <- String -> IO (Either String ByteString)
B.safeReadFile String
fp
let diagramE = do
diagFileContent <- Either String ByteString
contentEither
parseDiagram format diagFileContent exprP
pure $ mapLeft
(\String
msg -> Int -> String -> Location -> ErrorTriplet
ErrorTriplet Int
ecCannotReadDiagram String
msg (String -> Location
LocationFile String
fp))
diagramE
parseDiagram :: DiagramFormat
-> B.ByteString
-> ExprPair
-> Either String Diagram
parseDiagram :: DiagramFormat -> ByteString -> ExprPair -> Either String Diagram
parseDiagram DiagramFormat
Dot = ByteString -> ExprPair -> Either String Diagram
parseDiagramDot
parseDiagram DiagramFormat
Mermaid = ByteString -> ExprPair -> Either String Diagram
parseDiagramMermaid
ecCannotReadDiagram :: ErrorCode
ecCannotReadDiagram :: Int
ecCannotReadDiagram = Int
1