Add matching and entry point code
This commit is contained in:
26
src/Field.hs
26
src/Field.hs
@ -16,11 +16,21 @@ data Field = Field Range Step
|
||||
deriving (Eq, Show)
|
||||
|
||||
parseField :: String -> Constraint -> Maybe Field
|
||||
parseField text constraint
|
||||
parseField text constraint = parseField' (wordsWhen (== '/') text) constraint
|
||||
|
||||
parseField' :: [String] -> Constraint -> Maybe Field
|
||||
parseField' [rangeText] constraint
|
||||
| isJust range = Just (Field (fromJust range) Every)
|
||||
| otherwise = Nothing
|
||||
where
|
||||
range = parseFieldRange text constraint
|
||||
range = parseFieldRange rangeText constraint
|
||||
parseField' [rangeText, stepText] constraint
|
||||
| isJust range && isJust step = Just (Field (fromJust range) (fromJust step))
|
||||
| otherwise = Nothing
|
||||
where
|
||||
range = parseFieldRange rangeText constraint
|
||||
step = parseFieldStep stepText
|
||||
parseField' _ _ = Nothing
|
||||
|
||||
parseFieldRange :: String -> Constraint -> Maybe Range
|
||||
parseFieldRange text constraint
|
||||
@ -76,3 +86,15 @@ parseFieldStep :: String -> Maybe Step
|
||||
parseFieldStep "" = Just Every
|
||||
parseFieldStep text | isNumber text = Just (Step (read text))
|
||||
parseFieldStep _ = Nothing
|
||||
|
||||
matchField :: Field -> Int -> Bool
|
||||
matchField (Field range step) n = matchFieldRange range n && matchFieldStep step n
|
||||
|
||||
matchFieldRange :: Range -> Int -> Bool
|
||||
matchFieldRange All _ = True
|
||||
matchFieldRange (Range x y) n = n >= x && n <= y
|
||||
matchFieldRange (Sequence xs) n = n `elem` xs
|
||||
|
||||
matchFieldStep :: Step -> Int -> Bool
|
||||
matchFieldStep Every _ = True
|
||||
matchFieldStep (Step x) n = n `mod` x == 0
|
||||
|
@ -1,6 +1,7 @@
|
||||
module Pattern
|
||||
( Pattern(..),
|
||||
match,
|
||||
safeMatch,
|
||||
parse,
|
||||
check,
|
||||
createParts,
|
||||
@ -25,7 +26,7 @@ data Pattern = Pattern {
|
||||
match :: String -> DateTime -> Bool
|
||||
match s d = case parse s of
|
||||
Just p -> check p d
|
||||
Nothing -> error ""
|
||||
Nothing -> error "Parse error"
|
||||
|
||||
safeMatch :: String -> DateTime -> Maybe Bool
|
||||
safeMatch s d = case parse s of
|
||||
@ -81,7 +82,3 @@ check pattern date = all isRight pairs
|
||||
(cyear pattern, year date)
|
||||
]
|
||||
isRight (pattern, value) = matchField pattern value
|
||||
|
||||
matchField :: Field -> Int -> Bool
|
||||
matchField (Field All Every) _ = True
|
||||
matchField (Field (Range f t) Every) x = x >= f && x <= t
|
||||
|
Reference in New Issue
Block a user