Add sequence parsing
This commit is contained in:
parent
67aa89a27c
commit
d73e5f5972
22
src/Field.hs
22
src/Field.hs
@ -20,22 +20,25 @@ parseField text constraint
|
|||||||
| isAll = Just (Field All Every)
|
| isAll = Just (Field All Every)
|
||||||
| isNumber = Just (Field (Range number number) Every)
|
| isNumber = Just (Field (Range number number) Every)
|
||||||
| isRange = Just (Field (Range leftBound rightBound) Every)
|
| isRange = Just (Field (Range leftBound rightBound) Every)
|
||||||
|
| isSequence = Just (Field (Sequence numbers) Every)
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
where
|
where
|
||||||
-- All
|
-- All
|
||||||
isAll = parseAll text
|
isAll = parseAll text
|
||||||
-- Number
|
-- Number
|
||||||
numberParseResult = parseNumber text constraint
|
numberParseResult = parseNumber text constraint
|
||||||
isNumber = isJust $ numberParseResult
|
isNumber = isJust numberParseResult
|
||||||
number = fromJust numberParseResult
|
number = fromJust numberParseResult
|
||||||
-- Range
|
-- Range
|
||||||
rangeParseResult = parseRange text constraint
|
rangeParseResult = parseRange text constraint
|
||||||
isRange = isJust $ rangeParseResult
|
isRange = isJust rangeParseResult
|
||||||
rangeValues (Just p) = p
|
rangeValues (Just p) = p
|
||||||
leftBound = fst (rangeValues rangeParseResult)
|
leftBound = fst (rangeValues rangeParseResult)
|
||||||
rightBound = snd (rangeValues rangeParseResult)
|
rightBound = snd (rangeValues rangeParseResult)
|
||||||
-- -- Sequence
|
-- Sequence
|
||||||
-- matchSequence = matchRegex (mkRegex "(([0-9]+)[, ]?)+") s
|
sequenceParseResult = parseSequence text constraint
|
||||||
|
isSequence = isJust sequenceParseResult
|
||||||
|
numbers = fromJust sequenceParseResult
|
||||||
|
|
||||||
parseAll :: String -> Bool
|
parseAll :: String -> Bool
|
||||||
parseAll "*" = True
|
parseAll "*" = True
|
||||||
@ -63,3 +66,14 @@ parseRange text constraint
|
|||||||
start = read (pieces !! 0) :: Int
|
start = read (pieces !! 0) :: Int
|
||||||
end = read (pieces !! 1) :: Int
|
end = read (pieces !! 1) :: Int
|
||||||
isValid = isTwo && isAllNumbers && start <= start && (start, end) `inside` constraint
|
isValid = isTwo && isAllNumbers && start <= start && (start, end) `inside` constraint
|
||||||
|
|
||||||
|
parseSequence :: String -> Constraint -> Maybe [Int]
|
||||||
|
parseSequence text constraint
|
||||||
|
| isValid = Just numbers
|
||||||
|
| otherwise = Nothing
|
||||||
|
where
|
||||||
|
pieces = wordsWhen (== ',') text
|
||||||
|
isAllNumbers = all isNumber pieces
|
||||||
|
numbers = map read pieces
|
||||||
|
allInRange = all (\x -> x `inRange` constraint) numbers
|
||||||
|
isValid = length pieces >= 2 && isAllNumbers && allInRange
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
module FieldSpec (main, spec) where
|
module FieldSpec (main, spec) where
|
||||||
|
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
|
|
||||||
import Constraint
|
import Constraint
|
||||||
import Field
|
import Field
|
||||||
|
|
||||||
@ -20,12 +19,15 @@ spec = do
|
|||||||
it "fails constraints" $
|
it "fails constraints" $
|
||||||
parseNumber "10" (Constraint 0 5) `shouldBe` Nothing
|
parseNumber "10" (Constraint 0 5) `shouldBe` Nothing
|
||||||
|
|
||||||
describe "Field" $ do
|
describe "Field can be created from" $ do
|
||||||
it "can be created from asterisk" $
|
it "asterisk" $
|
||||||
parseField "*" (Constraint 0 0) `shouldBe` Just (Field All Every)
|
parseField "*" (Constraint 0 0) `shouldBe` Just (Field All Every)
|
||||||
|
|
||||||
it "can be created from number" $
|
it "number" $
|
||||||
parseField "10" (Constraint 0 10) `shouldBe` Just (Field (Range 10 10) Every)
|
parseField "10" (Constraint 0 10) `shouldBe` Just (Field (Range 10 10) Every)
|
||||||
|
|
||||||
it "can be created from range" $
|
it "range" $
|
||||||
parseField "10-20" (Constraint 0 59) `shouldBe` Just (Field (Range 10 20) Every)
|
parseField "10-20" (Constraint 0 59) `shouldBe` Just (Field (Range 10 20) Every)
|
||||||
|
|
||||||
|
it "sequence" $
|
||||||
|
parseField "1,2,3" (Constraint 0 59) `shouldBe` Just (Field (Sequence [1, 2, 3]) Every)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
module HelperSpec (main, spec) where
|
module HelperSpec (main, spec) where
|
||||||
|
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
|
|
||||||
import Helper
|
import Helper
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
|
Loading…
Reference in New Issue
Block a user