How to do it...

  1. Open the Cargo.toml file that was generated earlier for you

  1. Under [dependencies], add the following line:
toml = "0.4.5"
  1. If you haven't done so already, add the following lines as well:
serde = "1.0.24"
serde_derive
= "1.0.24"
  1. If you want, you can go to the crates.io web pages for TOML (https://crates.io/crates/toml), Serde (https://crates.io/crates/serde), and serde_derive (https://crates.io/crates/serde_derive) to check for the newest versions and use those ones instead
  2. In the bin folder, create a file called toml.rs

  3. Add the following code and run it with cargo run --bin toml:

1    #[macro_use]
2 extern crate serde_derive;
3 extern crate toml;
4
5 use std::io::{BufReader, BufWriter, Read, Seek, SeekFrom,
Write};
6 use std::fs::OpenOptions;
  1. These are the structures we are going to use throughout the recipe:
8    #[derive(Serialize, Deserialize)]
9 struct Preferences {
10 person: Person,
11 language: Language,
12 privacy: Privacy,
13 }
14
15 #[derive(Serialize, Deserialize)]
16 struct Person {
17 name: String,
18 email: String,
19 }
20
21 #[derive(Serialize, Deserialize)]
22 struct Language {
23 display: String,
24 autocorrect: Option<Vec<String>>,
25 }
26
27 #[derive(Serialize, Deserialize)]
28 struct Privacy {
29 share_anonymous_statistics: bool,
30 public_name: bool,
31 public_email: bool,
32 }
  1. Prepare a new file and call the other functions:
34   fn main() {
35 let file = OpenOptions::new()
36 .read(true)
37 .write(true)
38 .create(true)
39 .open("preferences.toml")
40 .expect("failed to create TOML file");
41
42 let buf_writer = BufWriter::new(&file);
43 write_toml(buf_writer).expect("Failed to write TOML");
44
45 let mut buf_reader = BufReader::new(&file);
46 buf_reader
47 .seek(SeekFrom::Start(0))
48 .expect("Failed to jump to the beginning of the TOML
file");
49 read_toml(buf_reader).expect("Failed to read TOML");
50 }
  1. Save our structures as a TOML file:
52   type SerializeResult<T> = Result<T, toml::ser::Error>;
53 fn write_toml<W>(mut writer: W) -> SerializeResult<()>
54 where
55 W: Write,
56 {
57 let preferences = Preferences {
58 person: Person {
59 name: "Jan Nils Ferner".to_string(),
60 email: "[email protected]".to_string(),
61 },
62 language: Language {
63 display: "en-GB".to_string(),
64 autocorrect: Some(vec![
65 "en-GB".to_string(),
66 "en-US".to_string(),
67 "de-CH".to_string(),
68 ]),
69 },
70 privacy: Privacy {
71 share_anonymous_statistics: false,
72 public_name: true,
73 public_email: true,
74 },
75 };
76
77 let toml = toml::to_string(&preferences)?;
78 writer
79 .write_all(toml.as_bytes())
80 .expect("Failed to write file");
81 Ok(())
82 }
  1. Read the TOML file we just created:
84   type DeserializeResult<T> = Result<T, toml::de::Error>;
85 fn read_toml<R>(mut reader: R) -> DeserializeResult<()>
86 where
87 R: Read,
88 {
89 let mut toml = String::new();
90 reader
91 .read_to_string(&mut toml)
92 .expect("Failed to read TOML");
93 let preferences: Preferences = toml::from_str(&toml)?;
94
95 println!("Personal data:");
96 let person = &preferences.person;
97 println!(" Name: {}", person.name);
98 println!(" Email: {}", person.email);
99
100 println!(" Language preferences:");
101 let language = &preferences.language;
102 println!(" Display language: {}", language.display);
103 println!(" Autocorrect priority: {:?}",
language.autocorrect);
104
105
106 println!(" Privacy settings:");
107 let privacy = &preferences.privacy;
108 println!(
109 " Share anonymous usage statistics: {}",
110 privacy.share_anonymous_statistics
111 );
112 println!(" Display name publically: {}",
privacy.public_name);
113 println!(" Display email publically: {}",
privacy.public_email);
114
115 Ok(())
116 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.66.128