|
| 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 | +} |
0 commit comments