Skip to content

Commit fa738b1

Browse files
authored
Merge pull request #103 from hackberrydev/schedule-parser-errors
Return tuple with tasks or errors from schedule parser
2 parents 97e5b4b + 388c88c commit fa738b1

7 files changed

Lines changed: 67 additions & 14 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,17 @@ schedule tasks and help you stay in touch with your contacts.
3232

3333
For more information, visit the the main [Alas
3434
website](https://www.hackberry.dev/alas/).
35+
36+
## Development
37+
38+
Install dependencies with:
39+
40+
```sh
41+
jpm deps
42+
```
43+
44+
Run tests with:
45+
46+
```sh
47+
jpm test
48+
```

TODO.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/commands/schedule_tasks.janet

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
(import ../file_repository)
1010
(import ../schedule_parser)
1111

12+
(def command "--schedule-tasks")
1213
(def weekdays ["Monday" "Tuesday" "Wednesday" "Thursday" "Friday"])
1314

1415
(defn- remove-year [formatted-date]
@@ -48,6 +49,9 @@
4849
(task/mark-as-missed task (day :date))))
4950
tasks))
5051

52+
(defn- format-parse-errors [errors]
53+
(map (fn [error] (string command " " (string/ascii-lower error) ".")) errors))
54+
5155
## —————————————————————————————————————————————————————————————————————————————————————————————————
5256
## Public Interface
5357

@@ -70,12 +74,10 @@
7074
(let [load-file-result (file_repository/load argument)
7175
error (load-file-result :error)]
7276
(if error
73-
{:errors [(string "--schedule-tasks " (string/ascii-lower error))]}
74-
(let [parse-result (schedule_parser/parse (load-file-result :text))]
75-
(if parse-result
76-
(let [schedule (first parse-result)]
77-
(if (empty? schedule)
78-
{:errors ["--schedule-tasks schedule is empty."]}
79-
{:command [schedule-tasks (first parse-result) (date/today)]}))
80-
{:errors ["--schedule-tasks schedule could not be parsed."]}))))
77+
{:errors [(string command " " (string/ascii-lower error))]}
78+
(let [parse-result (schedule_parser/parse (load-file-result :text))
79+
errors (parse-result :errors)]
80+
(if errors
81+
{:errors (format-parse-errors errors)}
82+
{:command [schedule-tasks (parse-result :tasks) (date/today)]}))))
8183
{}))

src/schedule_parser.janet

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717

1818
(defn parse
1919
```
20-
Parses schedule string and returns an array of task entities.
20+
Parses schedule-string and returns a tuple:
21+
22+
- {:tasks tasks} Where tasks is an array of task entities, when parsing was successfull.
23+
- {:errors errors} Where errors is an array of strings.
2124
```
2225
[schedule-string]
23-
(peg/match schedule-grammar schedule-string))
26+
(let [parse-result (peg/match schedule-grammar schedule-string)]
27+
(if parse-result
28+
(let [tasks (first parse-result)]
29+
(if (empty? tasks)
30+
{:errors ["Schedule is empty"]}
31+
{:tasks tasks}))
32+
{:errors ["Schedule can not be parsed"]})))

test/commands/schedule_tasks_test.janet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
(def arguments {"schedule-tasks" "test/examples/unparsable-schedule.md"})
200200
(def result (build-command arguments))
201201
(is (nil? (result :command)))
202-
(is (= "--schedule-tasks schedule could not be parsed." (first (result :errors)))))
202+
(is (= "--schedule-tasks schedule can not be parsed." (first (result :errors)))))
203203

204204
(deftest build-command-when-schedule-is-empty
205205
(def arguments {"schedule-tasks" "test/examples/empty-schedule.md"})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Scheduled Tasks
2+
3+
- Weekly Meeting (every Tuesday)
4+
- Deploy the web app (every weekday)
5+
- Pay football practice
6+
- Martha's birthsday (every 05-24)
7+
- Meeting with Jack (on 2022-05-03)

test/schedule_parser_test.janet

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
(import ../src/date :as d)
33
(import ../src/schedule_parser)
44

5+
## —————————————————————————————————————————————————————————————————————————————————————————————————
6+
## Test parse-schedule
7+
58
(deftest parse-schedule
69
(def schedule-string
710
```
@@ -11,10 +14,11 @@
1114
- Puzzle Storm on Lichess (every day)
1215
- Deploy the web app (every weekday)
1316
- Pay football practice (every month)
14-
- Martha's birthsday (every 05-24)
17+
- Martha's birthday (every 05-24)
1518
- Meeting with Jack (on 2022-05-03)
1619
```)
17-
(def scheduled-tasks (first (schedule_parser/parse schedule-string)))
20+
(def result (schedule_parser/parse schedule-string))
21+
(def scheduled-tasks (result :tasks))
1822
(is (= 6 (length scheduled-tasks)))
1923
(let [task (scheduled-tasks 0)]
2024
(is (= "Weekly Meeting" (task :title)))
@@ -29,4 +33,22 @@
2933
(is (= false (task :done)))
3034
(is (= "on 2022-05-03" (task :schedule)))))
3135

36+
(deftest parse-schedule-when-schedule-can-not-be-parsed
37+
(def schedule-string
38+
```
39+
## Schedule
40+
41+
* One (always)
42+
```)
43+
(def result (schedule_parser/parse schedule-string))
44+
(is (= "Schedule can not be parsed" (first (result :errors)))))
45+
46+
(deftest parse-schedule-when-schedule-is-empty
47+
(def schedule-string
48+
```
49+
# Scheduled Tasks
50+
```)
51+
(def result (schedule_parser/parse schedule-string))
52+
(is (= "Schedule is empty" (first (result :errors)))))
53+
3254
(run-tests!)

0 commit comments

Comments
 (0)