blob: b7855f825aed3768997f70fbae0d06235dd3d5c0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
{-# LANGUAGE OverloadedStrings #-}
module Model.Message.Parts
( replaceParts
, getParts
) where
import Data.Maybe (listToMaybe, fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Text.ParserCombinators.Parsec
replaceParts :: [Text] -> [Part] -> Text
replaceParts values = T.concat . map (replacePart values)
replacePart :: [Text] -> Part -> Text
replacePart _ (Str str) = str
replacePart values (Num n) =
fromMaybe (T.concat ["{", T.pack (show n), "}"]) . listToMaybe . drop (n - 1) $ values
data Part =
Num Int
| Str Text
getParts :: Text -> [Part]
getParts str =
case parse partsParser "" (T.unpack str) of
Right parts -> parts
Left _ -> []
partsParser :: Parser [Part]
partsParser = many partParser
partParser :: Parser Part
partParser =
(do _ <- string "{"; n <- read <$> many1 digit; _ <- string "}"; return (Num n))
<|> (do str <- T.pack <$> many1 (noneOf "{"); return (Str str))
|