]>
Commit | Line | Data |
---|---|---|
a9d77a20 FM |
1 | {-# LANGUAGE OverloadedStrings #-} |
2 | ||
3 | module Text.Edifact.BundleReader.CodedSimples | |
4 | ( readCodedSimples | |
5 | ) where | |
6 | ||
7 | import Text.Edifact.BundleReader.Commons | |
8 | import Text.Edifact.BundleReader.Configuration (BundleReaderEnv (..)) | |
9 | import Text.Edifact.BundleReader.Simples (simplesDirectory) | |
10 | import Text.Edifact.Scaffolder.Commons | |
11 | ||
12 | import Text.Edifact.Scaffolder.CodedSimples.Specification | |
13 | ||
14 | import Control.Monad (when) | |
15 | import Control.Monad.Reader (asks) | |
16 | import Data.ByteString as BS (ByteString, | |
17 | readFile) | |
18 | import Formatting | |
19 | ||
20 | readCodedSimples :: [ByteString] -> [SimpleCode] -> BundleReader () | |
21 | readCodedSimples contents simples = do | |
22 | let parsedFile path = parseFile simples =<< liftIO (BS.readFile path) | |
23 | parsedString = parseFile simples | |
24 | files <- asks codedSimplesFiles | |
25 | mapM_ parsedFile files | |
26 | mapM_ parsedString contents | |
27 | ||
28 | parseFile :: [SimpleCode] -> ByteString -> BundleReader [SimpleCode] | |
29 | parseFile simples content = | |
30 | let chunks = tail $ splitFileByDash 70 $ decodeContent content | |
31 | in traverse (parseChunk simples) chunks | |
32 | ||
33 | parseChunk :: [SimpleCode] -> Text -> BundleReader SimpleCode | |
34 | parseChunk simples chunk = do | |
35 | parsed <- parseOrFail chunk specificationParser | |
36 | outputFile <- getOutputFile (fSimpleCodeLower % ".txt") simplesDirectory (fst parsed) | |
37 | when (fst parsed `elem` simples) $ toFile chunk outputFile | |
38 | pure $ fst parsed |