pnmixer-rust/toml/index.html

274 lines
15 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="rustdoc">
<meta name="description" content="API documentation for the Rust `toml` crate.">
<meta name="keywords" content="rust, rustlang, rust-lang, toml">
<title>toml - Rust</title>
<link rel="stylesheet" type="text/css" href="../normalize.css">
<link rel="stylesheet" type="text/css" href="../rustdoc.css">
<link rel="stylesheet" type="text/css" href="../main.css">
</head>
<body class="rustdoc mod">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<nav class="sidebar">
<p class='location'>Crate toml</p><div class="block items"><ul><li><a href="#reexports">Reexports</a></li><li><a href="#modules">Modules</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'toml', ty: 'mod', relpath: '../'};</script>
</nav>
<nav class="sub">
<form class="search-form js-only">
<div class="search-container">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Crate <a class="mod" href=''>toml</a></span><span class='out-of-band'><span id='render-detail'>
<a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
[<span class='inner'>&#x2212;</span>]
</a>
</span><a class='srclink' href='../src/toml/lib.rs.html#1-168' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>A <a href="https://github.com/toml-lang/toml">TOML</a>-parsing library</p>
<p>This library implements a <a href="https://github.com/toml-lang/toml">TOML</a> v0.4.0 compatible parser,
primarily supporting the <a href="https://serde.rs/"><code>serde</code></a> library for encoding/decoding
various types in Rust.</p>
<p>TOML itself is a simple, ergonomic, and readable configuration format:</p>
<pre><code class="language-toml">[package]
name = &quot;toml&quot;
version = &quot;0.2.1&quot;
authors = [&quot;Alex Crichton &lt;alex@alexcrichton.com&gt;&quot;]
[dependencies]
serde = &quot;1.0&quot;
</code></pre>
<p>The TOML format tends to be relatively common throughout the Rust community
for configuration, notably being used by <a href="https://crates.io/">Cargo</a>, Rust&#39;s package manager.</p>
<h2 id='toml-values' class='section-header'><a href='#toml-values'>TOML values</a></h2>
<p>A value in TOML is represented with the <code>Value</code> enum in this crate:</p>
<pre class="rust rust-example-rendered">
<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">Value</span> {
<span class="ident">String</span>(<span class="ident">String</span>),
<span class="ident">Integer</span>(<span class="ident">i64</span>),
<span class="ident">Float</span>(<span class="ident">f64</span>),
<span class="ident">Boolean</span>(<span class="ident">bool</span>),
<span class="ident">Datetime</span>(<span class="ident">Datetime</span>),
<span class="ident">Array</span>(<span class="ident">Array</span>),
<span class="ident">Table</span>(<span class="ident">Table</span>),
}</pre>
<p>TOML is similar to JSON with the notable addition of a <code>Datetime</code>
type. In general, TOML and JSON are interchangeable in terms of
formats.</p>
<h2 id='parsing-toml' class='section-header'><a href='#parsing-toml'>Parsing TOML</a></h2>
<p>The easiest way to parse a TOML document is via the <code>Value</code> type:</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">toml</span>::<span class="ident">Value</span>;
<span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="string">&quot;foo = &#39;bar&#39;&quot;</span>.<span class="ident">parse</span>::<span class="op">&lt;</span><span class="ident">Value</span><span class="op">&gt;</span>().<span class="ident">unwrap</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">value</span>[<span class="string">&quot;foo&quot;</span>].<span class="ident">as_str</span>(), <span class="prelude-val">Some</span>(<span class="string">&quot;bar&quot;</span>));</pre>
<p>The <code>Value</code> type implements a number of convenience methods and
traits; the example above uses <code>FromStr</code> to parse a <code>str</code> into a
<code>Value</code>.</p>
<h2 id='deserialization-and-serialization' class='section-header'><a href='#deserialization-and-serialization'>Deserialization and Serialization</a></h2>
<p>This crate supports <a href="https://serde.rs/"><code>serde</code></a> 1.0 with a number of
implementations of the <code>Deserialize</code>, <code>Serialize</code>, <code>Deserializer</code>, and
<code>Serializer</code> traits. Namely, you&#39;ll find:</p>
<ul>
<li><code>Deserialize for Value</code></li>
<li><code>Serialize for Value</code></li>
<li><code>Deserialize for Datetime</code></li>
<li><code>Serialize for Datetime</code></li>
<li><code>Deserializer for de::Deserializer</code></li>
<li><code>Serializer for ser::Serializer</code></li>
<li><code>Deserializer for Value</code></li>
</ul>
<p>This means that you can use Serde to deserialize/serialize the
<code>Value</code> type as well as the <code>Datetime</code> type in this crate. You can also
use the <code>Deserializer</code>, <code>Serializer</code>, or <code>Value</code> type itself to act as
a deserializer/serializer for arbitrary types.</p>
<p>An example of deserializing with TOML is:</p>
<pre class="rust rust-example-rendered">
<span class="attribute">#[<span class="ident">macro_use</span>]</span>
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">serde_derive</span>;
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">toml</span>;
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Deserialize</span>)]</span>
<span class="kw">struct</span> <span class="ident">Config</span> {
<span class="ident">ip</span>: <span class="ident">String</span>,
<span class="ident">port</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">u16</span><span class="op">&gt;</span>,
<span class="ident">keys</span>: <span class="ident">Keys</span>,
}
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Deserialize</span>)]</span>
<span class="kw">struct</span> <span class="ident">Keys</span> {
<span class="ident">github</span>: <span class="ident">String</span>,
<span class="ident">travis</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">String</span><span class="op">&gt;</span>,
}
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">config</span>: <span class="ident">Config</span> <span class="op">=</span> <span class="ident">toml</span>::<span class="ident">from_str</span>(<span class="string">r#&quot;
ip = &#39;127.0.0.1&#39;
[keys]
github = &#39;xxxxxxxxxxxxxxxxx&#39;
travis = &#39;yyyyyyyyyyyyyyyyy&#39;
&quot;#</span>).<span class="ident">unwrap</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">config</span>.<span class="ident">ip</span>, <span class="string">&quot;127.0.0.1&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">config</span>.<span class="ident">port</span>, <span class="prelude-val">None</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">config</span>.<span class="ident">keys</span>.<span class="ident">github</span>, <span class="string">&quot;xxxxxxxxxxxxxxxxx&quot;</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">config</span>.<span class="ident">keys</span>.<span class="ident">travis</span>.<span class="ident">as_ref</span>().<span class="ident">unwrap</span>(), <span class="string">&quot;yyyyyyyyyyyyyyyyy&quot;</span>);
}</pre>
<p>You can serialize types in a similar fashion:</p>
<pre class="rust rust-example-rendered">
<span class="attribute">#[<span class="ident">macro_use</span>]</span>
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">serde_derive</span>;
<span class="kw">extern</span> <span class="kw">crate</span> <span class="ident">toml</span>;
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Serialize</span>)]</span>
<span class="kw">struct</span> <span class="ident">Config</span> {
<span class="ident">ip</span>: <span class="ident">String</span>,
<span class="ident">port</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">u16</span><span class="op">&gt;</span>,
<span class="ident">keys</span>: <span class="ident">Keys</span>,
}
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Serialize</span>)]</span>
<span class="kw">struct</span> <span class="ident">Keys</span> {
<span class="ident">github</span>: <span class="ident">String</span>,
<span class="ident">travis</span>: <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">String</span><span class="op">&gt;</span>,
}
<span class="kw">fn</span> <span class="ident">main</span>() {
<span class="kw">let</span> <span class="ident">config</span> <span class="op">=</span> <span class="ident">Config</span> {
<span class="ident">ip</span>: <span class="string">&quot;127.0.0.1&quot;</span>.<span class="ident">to_string</span>(),
<span class="ident">port</span>: <span class="prelude-val">None</span>,
<span class="ident">keys</span>: <span class="ident">Keys</span> {
<span class="ident">github</span>: <span class="string">&quot;xxxxxxxxxxxxxxxxx&quot;</span>.<span class="ident">to_string</span>(),
<span class="ident">travis</span>: <span class="prelude-val">Some</span>(<span class="string">&quot;yyyyyyyyyyyyyyyyy&quot;</span>.<span class="ident">to_string</span>()),
},
};
<span class="kw">let</span> <span class="ident">toml</span> <span class="op">=</span> <span class="ident">toml</span>::<span class="ident">to_string</span>(<span class="kw-2">&amp;</span><span class="ident">config</span>).<span class="ident">unwrap</span>();
}</pre>
</div><h2 id='reexports' class='section-header'><a href="#reexports">Reexports</a></h2>
<table><tr><td><code>pub use value::<a class="enum" href="../toml/value/enum.Value.html" title="enum toml::value::Value">Value</a>;</code></td></tr><tr><td><code>pub use ser::<a class="fn" href="../toml/ser/fn.to_string.html" title="fn toml::ser::to_string">to_string</a>;</code></td></tr><tr><td><code>pub use ser::<a class="fn" href="../toml/ser/fn.to_vec.html" title="fn toml::ser::to_vec">to_vec</a>;</code></td></tr><tr><td><code>pub use ser::<a class="struct" href="../toml/ser/struct.Serializer.html" title="struct toml::ser::Serializer">Serializer</a>;</code></td></tr><tr><td><code>pub use de::<a class="fn" href="../toml/de/fn.from_slice.html" title="fn toml::de::from_slice">from_slice</a>;</code></td></tr><tr><td><code>pub use de::<a class="fn" href="../toml/de/fn.from_str.html" title="fn toml::de::from_str">from_str</a>;</code></td></tr><tr><td><code>pub use de::<a class="struct" href="../toml/de/struct.Deserializer.html" title="struct toml::de::Deserializer">Deserializer</a>;</code></td></tr></table><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
<table>
<tr class=' module-item'>
<td><a class="mod" href="de/index.html"
title='mod toml::de'>de</a></td>
<td class='docblock-short'>
<p>Deserializing TOML into Rust structures.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="mod" href="ser/index.html"
title='mod toml::ser'>ser</a></td>
<td class='docblock-short'>
<p>Serializing Rust structures into TOML.</p>
</td>
</tr>
<tr class=' module-item'>
<td><a class="mod" href="value/index.html"
title='mod toml::value'>value</a></td>
<td class='docblock-short'>
<p>Definition of a TOML value</p>
</td>
</tr></table></section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
<aside id="help" class="hidden">
<div>
<h1 class="hidden">Help</h1>
<div class="shortcuts">
<h2>Keyboard Shortcuts</h2>
<dl>
<dt>?</dt>
<dd>Show this help dialog</dd>
<dt>S</dt>
<dd>Focus the search field</dd>
<dt>&larrb;</dt>
<dd>Move up in search results</dd>
<dt>&rarrb;</dt>
<dd>Move down in search results</dd>
<dt>&#9166;</dt>
<dd>Go to active search result</dd>
<dt>+</dt>
<dd>Collapse/expand all sections</dd>
</dl>
</div>
<div class="infos">
<h2>Search Tricks</h2>
<p>
Prefix searches with a type followed by a colon (e.g.
<code>fn:</code>) to restrict the search to a given type.
</p>
<p>
Accepted types are: <code>fn</code>, <code>mod</code>,
<code>struct</code>, <code>enum</code>,
<code>trait</code>, <code>type</code>, <code>macro</code>,
and <code>const</code>.
</p>
<p>
Search functions by type signature (e.g.
<code>vec -> usize</code> or <code>* -> vec</code>)
</p>
</div>
</div>
</aside>
<script>
window.rootPath = "../";
window.currentCrate = "toml";
</script>
<script src="../main.js"></script>
<script defer src="../search-index.js"></script>
</body>
</html>