Skip to content

Commit fa64fc8

Browse files
authored
Merge pull request #106 from hackberrydev/contact-repository-errors
Return array of errors from contacts repository
2 parents c9164fc + 2bbebbb commit fa64fc8

7 files changed

Lines changed: 24 additions & 18 deletions

File tree

src/commands/list_contacts.janet

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### ————————————————————————————————————————————————————————————————————————————————————————————————
22
### This module implements the list contacts command.
33

4+
(import ../utils :prefix "")
45
(import ../date :as d)
56
(import ../contact/repository :as contacts_repository)
67

@@ -32,8 +33,8 @@
3233
(def argument (arguments "list-contacts"))
3334
(if argument
3435
(let [load-result (contacts_repository/load-contacts argument)
35-
error (load-result :error)]
36-
(if error
37-
{:errors [(string "--list-contacts " (string/ascii-lower error))]}
36+
errors (load-result :errors)]
37+
(if errors
38+
{:errors (format-command-errors "--list-contacts" errors)}
3839
{:command [print-contacts (load-result :contacts)]}))
3940
{}))

src/commands/schedule_contacts.janet

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
### ————————————————————————————————————————————————————————————————————————————————————————————————
22
### This module implements a command for scheduling contacts for today in a plan.
33

4+
(import ../utils :prefix "")
5+
46
(import ../contact)
57
(import ../date)
68
(import ../day)
@@ -64,9 +66,9 @@
6466
(def argument (arguments "schedule-contacts"))
6567
(if argument
6668
(let [load-result (contacts_repository/load-contacts argument)
67-
error (load-result :error)
69+
errors (load-result :errors)
6870
contacts (load-result :contacts)]
69-
(if error
70-
{:errors [(string "--schedule-contacts " (string/ascii-lower error))]}
71+
(if errors
72+
{:errors (format-command-errors "--schedule-contacts" errors)}
7173
{:command [schedule-contacts contacts (date/today)]}))
7274
{}))

src/commands/schedule_tasks.janet

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
### ————————————————————————————————————————————————————————————————————————————————————————————————
22
### This module implements a command for scheduling days for today in a plan.
33

4+
(import ../utils :prefix "")
5+
46
(import ../date)
57
(import ../day)
68
(import ../plan)
@@ -49,9 +51,6 @@
4951
(task/mark-as-missed task (day :date))))
5052
tasks))
5153

52-
(defn- format-errors [errors]
53-
(map (fn [error] (string command " " (string/ascii-lower error) ".")) errors))
54-
5554
## —————————————————————————————————————————————————————————————————————————————————————————————————
5655
## Public Interface
5756

@@ -74,10 +73,10 @@
7473
(let [load-file-result (file_repository/load argument)
7574
errors (load-file-result :errors)]
7675
(if errors
77-
{:errors (format-errors errors)}
76+
{:errors (format-command-errors command errors)}
7877
(let [parse-result (schedule_parser/parse (load-file-result :text))
7978
errors (parse-result :errors)]
8079
(if errors
81-
{:errors (format-errors errors)}
80+
{:errors (format-command-errors command errors)}
8281
{:command [schedule-tasks (parse-result :tasks) (date/today)]}))))
8382
{}))

src/commands/stats.janet

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### ————————————————————————————————————————————————————————————————————————————
22
### This module implements stats command.
33

4-
(import ../utils :as "u")
4+
(import ../utils :prefix "")
55
(import ../plan)
66

77
## —————————————————————————————————————————————————————————————————————————————
@@ -12,9 +12,9 @@
1212
Prints stats for the plan. Returns the plan.
1313
```
1414
[plan]
15-
(print (u/pluralize (length (plan :days)) "day"))
16-
(print (u/pluralize (length (plan/completed-tasks plan)) "completed task"))
17-
(print (u/pluralize (length (plan/pending-tasks plan)) "pending task"))
15+
(print (pluralize (length (plan :days)) "day"))
16+
(print (pluralize (length (plan/completed-tasks plan)) "completed task"))
17+
(print (pluralize (length (plan/pending-tasks plan)) "pending task"))
1818
plan)
1919

2020
(defn build-command [arguments &]

src/contact/repository.janet

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
### ————————————————————————————————————————————————————————————————————————————————————————————————
22
### This module implements a repository that loads contacts from Markdown files.
33

4-
(import ../file_repository)
54
(import ../utils)
5+
6+
(import ../file_repository)
67
(import ./parser :as contact_parser)
78

89
(defn- load-contact [contact-path]
@@ -22,4 +23,4 @@
2223
(fn [contact-file] (load-contact (string directory "/" contact-file)))
2324
(os/dir directory)))]
2425
{:contacts contacts})
25-
{:error "Directory does not exist."}))
26+
{:errors ["Directory does not exist"]}))

src/utils.janet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
(if (string/has-suffix? "/" path)
1414
(string/trimr path "/")
1515
path))
16+
17+
(defn format-command-errors [command errors]
18+
(map (fn [error] (string command " " (string/ascii-lower error) ".")) errors))

test/contact/repository_test.janet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
(deftest load-contacts-when-directory-does-not-exist
1616
(def result (load-contacts "test/missing-directory"))
17-
(is (= "Directory does not exist." (result :error))))
17+
(is (= "Directory does not exist" (first (result :errors)))))
1818

1919
(run-tests!)

0 commit comments

Comments
 (0)