aboutsummaryrefslogtreecommitdiff
path: root/internal/imap/folder.go
blob: 32bfefe28e323cbf8789a7133581b8c5219aa48d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package imap

import "strings"

type Folder struct {
	str       string
	delimiter string
}

func (f Folder) IsBlank() bool {
	return f.str == ""
}

func (f Folder) String() string {
	return f.str
}

func (f Folder) Append(other Folder) Folder {
	if f.delimiter != other.delimiter {
		panic("Delimiters do not match")
	}

	if other.str == "" {
		return f
	}

	var prefix string
	if f.str == "" {
		prefix = ""
	} else {
		prefix = f.str + f.delimiter
	}

	return Folder{
		str:       prefix + other.str,
		delimiter: f.delimiter,
	}
}

func buildFolderName(path []string, delimiter string) (name string) {
	name = strings.Join(path, delimiter)
	if delimiter != "" {
		name = strings.Trim(name, delimiter[0:1])
	}
	return
}

func (cl *Client) folderName(path []string) Folder {
	return Folder{
		buildFolderName(path, cl.delimiter),
		cl.delimiter,
	}
}

func (cl *Client) NewFolder(path []string) Folder {
	return cl.toplevel.Append(cl.folderName(path))
}