Skip to content

Commit 8f9edfe

Browse files
author
BirdeeHub
committed
docs(readme): readme
1 parent 934b49f commit 8f9edfe

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

README.md

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ gcc -xc -E -v - <<< '#include <lua.h>' 2>&1 | grep lua.h | head -n 1 | awk '{pri
6565

6666
### Useage
6767

68+
#### Options
69+
6870
```lua
6971
package.cpath = package.cpath .. ";/path/to/tomlua/lib/?.so"
7072

@@ -89,7 +91,9 @@ local tomlua2 = tomlua {
8991
-- such that it keeps track of what was inline or a heading in the file for encode
9092
mark_inline = false,
9193
-- causes multiline strings to be parsed into a userdata type
92-
-- which can be converted to a lua string with tostring
94+
-- which records that it was a multiline string
95+
-- so that it may be emitted as a multiline string as well.
96+
-- It may be converted to a lua string with tostring
9397
multi_strings = false,
9498
-- if tomlua.decode reads a number bigger than can fit in a lua number or integer
9599
-- with false it will set + or - math.huge for the value and return normally
@@ -107,7 +111,11 @@ local tomlua2 = tomlua {
107111
tomlua.opts.fancy_dates = true
108112
-- note, when setting options this way, do not replace the entire table.
109113
-- this is a proxy table for the settings, which are represented in C for performance reasons.
114+
```
110115

116+
#### Decode
117+
118+
```lua
111119
local some_string = [=[
112120
xmplarray = [ "this", "is", "an", "array" ]
113121
[example]
@@ -129,9 +137,11 @@ local defaults = {
129137
}
130138

131139
local data, err = tomlua.decode(some_string, defaults)
140+
```
132141

133-
-- and encode, explained further below.
142+
#### Encode
134143

144+
```lua
135145
local str, err = tomlua.encode(data)
136146
```
137147

@@ -150,6 +160,10 @@ You may create date objects, and you may decide to make some strings multiline w
150160
You may make arrays appear as tables, or empty tables appear as arrays.
151161

152162
```lua
163+
164+
local toml_multiline_str = tomlua.str_2_mul("hello\nworld")
165+
local regular_str = tostring(toml_multiline_str)
166+
153167
local str, err = tomlua.encode({
154168
empty_toml_array = setmetatable({}, {
155169
toml_type = "ARRAY"
@@ -170,32 +184,9 @@ There are some circumstances where this doesn't make sense, however,
170184
such as setting the tables within an array only and not the array itself.
171185
It will only be able to affect the child elements in that case.
172186

173-
```lua
174-
tomlua.types = {
175-
UNTYPED, -- 0 -- Untyped TOML Item
176-
STRING, -- 1 -- lua string
177-
STRING_MULTI, -- 2 -- lua string
178-
INTEGER, -- 3 -- lua number
179-
FLOAT, -- 4 -- lua number
180-
BOOL, -- 5 -- lua bool
181-
ARRAY, -- 6 -- lua table
182-
TABLE, -- 7 -- lua table
183-
ARRAY_INLINE, -- 8 -- same as ARRAY, but forces encode to print it as a inline
184-
TABLE_INLINE, -- 9 -- same as TABLE, but forces encode to print it as a inline
185-
LOCAL_DATE, -- 10 -- string, or userdata with fancy_dates
186-
LOCAL_TIME, -- 11 -- string, or userdata with fancy_dates
187-
LOCAL_DATETIME, -- 12 -- string, or userdata with fancy_dates
188-
OFFSET_DATETIME, -- 13 -- string, or userdata with fancy_dates
189-
}
190-
-- get the type tomlua thinks a lua value is
191-
tomlua.type(value) --> returns one of the names from tomlua.types
192-
-- get the type tomlua thinks a lua value is
193-
tomlua.type_of(value) --> returns corresponding number from tomlua.types instead
194-
tomlua.typename(typenumber) --> tomlua.types[result] = typenumber
195-
196-
local toml_multiline_str = tomlua.str_2_mul("hello\nworld")
197-
local regular_str = tostring(toml_multiline_str)
187+
#### Dates
198188

189+
```lua
199190
-- accepts utc_timestamp,
200191
-- toml date string,
201192
-- a table or array with the same fields as date,
@@ -225,6 +216,32 @@ print(date > date2) -- true
225216
print(date) -- print as toml date string
226217
```
227218

219+
#### Type checking
220+
221+
```lua
222+
tomlua.types = {
223+
UNTYPED, -- 0 -- Untyped TOML Item
224+
STRING, -- 1 -- lua string
225+
STRING_MULTI, -- 2 -- lua string
226+
INTEGER, -- 3 -- lua number
227+
FLOAT, -- 4 -- lua number
228+
BOOL, -- 5 -- lua bool
229+
ARRAY, -- 6 -- lua table
230+
TABLE, -- 7 -- lua table
231+
ARRAY_INLINE, -- 8 -- same as ARRAY, but forces encode to print it as a inline
232+
TABLE_INLINE, -- 9 -- same as TABLE, but forces encode to print it as a inline
233+
LOCAL_DATE, -- 10 -- string, or userdata with fancy_dates
234+
LOCAL_TIME, -- 11 -- string, or userdata with fancy_dates
235+
LOCAL_DATETIME, -- 12 -- string, or userdata with fancy_dates
236+
OFFSET_DATETIME, -- 13 -- string, or userdata with fancy_dates
237+
}
238+
-- get the type tomlua thinks a lua value is
239+
tomlua.type(value) --> returns one of the names from tomlua.types
240+
-- get the type tomlua thinks a lua value is
241+
tomlua.type_of(value) --> returns corresponding number from tomlua.types instead
242+
tomlua.typename(typenumber) --> tomlua.types[result] = typenumber
243+
```
244+
228245
## Philosophy
229246

230247
* TOML is usually a **config format**, not a serialization format.
@@ -233,6 +250,8 @@ print(date) -- print as toml date string
233250

234251
On startup you may have many toml files to parse in some situations, if you used it in a package spec format of some kind for example.
235252

253+
Or maybe you need to parse toml files in a mass CI script.
254+
236255
This is a tiny c library designed to chew through those as if you were using cjson for parsing json.
237256

238257
It is able to emit toml as well, and still does so quickly, but this was mostly for completeness.
@@ -246,4 +265,4 @@ As a result of that, editing existing toml files, or emitting them at all, only
246265

247266
This means that it makes a lot of sense to use a simple but fast parser to parse config files on startup.
248267

249-
And then later you can use one which is better for editing but is slower when making things like a settings page which may edit the file.
268+
And then later you can use one which is better for editing but is slower when making things like a settings page or cargo add which may edit the file.

0 commit comments

Comments
 (0)