|
1 |
| -extern crate serde; |
2 |
| -#[macro_use] |
3 |
| -extern crate serde_derive; |
| 1 | +extern crate modify_cargo_toml; |
4 | 2 | extern crate toml;
|
5 | 3 |
|
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}; |
7 | 6 | use toml::Value;
|
8 | 7 |
|
9 | 8 | fn main() {
|
@@ -41,127 +40,3 @@ fn main() {
|
41 | 40 | fs::write(&output_filename, output)
|
42 | 41 | .unwrap_or_else(|e| panic!("Cannot write to {}: {}", output_filename.display(), e));
|
43 | 42 | }
|
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