mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-27 13:51:03 +00:00
197 lines
7.7 KiB
HTML
197 lines
7.7 KiB
HTML
<!DOCTYPE HTML>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>TOML - </title>
|
|
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
|
<meta name="description" content="">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
<base href="../">
|
|
|
|
<link rel="stylesheet" href="book.css">
|
|
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
|
|
|
|
<link rel="shortcut icon" href="favicon.png">
|
|
|
|
<!-- Font Awesome -->
|
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
|
|
|
|
<link rel="stylesheet" href="highlight.css">
|
|
<link rel="stylesheet" href="tomorrow-night.css">
|
|
|
|
<!-- MathJax -->
|
|
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
|
|
|
<!-- Fetch JQuery from CDN but have a local fallback -->
|
|
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
|
|
<script>
|
|
if (typeof jQuery == 'undefined') {
|
|
document.write(unescape("%3Cscript src='jquery.js'%3E%3C/script%3E"));
|
|
}
|
|
</script>
|
|
</head>
|
|
<body class="light">
|
|
<!-- Set the theme before any content is loaded, prevents flash -->
|
|
<script type="text/javascript">
|
|
var theme = localStorage.getItem('theme');
|
|
if (theme == null) { theme = 'light'; }
|
|
$('body').removeClass().addClass(theme);
|
|
</script>
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
<script type="text/javascript">
|
|
var sidebar = localStorage.getItem('sidebar');
|
|
if (sidebar === "hidden") { $("html").addClass("sidebar-hidden") }
|
|
else if (sidebar === "visible") { $("html").addClass("sidebar-visible") }
|
|
</script>
|
|
|
|
<div id="sidebar" class="sidebar">
|
|
<ul class="chapter"><li class="affix"><a href="pages/table_of_contents.html">Table of Contents</a></li><li class="affix"><a href="pages/error_handling_note.html">A Note About Error Handling</a></li><li><a href="pages/math.html"><strong>1.</strong> Math</a></li><li><ul class="section"><li><a href="pages/rand.html"><strong>1.1.</strong> rand</a></li></ul></li><li><a href="pages/IO.html"><strong>2.</strong> IO</a></li><li><ul class="section"><li><a href="pages/byteorder.html"><strong>2.1.</strong> Byteorder</a></li><li><a href="pages/file_io.html"><strong>2.2.</strong> File IO</a></li><li><a href="pages/cli_parsing.html"><strong>2.3.</strong> Command Line Parsing</a></li></ul></li><li><a href="pages/serialization.html"><strong>3.</strong> Serialization</a></li><li><ul class="section"><li><a href="pages/json.html"><strong>3.1.</strong> JSON</a></li><li><a href="pages/toml.html" class="active"><strong>3.2.</strong> TOML</a></li></ul></li><li><a href="pages/contributing.html">Contributing</a></li></ul>
|
|
</div>
|
|
|
|
<div id="page-wrapper" class="page-wrapper">
|
|
|
|
<div class="page">
|
|
<div id="menu-bar" class="menu-bar">
|
|
<div class="left-buttons">
|
|
<i id="sidebar-toggle" class="fa fa-bars"></i>
|
|
<i id="theme-toggle" class="fa fa-paint-brush"></i>
|
|
</div>
|
|
|
|
<h1 class="menu-title"></h1>
|
|
|
|
<div class="right-buttons">
|
|
<i id="print-button" class="fa fa-print" title="Print this book"></i>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="content" class="content">
|
|
<a class="header" href="#toml" name="toml"><h1>TOML</h1></a>
|
|
<p><a href="http://alexcrichton.com/toml-rs/toml/"><img src="https://img.shields.io/crates/v/rustc-serialize.svg?label=toml" alt="toml-badge" /></a></p>
|
|
<p>Parse TOML into a <code>toml::Value</code> and then operate on it:</p>
|
|
<pre><code class="language-rust">extern crate toml;
|
|
|
|
fn main() {
|
|
|
|
let toml_source = "
|
|
[package]
|
|
name = \"your package!\"
|
|
version = \"0.1.0\"
|
|
authors = [\"You! <you@example.org>\"]
|
|
|
|
[dependencies]
|
|
cool = \"0.2.1\"";
|
|
|
|
let package_info = toml_source.parse::<toml::Value>().unwrap();
|
|
|
|
assert_eq!(package_info["dependencies"]["cool"].as_str(), Some("0.2.1"));
|
|
assert_eq!(package_info["package"]["name"].as_str(), Some("your package!"));
|
|
|
|
}
|
|
</code></pre>
|
|
<p>Parse TOML into your own structs using the <code>serde</code> crate:</p>
|
|
<pre><code class="language-rust">
|
|
extern crate toml;
|
|
|
|
#[macro_use]
|
|
extern crate serde_derive;
|
|
extern crate serde;
|
|
|
|
#[derive(Deserialize)]
|
|
struct Config {
|
|
package: Package,
|
|
dependencies: std::collections::HashMap<String, String>,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct Package {
|
|
name: String,
|
|
version: String,
|
|
authors: Vec<String>,
|
|
}
|
|
|
|
fn main() {
|
|
|
|
let toml_source = "
|
|
[package]
|
|
name = \"your package!\"
|
|
version = \"0.1.0\"
|
|
authors = [\"You! <you@example.org>\"]
|
|
|
|
[dependencies]
|
|
cool = \"0.2.1\"";
|
|
|
|
let package_info : Config = toml::from_str(toml_source).unwrap();
|
|
|
|
assert_eq!(package_info.package.name, "your package!");
|
|
assert_eq!(package_info.package.version, "0.1.0");
|
|
assert_eq!(package_info.package.authors, vec!["You! <you@example.org>"]);
|
|
assert_eq!(package_info.dependencies["cool"], "0.2.1");
|
|
|
|
}
|
|
</code></pre>
|
|
<a class="header" href="#license" name="license"><h1>License</h1></a>
|
|
<p>MIT/Apache-2.0</p>
|
|
<!-- Links -->
|
|
|
|
</div>
|
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
|
<a href="pages/json.html" class="mobile-nav-chapters previous">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
|
|
|
|
<a href="pages/contributing.html" class="mobile-nav-chapters next">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<a href="pages/json.html" class="nav-chapters previous" title="You can navigate through the chapters using the arrow keys">
|
|
<i class="fa fa-angle-left"></i>
|
|
</a>
|
|
|
|
|
|
|
|
<a href="pages/contributing.html" class="nav-chapters next" title="You can navigate through the chapters using the arrow keys">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<!-- Local fallback for Font Awesome -->
|
|
<script>
|
|
if ($(".fa").css("font-family") !== "FontAwesome") {
|
|
$('<link rel="stylesheet" type="text/css" href="_FontAwesome/css/font-awesome.css">').prependTo('head');
|
|
}
|
|
</script>
|
|
|
|
<!-- Livereload script (if served using the cli tool) -->
|
|
|
|
<script type="text/javascript">
|
|
var socket = new WebSocket("ws://localhost:3001");
|
|
socket.onmessage = function (event) {
|
|
if (event.data === "reload") {
|
|
socket.close();
|
|
location.reload(true); // force reload from server (not from cache)
|
|
}
|
|
};
|
|
|
|
window.onbeforeunload = function() {
|
|
socket.close();
|
|
}
|
|
</script>
|
|
|
|
|
|
<script src="highlight.js"></script>
|
|
<script src="book.js"></script>
|
|
</body>
|
|
</html>
|