Skip to content

Commit 160f496

Browse files
authored
Merge pull request #107 from hackberrydev/scheduled-task-without-time
Scheduled task without time
2 parents fa64fc8 + d829f8e commit 160f496

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/schedule_parser.janet

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### ————————————————————————————————————————————————————————————————————————————
1+
### ————————————————————————————————————————————————————————————————————————————————————————————————
22
### This module implements a PEG parser that parses schedule string into
33
### task entities.
44

@@ -9,10 +9,18 @@
99
:title (* "# " (some (+ :w+ :s+)))
1010
:tasks (group (any :task))
1111
:task (replace (* "- " :task-title :task-schedule (? "\n")) ,task/build-scheduled-task)
12-
:task-title (replace (capture (some (if-not "(" 1))) ,string/trim)
12+
:task-title (replace (capture (some (if-not (+ "(" "\n") 1))) ,string/trim)
1313
:task-schedule (* "(" (replace (capture (some (+ :w+ :s+ "-"))) ,string/trim) ")")})
1414

15-
## —————————————————————————————————————————————————————————————————————————————
15+
(defn- task-lines-count
16+
```
17+
Returns the number of lines that start with '-' in the schedule string.
18+
```
19+
[schedule-string]
20+
(length (filter (fn [line] (string/has-prefix? "-" line))
21+
(string/split "\n" schedule-string))))
22+
23+
## —————————————————————————————————————————————————————————————————————————————————————————————————
1624
## Public Interface
1725

1826
(defn parse
@@ -28,5 +36,10 @@
2836
(let [tasks (first parse-result)]
2937
(if (empty? tasks)
3038
{:errors ["Schedule is empty"]}
31-
{:tasks tasks}))
39+
(do
40+
(if (= (length tasks) (task-lines-count schedule-string))
41+
{:tasks tasks}
42+
{:errors [(string "Schedule can not be parsed - last parsed task is \""
43+
((last tasks) :title)
44+
"\"")]}))))
3245
{:errors ["Schedule can not be parsed"]})))

test/schedule_parser_test.janet

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@
4343
(def result (schedule_parser/parse schedule-string))
4444
(is (= "Schedule can not be parsed" (first (result :errors)))))
4545

46+
(deftest parse-schedule-when-task-has-multiple-lines
47+
(def schedule-string
48+
```
49+
# Scheduled Tasks
50+
51+
- Weekly Meeting (every Tuesday)
52+
- Puzzle Storm on Lichess
53+
- Deploy the web app (every weekday)
54+
```)
55+
(def result (schedule_parser/parse schedule-string))
56+
(is (= "Schedule can not be parsed - last parsed task is \"Weekly Meeting\""
57+
(first (result :errors)))))
58+
4659
(deftest parse-schedule-when-schedule-is-empty
4760
(def schedule-string
4861
```

0 commit comments

Comments
 (0)