Skip to content

Commit fa9fb28

Browse files
author
BirdeeHub
committed
docs(luaCats): type annotations
1 parent 40d60fc commit fa9fb28

3 files changed

Lines changed: 77 additions & 2 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ test: $(SRC)/src/* $(TESTDIR)/*
4242
$(check_so_was_built)
4343
$(LUA) "$(TESTDIR)/run_tests.lua" -- "$(TESTDIR)" "$(DESTDIR)"
4444

45-
install:
45+
install: $(SRC)/meta.lua
4646
ifdef LIBDIR
4747
$(check_so_was_built)
4848
@mkdir -p "$(LIBDIR)";
4949
cp "$(DESTDIR)/tomlua.so" "$(LIBDIR)/";
5050
@echo "Installed to $(LIBDIR)";
51+
ifdef LUADIR
52+
@mkdir -p "$(LUADIR)/tomlua";
53+
cp "$(SRC)/meta.lua" "$(LUADIR)/tomlua/";
54+
endif
5155
else
5256
@echo "LIBDIR not set, skipping install"
5357
endif

meta.lua

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---@meta
2+
error("Cannot import a meta module")
3+
4+
---@class Tomlua.DateTable
5+
---@field toml_type TomlType|TomlTypeNum -- one of tomlua.types, e.g., OFFSET_DATETIME
6+
---@field year integer -- 1979, 3333, etc.
7+
---@field month integer -- 112
8+
---@field day integer -- 131
9+
---@field hour integer -- 023
10+
---@field minute integer -- 059
11+
---@field second integer -- 059, leap second 60 optional
12+
---@field fractional integer -- 0999999 (microsecond precision optional)
13+
---@field offset_hour integer -- UTC offset hours, e.g., -7
14+
---@field offset_minute integer -- UTC offset minutes, usually 0, can be 30/45
15+
16+
---@alias Tomlua.Date Tomlua.DateTable | userdata
17+
18+
---@class TomluaOptions
19+
---@field fancy_tables? boolean
20+
---@field int_keys? boolean
21+
---@field fancy_dates? boolean
22+
---@field multi_strings? boolean
23+
---@field mark_inline? boolean
24+
---@field overflow_errors? boolean
25+
---@field underflow_errors? boolean
26+
27+
---@alias TomlType
28+
---| "UNTYPED"
29+
---| "STRING"
30+
---| "STRING_MULTI"
31+
---| "INTEGER"
32+
---| "FLOAT"
33+
---| "BOOL"
34+
---| "ARRAY"
35+
---| "TABLE"
36+
---| "ARRAY_INLINE"
37+
---| "TABLE_INLINE"
38+
---| "LOCAL_DATE"
39+
---| "LOCAL_TIME"
40+
---| "LOCAL_DATETIME"
41+
---| "OFFSET_DATETIME"
42+
43+
---@alias TomlTypeNum
44+
---| 0
45+
---| 1
46+
---| 2
47+
---| 3
48+
---| 4
49+
---| 5
50+
---| 6
51+
---| 7
52+
---| 8
53+
---| 9
54+
---| 10
55+
---| 11
56+
---| 12
57+
---| 13
58+
59+
---@class Tomlua.main
60+
---@field opts TomluaOptions
61+
---@field types table<TomlType, TomlTypeNum>
62+
---@field decode fun(str:string):(any, string?): table?, string? -- returns result?, err?
63+
---@field encode fun(val:any):(string, string?): string?, string? -- returns result?, err?
64+
---@field type fun(val:any):TomlType
65+
---@field type_of fun(val:any):TomlTypeNum
66+
---@field typename fun(typ:TomlTypeNum):TomlType
67+
---@field str_2_mul fun(s:string):userdata -- can call tostring on the result to get it back, written as multiline string by encode.
68+
---@field new_date fun(src:string|number|Tomlua.DateTable|Tomlua.Date?):Tomlua.Date
69+
70+
---@alias Tomlua Tomlua.main | fun(opts?:TomluaOptions):Tomlua

tests/scratch.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package.cpath = "./lib/?.so;" .. package.cpath
22
local inspect = require('inspect')
3+
---@type Tomlua
34
local tomlua = require("tomlua")
45

56
print(inspect(tomlua))
@@ -100,7 +101,7 @@ print(date < date2) -- false
100101
print("date1", date)
101102

102103
print(date.toml_type)
103-
print(tomlua.typename(tomlua.type(date)))
104+
print(tomlua.typename(tomlua.type_of(date)))
104105
date.toml_type = "LOCAL_TIME"
105106
print(date.toml_type)
106107
print(date)

0 commit comments

Comments
 (0)