Skip to content

Commit 00cb362

Browse files
committed
Extract a modify-cargo-toml library for reuse
1 parent 2136e81 commit 00cb362

File tree

2 files changed

+134
-128
lines changed

2 files changed

+134
-128
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
extern crate serde;
2+
#[macro_use]
3+
extern crate serde_derive;
4+
extern crate toml;
5+
6+
use std::{collections::BTreeMap};
7+
use toml::Value;
8+
9+
type Other = BTreeMap<String, Value>;
10+
11+
fn modify<F, T>(cargo_toml: Value, f: F) -> Value
12+
where
13+
F: FnOnce(T) -> T,
14+
T: serde::Serialize + for<'de> serde::Deserialize<'de>,
15+
{
16+
let cargo_toml = cargo_toml.try_into().unwrap();
17+
18+
let cargo_toml = f(cargo_toml);
19+
20+
Value::try_from(cargo_toml).unwrap()
21+
}
22+
23+
fn ensure_string_in_vec(values: &mut Vec<String>, val: &str) {
24+
if !values.iter().any(|f| f == val) {
25+
values.push(val.into());
26+
}
27+
}
28+
29+
pub fn set_edition(cargo_toml: Value, edition: &str) -> Value {
30+
#[derive(Debug, Serialize, Deserialize)]
31+
#[serde(rename_all = "kebab-case")]
32+
struct CargoToml {
33+
package: Package,
34+
#[serde(flatten)]
35+
other: Other,
36+
}
37+
38+
#[derive(Debug, Serialize, Deserialize)]
39+
#[serde(rename_all = "kebab-case")]
40+
struct Package {
41+
#[serde(default)]
42+
edition: String,
43+
#[serde(flatten)]
44+
other: Other,
45+
}
46+
47+
modify(cargo_toml, |mut cargo_toml: CargoToml| {
48+
cargo_toml.package.edition = edition.into();
49+
cargo_toml
50+
})
51+
}
52+
53+
pub fn remove_dependencies(cargo_toml: Value) -> Value {
54+
#[derive(Debug, Serialize, Deserialize)]
55+
#[serde(rename_all = "kebab-case")]
56+
struct CargoToml {
57+
dependencies: BTreeMap<String, Value>,
58+
#[serde(flatten)]
59+
other: Other,
60+
}
61+
62+
modify(cargo_toml, |mut cargo_toml: CargoToml| {
63+
cargo_toml.dependencies.clear();
64+
cargo_toml
65+
})
66+
}
67+
68+
pub fn set_crate_type(cargo_toml: Value, crate_type: &str) -> Value {
69+
#[derive(Debug, Serialize, Deserialize)]
70+
#[serde(rename_all = "kebab-case")]
71+
struct CargoToml {
72+
#[serde(default)]
73+
lib: Lib,
74+
#[serde(flatten)]
75+
other: Other,
76+
}
77+
78+
#[derive(Debug, Default, Serialize, Deserialize)]
79+
#[serde(rename_all = "kebab-case")]
80+
struct Lib {
81+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
82+
crate_type: Vec<String>,
83+
#[serde(default)]
84+
proc_macro: bool,
85+
#[serde(flatten)]
86+
other: Other,
87+
}
88+
89+
modify(cargo_toml, |mut cargo_toml: CargoToml| {
90+
if crate_type == "proc-macro" {
91+
cargo_toml.lib.proc_macro = true;
92+
} else {
93+
ensure_string_in_vec(&mut cargo_toml.lib.crate_type, crate_type);
94+
}
95+
cargo_toml
96+
})
97+
}
98+
99+
pub fn set_release_lto(cargo_toml: Value, lto: bool) -> Value {
100+
#[derive(Debug, Serialize, Deserialize)]
101+
#[serde(rename_all = "kebab-case")]
102+
struct CargoToml {
103+
#[serde(default)]
104+
profile: Profiles,
105+
#[serde(flatten)]
106+
other: Other,
107+
}
108+
109+
#[derive(Debug, Default, Serialize, Deserialize)]
110+
#[serde(rename_all = "kebab-case")]
111+
struct Profiles {
112+
#[serde(default)]
113+
release: Profile,
114+
#[serde(flatten)]
115+
other: Other,
116+
}
117+
118+
#[derive(Debug, Default, Serialize, Deserialize)]
119+
#[serde(rename_all = "kebab-case")]
120+
struct Profile {
121+
#[serde(default)]
122+
lto: bool,
123+
#[serde(flatten)]
124+
other: Other,
125+
}
126+
127+
modify(cargo_toml, |mut cargo_toml: CargoToml| {
128+
cargo_toml.profile.release.lto = lto;
129+
cargo_toml
130+
})
131+
}
Lines changed: 3 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
extern crate serde;
2-
#[macro_use]
3-
extern crate serde_derive;
1+
extern crate modify_cargo_toml;
42
extern crate toml;
53

