Rename data structures

This commit is contained in:
Anton Vakhrushev 2017-11-11 16:55:03 +03:00
parent 47c65c9b25
commit e57e75a905

View File

@ -14,15 +14,15 @@ data Range = Any | Pair Int Int | Sequence [Int]
data Step = All | Value Int data Step = All | Value Int
data CronItemPattern = CronItemPattern Range Step data Field = Field Range Step
data CronPattern = CronPattern { data Pattern = Pattern {
cminute :: CronItemPattern, cminute :: Field,
chour :: CronItemPattern, chour :: Field,
cday :: CronItemPattern, cday :: Field,
cmonth :: CronItemPattern, cmonth :: Field,
cweek :: CronItemPattern, cweek :: Field,
cyear :: CronItemPattern cyear :: Field
} }
match :: String -> DateTime -> Bool match :: String -> DateTime -> Bool
@ -35,14 +35,14 @@ safeMatch s d = case parse s of
Just p -> Just (check p d) Just p -> Just (check p d)
Nothing -> Nothing Nothing -> Nothing
parse :: String -> Maybe CronPattern parse :: String -> Maybe Pattern
parse s parse s
| isInvalid = Nothing | isInvalid = Nothing
| otherwise = Just (createPattern $ catMaybes parts) | otherwise = Just (createPattern $ catMaybes parts)
where where
parts = createParts s parts = createParts s
isInvalid = checkParts parts == False isInvalid = checkParts parts == False
createPattern xs = CronPattern { createPattern xs = Pattern {
cminute = xs !! 0, cminute = xs !! 0,
chour = xs !! 1, chour = xs !! 1,
cday = xs !! 2, cday = xs !! 2,
@ -57,29 +57,29 @@ createParts s = map f $ zip parsers (words s)
where where
f (g, s) = g s f (g, s) = g s
checkParts :: [Maybe CronItemPattern] -> Bool checkParts :: [Maybe Field] -> Bool
checkParts xs checkParts xs
| length xs /= 6 = False | length xs /= 6 = False
| any isNothing xs = False | any isNothing xs = False
| otherwise = True | otherwise = True
parseCronItemPattern :: (Int, Int) -> String -> Maybe CronItemPattern parseField :: (Int, Int) -> String -> Maybe Field
parseCronItemPattern (f, t) s parseField (f, t) s
| s == "*" = Just (CronItemPattern Any All) | s == "*" = Just (Field Any All)
| validNumber == True = Just (CronItemPattern (Pair x x) All) | validNumber == True = Just (Field (Pair x x) All)
| otherwise = Nothing | otherwise = Nothing
where where
x = read s :: Int x = read s :: Int
validNumber = all isDigit s && x >= f && x <= t validNumber = all isDigit s && x >= f && x <= t
parseMinute = parseCronItemPattern (0, 59) parseMinute = parseField (0, 59)
parseHour = parseCronItemPattern (0, 59) parseHour = parseField (0, 59)
parseDay = parseCronItemPattern (1, 31) parseDay = parseField (1, 31)
parseMonth = parseCronItemPattern (1, 12) parseMonth = parseField (1, 12)
parseWeek = parseCronItemPattern (1, 7) parseWeek = parseField (1, 7)
parseYear = parseCronItemPattern (0, 9999) parseYear = parseField (0, 9999)
check :: CronPattern -> DateTime -> Bool check :: Pattern -> DateTime -> Bool
check pattern date = all isRight pairs check pattern date = all isRight pairs
where where
pairs = [ (cminute pattern, minute date), pairs = [ (cminute pattern, minute date),
@ -89,8 +89,8 @@ check pattern date = all isRight pairs
(cweek pattern, weekdayNumber $ dateWeekDay date), (cweek pattern, weekdayNumber $ dateWeekDay date),
(cyear pattern, year date) (cyear pattern, year date)
] ]
isRight (pattern, value) = matchItemPattern pattern value isRight (pattern, value) = matchField pattern value
matchItemPattern :: CronItemPattern -> Int -> Bool matchField :: Field -> Int -> Bool
matchItemPattern (CronItemPattern Any All) _ = True matchField (Field Any All) _ = True
matchItemPattern (CronItemPattern (Pair f t) All) x = x >= f && x <= t matchField (Field (Pair f t) All) x = x >= f && x <= t