aboutsummaryrefslogtreecommitdiffstats
path: root/worker/lib
diff options
context:
space:
mode:
Diffstat (limited to 'worker/lib')
-rw-r--r--worker/lib/foldermap.go34
-rw-r--r--worker/lib/foldermap_test.go54
2 files changed, 88 insertions, 0 deletions
diff --git a/worker/lib/foldermap.go b/worker/lib/foldermap.go
new file mode 100644
index 00000000..4fd15979
--- /dev/null
+++ b/worker/lib/foldermap.go
@@ -0,0 +1,34 @@
+package lib
+
+import (
+ "fmt"
+ "io"
+
+ "github.com/go-ini/ini"
+)
+
+func ParseFolderMap(r io.Reader) (map[string]string, []string, error) {
+ cfg, err := ini.Load(r)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ sec, err := cfg.GetSection("")
+ if err != nil {
+ return nil, nil, err
+ }
+
+ order := sec.KeyStrings()
+
+ for _, k := range order {
+ v, err := sec.GetKey(k)
+ switch {
+ case v.String() == "":
+ return nil, nil, fmt.Errorf("no value for key '%s'", k)
+ case err != nil:
+ return nil, nil, err
+ }
+ }
+
+ return sec.KeysHash(), order, nil
+}
diff --git a/worker/lib/foldermap_test.go b/worker/lib/foldermap_test.go
new file mode 100644
index 00000000..ec8b6d52
--- /dev/null
+++ b/worker/lib/foldermap_test.go
@@ -0,0 +1,54 @@
+package lib_test
+
+import (
+ "reflect"
+ "strings"
+ "testing"
+
+ "git.sr.ht/~rjarry/aerc/worker/lib"
+)
+
+func TestFolderMap(t *testing.T) {
+ text := `#this is comment
+
+ Sent = [Gmail]/Sent
+
+ # a comment between entries
+ Spam=[Gmail]/Spam # this is comment after the values
+ `
+ fmap, order, err := lib.ParseFolderMap(strings.NewReader(text))
+ if err != nil {
+ t.Errorf("parsing failed: %v", err)
+ }
+
+ want_map := map[string]string{
+ "Sent": "[Gmail]/Sent",
+ "Spam": "[Gmail]/Spam",
+ }
+ want_order := []string{"Sent", "Spam"}
+
+ if !reflect.DeepEqual(order, want_order) {
+ t.Errorf("order is not correct; want: %v, got: %v",
+ want_order, order)
+ }
+
+ if !reflect.DeepEqual(fmap, want_map) {
+ t.Errorf("map is not correct; want: %v, got: %v",
+ want_map, fmap)
+ }
+}
+
+func TestFolderMap_ExpectFails(t *testing.T) {
+ tests := []string{
+ `key = `,
+ ` = value`,
+ ` = `,
+ `key = #value`,
+ }
+ for _, text := range tests {
+ _, _, err := lib.ParseFolderMap(strings.NewReader(text))
+ if err == nil {
+ t.Errorf("expected to fail, but it did not: %v", text)
+ }
+ }
+}