{"route":"/en-US-v0.14.1/reference/data-loading/toml/","title":"TOML","description":"Documentation for the `toml` function.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"example","name":"Example","children":[]},{"id":"conversion","name":"Conversion","children":[{"id":"notes","name":"Notes","children":[]}]},{"id":"parameters","name":"Parameters","children":[{"id":"parameters-source","name":"source","children":[]}]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-decode","name":"Decode TOML","children":[{"id":"definitions-decode-data","name":"data","children":[]}]},{"id":"definitions-encode","name":"Encode TOML","children":[{"id":"definitions-encode-value","name":"value","children":[]},{"id":"definitions-encode-pretty","name":"pretty","children":[]}]}]}],"body":{"kind":"func","content":{"path":[],"name":"toml","title":"TOML","keywords":[],"oneliner":"Reads structured data from a TOML file.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Reads structured data from a TOML file.</p>\n<p>The file must contain a valid TOML table. The TOML values will be converted\ninto corresponding Typst values as listed in the <a href=\"#conversion\">table below</a>.</p>\n<p>The function returns a dictionary representing the TOML table.</p>\n<p>The TOML file in the example consists of a table with the keys <code>title</code>,\n<code>version</code>, and <code>authors</code>.</p>\n<h2 id=\"example\">Example</h2>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> details <span class=\"typ-op\">=</span> <span class=\"typ-func\">toml</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;details.toml&quot;</span><span class=\"typ-punct\">)</span>\n\nTitle: <span class=\"typ-pol\">#</span><span class=\"typ-pol\">details</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">title</span> <span class=\"typ-escape\">\\</span>\nVersion: <span class=\"typ-pol\">#</span><span class=\"typ-pol\">details</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">version</span> <span class=\"typ-escape\">\\</span>\nAuthors: <span class=\"typ-punct\">#</span><span class=\"typ-punct\">(</span>details<span class=\"typ-punct\">.</span>authors\n  <span class=\"typ-punct\">.</span><span class=\"typ-func\">join</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;, &quot;</span><span class=\"typ-punct\">,</span> last<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot; and &quot;</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/7f6e9fac705651fafb6c8a26435ab058.png\" alt=\"Preview\"></div></div>\n<h2 id=\"conversion\">Conversion details</h2>\n<p>First of all, TOML documents are tables. Other values must be put in a table\nto be encoded or decoded.</p>\n<table><thead><tr><th>TOML value</th><th>Converted into Typst</th></tr></thead><tbody>\n<tr><td>string</td><td><a href=\"/en-US-v0.14.1/reference/foundations/str/\" title=\"`str`\"><code>str</code></a></td></tr>\n<tr><td>integer</td><td><a href=\"/en-US-v0.14.1/reference/foundations/int/\" title=\"`int`\"><code>int</code></a></td></tr>\n<tr><td>float</td><td><a href=\"/en-US-v0.14.1/reference/foundations/float/\" title=\"`float`\"><code>float</code></a></td></tr>\n<tr><td>boolean</td><td><a href=\"/en-US-v0.14.1/reference/foundations/bool/\" title=\"`bool`\"><code>bool</code></a></td></tr>\n<tr><td>datetime</td><td><a href=\"/en-US-v0.14.1/reference/foundations/datetime/\" title=\"`datetime`\"><code>datetime</code></a></td></tr>\n<tr><td>array</td><td><a href=\"/en-US-v0.14.1/reference/foundations/array/\" title=\"`array`\"><code>array</code></a></td></tr>\n<tr><td>table</td><td><a href=\"/en-US-v0.14.1/reference/foundations/dictionary/\" title=\"`dictionary`\"><code>dictionary</code></a></td></tr>\n</tbody></table>\n<table><thead><tr><th>Typst value</th><th>Converted into TOML</th></tr></thead><tbody>\n<tr><td>types that can be converted from TOML</td><td>corresponding TOML value</td></tr>\n<tr><td><code><span class=\"typ-key\">none</span></code></td><td>ignored</td></tr>\n<tr><td><a href=\"/en-US-v0.14.1/reference/foundations/bytes/\" title=\"`bytes`\"><code>bytes</code></a></td><td>string via <a href=\"/en-US-v0.14.1/reference/foundations/repr/\" title=\"`repr`\"><code>repr</code></a></td></tr>\n<tr><td><a href=\"/en-US-v0.14.1/reference/foundations/symbol/\" title=\"`symbol`\"><code>symbol</code></a></td><td>string</td></tr>\n<tr><td><a href=\"/en-US-v0.14.1/reference/foundations/content/\" title=\"`content`\"><code>content</code></a></td><td>a table describing the content</td></tr>\n<tr><td>other types (<a href=\"/en-US-v0.14.1/reference/layout/length/\" title=\"`length`\"><code>length</code></a>, etc.)</td><td>string via <a href=\"/en-US-v0.14.1/reference/foundations/repr/\" title=\"`repr`\"><code>repr</code></a></td></tr>\n</tbody></table>\n<h3 id=\"notes\">Notes</h3>\n<ul>\n<li>\n<p>Be aware that TOML integers larger than 2<sup>63</sup>-1 or smaller\nthan -2<sup>63</sup> cannot be represented losslessly in Typst, and an\nerror will be thrown according to the\n<a href=\"https://toml.io/en/v1.0.0#integer\">specification</a>.</p>\n</li>\n<li>\n<p>Bytes are not encoded as TOML arrays for performance and readability\nreasons. Consider using <a href=\"/en-US-v0.14.1/reference/data-loading/cbor/#definitions-encode\" title=\"`cbor.encode`\"><code>cbor.encode</code></a> for binary data.</p>\n</li>\n<li>\n<p>The <code>repr</code> function is <a href=\"/en-US-v0.14.1/reference/foundations/repr/#debugging-only\">for debugging purposes only</a>,\nand its output is not guaranteed to be stable across Typst versions.</p>\n</li>\n</ul>"}],"self":false,"params":[{"name":"source","details":[{"kind":"html","content":"<p>A <a href=\"/en-US-v0.14.1/reference/syntax/#paths\">path</a> to a TOML file or raw TOML bytes.</p>"}],"types":["str","bytes"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["dictionary"],"scope":[{"path":["toml"],"name":"decode","title":"Decode TOML","keywords":[],"oneliner":"Reads structured data from a TOML string/bytes.","element":false,"contextual":false,"deprecationMessage":"`toml.decode` is deprecated, directly pass bytes to `toml` instead","deprecationUntil":"0.15.0","details":[{"kind":"html","content":"<p>Reads structured data from a TOML string/bytes.</p>"}],"self":false,"params":[{"name":"data","details":[{"kind":"html","content":"<p>TOML data.</p>"}],"types":["str","bytes"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["dictionary"],"scope":[]},{"path":["toml"],"name":"encode","title":"Encode TOML","keywords":[],"oneliner":"Encodes structured data into a TOML string.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Encodes structured data into a TOML string.</p>"}],"self":false,"params":[{"name":"value","details":[{"kind":"html","content":"<p>Value to be encoded.</p>\n<p>TOML documents are tables. Therefore, only dictionaries are suitable.</p>"}],"types":["dictionary"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"pretty","details":[{"kind":"html","content":"<p>Whether to pretty-print the resulting TOML.</p>"}],"types":["bool"],"strings":[],"default":"<code><span class=\"typ-key\">true</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":false}],"returns":["str"],"scope":[]}]}}}