1 | use serde::{Deserialize, Serialize}; |
2 | |
3 | #[derive(Debug, Serialize, Deserialize)] |
4 | pub struct Recipe { |
5 | pub name: String, |
6 | pub description: Option<String>, |
7 | #[serde(default)] |
8 | pub modules: Vec<Modules>, |
9 | #[serde(default)] |
10 | pub packages: Vec<Packages>, |
11 | } |
12 | |
13 | #[derive(Debug, Serialize, Deserialize)] |
14 | pub struct Modules { |
15 | pub name: String, |
16 | pub version: Option<String>, |
17 | } |
18 | |
19 | #[derive(Debug, Serialize, Deserialize)] |
20 | pub struct Packages { |
21 | pub name: String, |
22 | pub version: Option<String>, |
23 | } |
24 | |
25 | #[test] |
26 | fn both_ends() { |
27 | let recipe_works = basic_toml::from_str::<Recipe>( |
28 | r#" |
29 | name = "testing" |
30 | description = "example" |
31 | modules = [] |
32 | |
33 | [[packages]] |
34 | name = "base" |
35 | "# , |
36 | ) |
37 | .unwrap(); |
38 | basic_toml::to_string(&recipe_works).unwrap(); |
39 | |
40 | let recipe_fails = basic_toml::from_str::<Recipe>( |
41 | r#" |
42 | name = "testing" |
43 | description = "example" |
44 | packages = [] |
45 | |
46 | [[modules]] |
47 | name = "base" |
48 | "# , |
49 | ) |
50 | .unwrap(); |
51 | let err = basic_toml::to_string(&recipe_fails).unwrap_err(); |
52 | assert_eq!(err.to_string(), "values must be emitted before tables" ); |
53 | } |
54 | |