* refactor: tighten LuaCATS annotations across modules Problem: type annotations repeated inline unions with no aliases, used `table<string, any>` where structured types exist, and had loose `string` where union types should be used. Solution: add `pending.TaskStatus`, `pending.RecurMode`, `pending.TaskExtra`, `pending.ForgeType`, `pending.ForgeState`, `pending.ForgeAuthStatus` aliases and `pending.SyncBackend` interface. Replace inline unions and loose types with the new aliases in `store.lua`, `forge.lua`, `config.lua`, `diff.lua`, `views.lua`, `parse.lua`, `init.lua`, and `oauth.lua`. * refactor: canonicalize internal metadata field names Problem: `pending.Metadata` used shorthand field names (`cat`, `rec`, `rec_mode`) matching user-facing token syntax, coupling internal representation to config. `RecurSpec.from_completion` used a boolean where a `pending.RecurMode` alias exists. `category_syntax` was hardcoded to `'cat'` with no config option. Solution: rename `Metadata` fields to `category`/`recur`/`recur_mode`, add `category_syntax` config option (default `'cat'`), rename `ParsedEntry` fields to match, replace `RecurSpec.from_completion` with `mode: pending.RecurMode`, and restore `[string]` indexer on `pending.ForgeConfig` alongside explicit fields.
188 lines
5 KiB
Lua
188 lines
5 KiB
Lua
---@class pending.RecurSpec
|
|
---@field freq 'daily'|'weekly'|'monthly'|'yearly'
|
|
---@field interval integer
|
|
---@field byday? string[]
|
|
---@field mode pending.RecurMode
|
|
---@field _raw? string
|
|
|
|
---@class pending.recur
|
|
local M = {}
|
|
|
|
---@type table<string, pending.RecurSpec>
|
|
local named = {
|
|
daily = { freq = 'daily', interval = 1, mode = 'scheduled' },
|
|
weekdays = {
|
|
freq = 'weekly',
|
|
interval = 1,
|
|
byday = { 'MO', 'TU', 'WE', 'TH', 'FR' },
|
|
mode = 'scheduled',
|
|
},
|
|
weekly = { freq = 'weekly', interval = 1, mode = 'scheduled' },
|
|
biweekly = { freq = 'weekly', interval = 2, mode = 'scheduled' },
|
|
monthly = { freq = 'monthly', interval = 1, mode = 'scheduled' },
|
|
quarterly = { freq = 'monthly', interval = 3, mode = 'scheduled' },
|
|
yearly = { freq = 'yearly', interval = 1, mode = 'scheduled' },
|
|
annual = { freq = 'yearly', interval = 1, mode = 'scheduled' },
|
|
}
|
|
|
|
---@param spec string
|
|
---@return pending.RecurSpec?
|
|
function M.parse(spec)
|
|
local mode = 'scheduled' ---@type pending.RecurMode
|
|
local s = spec
|
|
|
|
if s:sub(1, 1) == '!' then
|
|
mode = 'completion'
|
|
s = s:sub(2)
|
|
end
|
|
|
|
local lower = s:lower()
|
|
|
|
local base = named[lower]
|
|
if base then
|
|
return {
|
|
freq = base.freq,
|
|
interval = base.interval,
|
|
byday = base.byday,
|
|
mode = mode,
|
|
}
|
|
end
|
|
|
|
local n, unit = lower:match('^(%d+)([dwmy])$')
|
|
if n then
|
|
local num = tonumber(n) --[[@as integer]]
|
|
if num < 1 then
|
|
return nil
|
|
end
|
|
local freq_map = { d = 'daily', w = 'weekly', m = 'monthly', y = 'yearly' }
|
|
return {
|
|
freq = freq_map[unit],
|
|
interval = num,
|
|
mode = mode,
|
|
}
|
|
end
|
|
|
|
if s:match('^FREQ=') then
|
|
return {
|
|
freq = 'daily',
|
|
interval = 1,
|
|
mode = mode,
|
|
_raw = s,
|
|
}
|
|
end
|
|
|
|
return nil
|
|
end
|
|
|
|
---@param spec string
|
|
---@return boolean
|
|
function M.validate(spec)
|
|
return M.parse(spec) ~= nil
|
|
end
|
|
|
|
---@param due string
|
|
---@return string date_part
|
|
---@return string? time_part
|
|
local function split_datetime(due)
|
|
local dp, tp = due:match('^(.+)T(.+)$')
|
|
if dp then
|
|
return dp, tp
|
|
end
|
|
return due, nil
|
|
end
|
|
|
|
---@param base_date string
|
|
---@param freq string
|
|
---@param interval integer
|
|
---@return string
|
|
local function advance_date(base_date, freq, interval)
|
|
local date_part, time_part = split_datetime(base_date)
|
|
local y, m, d = date_part:match('^(%d+)-(%d+)-(%d+)$')
|
|
local yn = tonumber(y) --[[@as integer]]
|
|
local mn = tonumber(m) --[[@as integer]]
|
|
local dn = tonumber(d) --[[@as integer]]
|
|
|
|
local result
|
|
if freq == 'daily' then
|
|
result = os.date('%Y-%m-%d', os.time({ year = yn, month = mn, day = dn + interval })) --[[@as string]]
|
|
elseif freq == 'weekly' then
|
|
result = os.date('%Y-%m-%d', os.time({ year = yn, month = mn, day = dn + interval * 7 })) --[[@as string]]
|
|
elseif freq == 'monthly' then
|
|
local new_m = mn + interval
|
|
local new_y = yn
|
|
while new_m > 12 do
|
|
new_m = new_m - 12
|
|
new_y = new_y + 1
|
|
end
|
|
local last_day = os.date('*t', os.time({ year = new_y, month = new_m + 1, day = 0 })) --[[@as osdate]]
|
|
local clamped_d = math.min(dn, last_day.day --[[@as integer]])
|
|
result = os.date('%Y-%m-%d', os.time({ year = new_y, month = new_m, day = clamped_d })) --[[@as string]]
|
|
elseif freq == 'yearly' then
|
|
local new_y = yn + interval
|
|
local last_day = os.date('*t', os.time({ year = new_y, month = mn + 1, day = 0 })) --[[@as osdate]]
|
|
local clamped_d = math.min(dn, last_day.day --[[@as integer]])
|
|
result = os.date('%Y-%m-%d', os.time({ year = new_y, month = mn, day = clamped_d })) --[[@as string]]
|
|
else
|
|
return base_date
|
|
end
|
|
|
|
if time_part then
|
|
return result .. 'T' .. time_part
|
|
end
|
|
return result
|
|
end
|
|
|
|
---@param base_date string
|
|
---@param spec string
|
|
---@param mode pending.RecurMode
|
|
---@return string
|
|
function M.next_due(base_date, spec, mode)
|
|
local parsed = M.parse(spec)
|
|
if not parsed then
|
|
return base_date
|
|
end
|
|
|
|
local today = os.date('%Y-%m-%d') --[[@as string]]
|
|
local _, time_part = split_datetime(base_date)
|
|
|
|
if mode == 'completion' then
|
|
local base = time_part and (today .. 'T' .. time_part) or today
|
|
return advance_date(base, parsed.freq, parsed.interval)
|
|
end
|
|
|
|
local next_date = advance_date(base_date, parsed.freq, parsed.interval)
|
|
local compare_today = time_part and (today .. 'T' .. time_part) or today
|
|
while next_date <= compare_today do
|
|
next_date = advance_date(next_date, parsed.freq, parsed.interval)
|
|
end
|
|
return next_date
|
|
end
|
|
|
|
---@param spec string
|
|
---@return string
|
|
function M.to_rrule(spec)
|
|
local parsed = M.parse(spec)
|
|
if not parsed then
|
|
return ''
|
|
end
|
|
|
|
if parsed._raw then
|
|
return 'RRULE:' .. parsed._raw
|
|
end
|
|
|
|
local parts = { 'FREQ=' .. parsed.freq:upper() }
|
|
if parsed.interval > 1 then
|
|
table.insert(parts, 'INTERVAL=' .. parsed.interval)
|
|
end
|
|
if parsed.byday then
|
|
table.insert(parts, 'BYDAY=' .. table.concat(parsed.byday, ','))
|
|
end
|
|
return 'RRULE:' .. table.concat(parts, ';')
|
|
end
|
|
|
|
---@return string[]
|
|
function M.shorthand_list()
|
|
return { 'daily', 'weekdays', 'weekly', 'biweekly', 'monthly', 'quarterly', 'yearly', 'annual' }
|
|
end
|
|
|
|
return M
|