6-
use std::{collections::BTreeMap, env, ffi::OsString, fs, path::PathBuf};
4+
use modify_cargo_toml::*;
5+
use std::{env, ffi::OsString, fs, path::PathBuf};
76
use toml::Value;
87

98
fn main() {
@@ -41,127 +40,3 @@ fn main() {
4140
fs::write(&output_filename, output)
4241
.unwrap_or_else(|e| panic!("Cannot write to {}: {}", output_filename.display(), e));
4342
}
44-
45-
type Other = BTreeMap<String, Value>;
46-
47-
fn modify<F, T>(cargo_toml: Value, f: F) -> Value
48-
where
49-
F: FnOnce(T) -> T,
50-
T: serde::Serialize + for<'de> serde::Deserialize<'de>,
51-
{
52-
let cargo_toml = cargo_toml.try_into().unwrap();
53-
54-
let cargo_toml = f(cargo_toml);
55-
56-
Value::try_from(cargo_toml).unwrap()
57-
}
58-
59-
fn ensure_string_in_vec(values: &mut Vec<String>, val: &str) {
60-
if !values.iter().any(|f| f == val) {
61-
values.push(val.into());
62-
}
63-
}
64-
65-
fn set_edition(cargo_toml: Value, edition: &str) -> Value {
66-
#[derive(Debug, Serialize, Deserialize)]
67-
#[serde(rename_all = "kebab-case")]
68-
struct CargoToml {
69-
package: Package,
70-
#[serde(flatten)]
71-
other: Other,
72-
}
73-
74-
#[derive(Debug, Serialize, Deserialize)]
75-
#[serde(rename_all = "kebab-case")]
76-
struct Package {
77-
#[serde(default)]
78-
edition: String,
79-
#[serde(flatten)]
80-
other: Other,
81-
}
82-
83-
modify(cargo_toml, |mut cargo_toml: CargoToml| {
84-
cargo_toml.package.edition = edition.into();
85-
cargo_toml
86-
})
87-
}
88-
89-
fn remove_dependencies(cargo_toml: Value) -> Value {
90-
#[derive(Debug, Serialize, Deserialize)]
91-
#[serde(rename_all = "kebab-case")]
92-
struct CargoToml {
93-
dependencies: BTreeMap<String, Value>,
94-
#[serde(flatten)]
95-
other: Other,
96-
}
97-
98-
modify(cargo_toml, |mut cargo_toml: CargoToml| {
99-
cargo_toml.dependencies.clear();
100-
cargo_toml
101-
})
102-
}
103-
104-
fn set_crate_type(cargo_toml: Value, crate_type: &str) -> Value {
105-
#[derive(Debug, Serialize, Deserialize)]
106-
#[serde(rename_all = "kebab-case")]
107-
struct CargoToml {
108-
#[serde(default)]
109-
lib: Lib,
110-
#[serde(flatten)]
111-
other: Other,
112-
}
113-
114-
#[derive(Debug, Default, Serialize, Deserialize)]
115-
#[serde(rename_all = "kebab-case")]
116-
struct Lib {
117-
#[serde(default, skip_serializing_if = "Vec::is_empty")]
118-
crate_type: Vec<String>,
119-
#[serde(default)]
120-
proc_macro: bool,
121-
#[serde(flatten)]
122-
other: Other,
123-
}
124-
125-
modify(cargo_toml, |mut cargo_toml: CargoToml| {
126-
if crate_type == "proc-macro" {
127-
cargo_toml.lib.proc_macro = true;
128-
} else {
129-
ensure_string_in_vec(&mut cargo_toml.lib.crate_type, crate_type);
130-
}
131-
cargo_toml
132-
})
133-
}
134-
135-
fn set_release_lto(cargo_toml: Value, lto: bool) -> Value {
136-
#[derive(Debug, Serialize, Deserialize)]
137-
#[serde(rename_all = "kebab-case")]
138-
struct CargoToml {
139-
#[serde(default)]
140-
profile: Profiles,
141-
#[serde(flatten)]
142-
other: Other,
143-
}
144-
145-
#[derive(Debug, Default, Serialize, Deserialize)]
146-
#[serde(rename_all = "kebab-case")]
147-
struct Profiles {
148-
#[serde(default)]
149-
release: Profile,
150-
#[serde(flatten)]
151-
other: Other,
152-
}
153-
154-
#[derive(Debug, Default, Serialize, Deserialize)]
155-
#[serde(rename_all = "kebab-case")]
156-
struct Profile {
157-
#[serde(default)]
158-
lto: bool,
159-
#[serde(flatten)]
160-
other: Other,
161-
}
162-
163-
modify(cargo_toml, |mut cargo_toml: CargoToml| {
164-
cargo_toml.profile.release.lto = lto;
165-
cargo_toml
166-
})
167-
}

0 commit comments

Comments
 (0)