summaryrefslogtreecommitdiffstats
path: root/lua/inbox/indexers.lua
blob: f358ce0f27b363a2a1e4a0dc07bfe3fa4b97fd37 (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
---@class inbox.Indexer
---@field setup? fun(opts: table)
---@field available? fun(): boolean
---@field index? fun(cb: fun(entries: inbox.Summary[]), opts: table)

---@alias inbox.Indexer.Config
---| "notmuch"
---| string
---| table

local M = {}

---@return inbox.Indexer
function M.get_indexer()
	if not M.indexer then
		local config = require("inbox.config")
		M.setup(config.indexer_config)
	end

	return M.indexer
end

---@param opts inbox.Indexer.Config
function M.setup(opts)
	if not opts then
		vim.notify("No indexer set", vim.log.levels.ERROR)
		return nil
	end

	if type(opts) == "string" then
		opts = { name = opts }
	end

	local ok, indexer = pcall(require, string.format("inbox.indexers.%s", opts.name))
	if not ok then
		vim.notify(string.format("Indexer not found: '%s'", indexer), vim.log.levels.ERROR)
	end

	-- TODO: Validate indexer spec
	if indexer.available() then
		M.indexer = indexer
		M.indexer.setup(opts)
	else
		vim.notify(string.format("Indexer not available: '%s'", indexer), vim.log.levels.ERROR)
	end
end

return M