From 1742b74d5a6b47db8632b2f8608918277f6c02e8 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 30 Oct 2020 19:08:52 +0100 Subject: [PATCH] Refactor restricted option sets Added utility function must_contain which will crawl through a set and emit an error if the value is not contained in it. --- lua/zettelkasten/options.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lua/zettelkasten/options.lua b/lua/zettelkasten/options.lua index f47d9a8..bf80889 100644 --- a/lua/zettelkasten/options.lua +++ b/lua/zettelkasten/options.lua @@ -7,17 +7,30 @@ local ANCHOR_SEPARATOR = "_" -- TODO zettel_root = vim.g["zettel_root"] or vim.b["zettel_root"] or "~/documents/notes", -- TODO zettel_anchor_pattern = regex? -> needs custom creation function in `create_anchor` +-- constricted option sets +local ZETTEL_LINK_STYLE_OPTIONS = {markdown = true, wiki = true} + +local function must_contain(set, value, name) + if type(set) ~= "table" then return false end + if not set[value] then + local allvalues = "" + for n, _ in pairs(set) do allvalues = n .. ", " .. allvalues end + error((name or "value") .. " " .. value .. " must be one of " .. + allvalues:sub(1, -3)) + end +end + function Opt.zettel() local options = {} options.extension = vim.g["zettel_extension"] or vim.b["zettel_extension"] or ZETTEL_EXTENSION + options.link_style = vim.g["zettel_link_style"] or vim.b["zettel_link_style"] or ZETTEL_LINK_STYLE + must_contain(ZETTEL_LINK_STYLE_OPTIONS, options.link_style, + "zettel_link_style") - if options.link_style ~= "wiki" and options.link_style ~= "markdown" then - error("zettel_link_style can only be set to markdown or wiki.") - end return options end