WIP: Search parsing
This commit is contained in:
parent
a2434a2306
commit
f0171d78e6
5 changed files with 64 additions and 11 deletions
59
Search.hs
59
Search.hs
|
@ -1,12 +1,14 @@
|
|||
module Search
|
||||
( colors,
|
||||
colorless,
|
||||
parse
|
||||
( parse
|
||||
)
|
||||
where
|
||||
|
||||
import qualified Data.Char as C
|
||||
import qualified Data.Set as Set
|
||||
import qualified Data.Text as T
|
||||
import Text.Parsec
|
||||
import Text.Parsec.Extra
|
||||
import Text.Parsec.Text (Parser)
|
||||
|
||||
data Search = Search [Criteria] deriving (Show)
|
||||
|
||||
|
@ -59,13 +61,48 @@ data Rarity
|
|||
| Mythic
|
||||
deriving (Eq, Ord, Show)
|
||||
|
||||
colors :: [Color] -> Colors
|
||||
colors = Colors . Set.fromList
|
||||
-- colors :: [Color] -> Colors
|
||||
-- colors = Colors . Set.fromList
|
||||
|
||||
colorless :: Colors -> Bool
|
||||
colorless (Colors xs)
|
||||
| Set.null xs = True
|
||||
| otherwise = False
|
||||
-- colorless :: Colors -> Bool
|
||||
-- colorless (Colors xs)
|
||||
-- | Set.null xs = True
|
||||
-- | otherwise = False
|
||||
|
||||
parse :: T.Text -> Search
|
||||
parse _ = Search []
|
||||
parseSearchString :: T.Text -> Search
|
||||
parseSearchString _ = Search []
|
||||
|
||||
bareLiteral :: Parser T.Text
|
||||
bareLiteral = do
|
||||
chars <- many1 $ noneOf " \""
|
||||
return $ T.pack chars
|
||||
|
||||
quotedLiteral :: Parser T.Text
|
||||
quotedLiteral = do
|
||||
chars <- between (char '"') (char '"') (many $ noneOf "\"")
|
||||
return $ T.pack chars
|
||||
|
||||
literal :: Parser T.Text
|
||||
literal = bareLiteral <|> quotedLiteral
|
||||
|
||||
color :: Parser Color
|
||||
color = do
|
||||
c <- oneOf "wubgr"
|
||||
return $ case c of
|
||||
'w' -> White
|
||||
'u' -> Blue
|
||||
'b' -> Black
|
||||
'g' -> Green
|
||||
'r' -> Red
|
||||
|
||||
colors :: Parser Colors
|
||||
colors = Colors <$> Set.fromList <$> many color
|
||||
|
||||
criterion :: Parser Criteria
|
||||
criterion = NameContains <$> literal
|
||||
|
||||
|
||||
search :: Parser Search
|
||||
search = do
|
||||
criteria <- spaces >> sepEndBy criterion spaces
|
||||
return $ Search criteria
|
||||
|
|
|
@ -24,6 +24,8 @@ dependencies:
|
|||
- containers
|
||||
- lens
|
||||
- openapi3
|
||||
- parsec
|
||||
- parsec-extra
|
||||
- servant-openapi3
|
||||
- servant-server
|
||||
- text
|
||||
|
|
|
@ -42,6 +42,7 @@ packages:
|
|||
extra-deps:
|
||||
- hspec-wai-0.11.0@sha256:79f8aab21161cd551e0bb740f5416fb622fe94f710bedc97dabd39fe901b5291,2314
|
||||
- hspec-wai-json-0.11.0@sha256:1fdd66a61c84a9ba6f2e1673ed49b2da1f57f025cd3fb9a46d62620ff9259d63,1629
|
||||
- parsec-extra-0.2.0.0@sha256:47f16e5d00cb62db52d126903787dcccc94d500cfbcb54316f5db00f31da4fa2,735
|
||||
|
||||
# Override default flag values for local packages and extra-deps
|
||||
# flags: {}
|
||||
|
|
|
@ -18,6 +18,13 @@ packages:
|
|||
sha256: c25b93d8e0140a9ea24605199de5065967cd5fceeacac2b7f7d0747ab2142778
|
||||
original:
|
||||
hackage: hspec-wai-json-0.11.0@sha256:1fdd66a61c84a9ba6f2e1673ed49b2da1f57f025cd3fb9a46d62620ff9259d63,1629
|
||||
- completed:
|
||||
hackage: parsec-extra-0.2.0.0@sha256:47f16e5d00cb62db52d126903787dcccc94d500cfbcb54316f5db00f31da4fa2,735
|
||||
pantry-tree:
|
||||
size: 225
|
||||
sha256: 8d8d15ac5aca977855d1fe89f3f23ecbbddc604e3cb887af5c27b540dba49ffc
|
||||
original:
|
||||
hackage: parsec-extra-0.2.0.0@sha256:47f16e5d00cb62db52d126903787dcccc94d500cfbcb54316f5db00f31da4fa2,735
|
||||
snapshots:
|
||||
- completed:
|
||||
size: 616897
|
||||
|
|
|
@ -34,6 +34,8 @@ library
|
|||
, containers
|
||||
, lens
|
||||
, openapi3
|
||||
, parsec
|
||||
, parsec-extra
|
||||
, servant-openapi3
|
||||
, servant-server
|
||||
, text
|
||||
|
@ -56,6 +58,8 @@ executable tutor-exe
|
|||
, containers
|
||||
, lens
|
||||
, openapi3
|
||||
, parsec
|
||||
, parsec-extra
|
||||
, servant-openapi3
|
||||
, servant-server
|
||||
, text
|
||||
|
@ -83,6 +87,8 @@ test-suite tutor-test
|
|||
, hspec-wai-json
|
||||
, lens
|
||||
, openapi3
|
||||
, parsec
|
||||
, parsec-extra
|
||||
, servant-openapi3
|
||||
, servant-server
|
||||
, text
|
||||
|
|
Loading…
Reference in a new issue