rust-manifest 0.1.0

Types belonging to a Cargo.toml file. Supports (de)serialization, JSON schema and merging
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use super::*;

/// Handling of LTO in a build profile
#[derive(Debug, Clone, PartialEq, Serialize, Eq, PartialOrd, Ord, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(try_from = "RawLto", into = "RawLto")]
#[cfg_attr(feature = "schemars", schemars(with = "RawLto"))]
#[serde(rename_all = "kebab-case")]
pub enum LtoSetting {
	None,
	ThinLocal,
	Thin,
	Fat,
}

#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(untagged)]
enum RawLto {
	Bool(bool),
	String(String),
}

impl TryFrom<RawLto> for LtoSetting {
	type Error = String;

	fn try_from(value: RawLto) -> Result<Self, Self::Error> {
		match value {
			RawLto::Bool(false) => Ok(Self::ThinLocal),
			RawLto::Bool(true) => Ok(Self::Fat),

			RawLto::String(s) => match s.as_str() {
				"off" => Ok(Self::None),
				"thin-local" => Ok(Self::ThinLocal),
				"thin" => Ok(Self::Thin),
				"fat" => Ok(Self::Fat),
				_ => Err(format!("Unknown LTO setting: {s}")),
			},
		}
	}
}

impl From<LtoSetting> for RawLto {
	fn from(setting: LtoSetting) -> Self {
		match setting {
			LtoSetting::None => Self::String("off".to_string()),
			LtoSetting::ThinLocal => Self::Bool(false),
			LtoSetting::Thin => Self::String("thin".to_string()),
			LtoSetting::Fat => Self::Bool(true),
		}
	}
}

impl AsTomlValue for LtoSetting {
	fn as_toml_value(&self) -> Item {
		match self {
			Self::None => "off".into(),
			Self::ThinLocal => false.into(),
			Self::Thin => "thin".into(),
			Self::Fat => true.into(),
		}
	}
}

/// Verbosity of debug info in a [`Profile`]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(try_from = "RawDebugSetting", into = "RawDebugSetting")]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "schemars", schemars(with = "RawDebugSetting"))]
pub enum DebugSetting {
	None,               // 0, false, "none"
	LineDirectivesOnly, // "line-directives-only"
	LineTablesOnly,     // "line-tables-only"
	Limited,            // 1, "limited"
	Full,               // 2, true, "full"
}

#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(untagged)]
enum RawDebugSetting {
	Bool(bool),
	Integer(u8),
	String(String),
}

impl TryFrom<RawDebugSetting> for DebugSetting {
	type Error = String;

	fn try_from(value: RawDebugSetting) -> Result<Self, Self::Error> {
		match value {
			RawDebugSetting::Bool(false) | RawDebugSetting::Integer(0) => Ok(Self::None),
			RawDebugSetting::Integer(1) => Ok(Self::Limited),
			RawDebugSetting::Bool(true) | RawDebugSetting::Integer(2) => Ok(Self::Full),
			RawDebugSetting::Integer(n) => {
				Err(format!("Invalid debug level: {n}. Use 0, 1, or 2."))
			}

			RawDebugSetting::String(s) => match s.as_str() {
				"none" | "false" | "0" => Ok(Self::None),

				"line-directives-only" => Ok(Self::LineDirectivesOnly),
				"line-tables-only" => Ok(Self::LineTablesOnly),

				"limited" | "1" => Ok(Self::Limited),
				"full" | "true" | "2" => Ok(Self::Full),
				_ => Err(format!("Unknown debug setting: '{s}'")),
			},
		}
	}
}

impl From<DebugSetting> for RawDebugSetting {
	fn from(setting: DebugSetting) -> Self {
		match setting {
			DebugSetting::None => Self::Bool(false),
			DebugSetting::Full => Self::Bool(true),

			DebugSetting::Limited => Self::Integer(1),

			DebugSetting::LineDirectivesOnly => Self::String("line-directives-only".to_string()),
			DebugSetting::LineTablesOnly => Self::String("line-tables-only".to_string()),
		}
	}
}

impl AsTomlValue for DebugSetting {
	fn as_toml_value(&self) -> Item {
		let raw: RawDebugSetting = (*self).into();

		match raw {
			RawDebugSetting::Bool(bool) => bool.into(),
			RawDebugSetting::Integer(int) => (i64::from(int)).into(),
			RawDebugSetting::String(str) => str.into(),
		}
	}
}

/// Handling of debug symbols in a build profile
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(try_from = "RawStripSetting", into = "RawStripSetting")]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "schemars", schemars(with = "RawStripSetting"))]
pub enum StripSetting {
	None,
	Debuginfo,
	Symbols,
}

#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(untagged)]
enum RawStripSetting {
	Bool(bool),
	String(String),
}

impl TryFrom<RawStripSetting> for StripSetting {
	type Error = String;

	fn try_from(value: RawStripSetting) -> Result<Self, Self::Error> {
		match value {
			RawStripSetting::Bool(false) => Ok(Self::None),
			RawStripSetting::Bool(true) => Ok(Self::Symbols),

			RawStripSetting::String(s) => match s.as_str() {
				"debuginfo" => Ok(Self::Debuginfo),
				"symbols" | "true" => Ok(Self::Symbols),

				"none" | "false" => Ok(Self::None),
				_ => Err(format!(
					"Invalid strip setting: '{s}'. Use 'none', 'debuginfo', 'symbols', true, or false."
				)),
			},
		}
	}
}

impl From<StripSetting> for RawStripSetting {
	fn from(setting: StripSetting) -> Self {
		match setting {
			StripSetting::None => Self::String("none".to_string()),
			StripSetting::Debuginfo => Self::String("debuginfo".to_string()),
			StripSetting::Symbols => Self::String("symbols".to_string()),
		}
	}
}

impl AsTomlValue for StripSetting {
	fn as_toml_value(&self) -> Item {
		let raw: RawStripSetting = (*self).into();

		match raw {
			RawStripSetting::Bool(bool) => bool.into(),
			RawStripSetting::String(str) => str.into(),
		}
	}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(try_from = "RawOptLevel", into = "RawOptLevel")]
pub enum OptLevel {
	Zero,
	One,
	Two,
	Three,
	S,
	Z,
}

impl AsTomlValue for OptLevel {
	fn as_toml_value(&self) -> Item {
		match self {
			Self::Zero => Item::from(0),
			Self::One => Item::from(1),
			Self::Two => Item::from(2),
			Self::Three => Item::from(3),
			Self::S => Item::from("s"),
			Self::Z => Item::from("z"),
		}
	}
}

#[derive(Debug, Deserialize, Serialize)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(untagged)]
enum RawOptLevel {
	Integer(u8),
	String(String),
}

impl TryFrom<RawOptLevel> for OptLevel {
	type Error = String;

	fn try_from(value: RawOptLevel) -> Result<Self, Self::Error> {
		match value {
			RawOptLevel::Integer(0) => Ok(Self::Zero),
			RawOptLevel::Integer(1) => Ok(Self::One),
			RawOptLevel::Integer(2) => Ok(Self::Two),
			RawOptLevel::Integer(3) => Ok(Self::Three),

			RawOptLevel::String(s) => match s.as_str() {
				"0" => Ok(Self::Zero),
				"1" => Ok(Self::One),
				"2" => Ok(Self::Two),
				"3" => Ok(Self::Three),
				"s" => Ok(Self::S),
				"z" => Ok(Self::Z),
				_ => Err(format!("Invalid opt-level: {s}")),
			},

			_ => Err("opt-level must be 0-3, 's', or 'z'".to_string()),
		}
	}
}

impl From<OptLevel> for RawOptLevel {
	fn from(val: OptLevel) -> Self {
		match val {
			OptLevel::Zero => Self::Integer(0),
			OptLevel::One => Self::Integer(1),
			OptLevel::Two => Self::Integer(2),
			OptLevel::Three => Self::Integer(3),
			OptLevel::S => Self::String("s".to_string()),
			OptLevel::Z => Self::String("z".to_string()),
		}
	}
}

/// Compilation/optimization settings for a workspace
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize, Merge)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[serde(rename_all = "kebab-case")]
#[serde(deny_unknown_fields)]
pub struct Profile {
	/// The opt-level setting controls the `-C opt-level` flag which controls the level of optimization.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub opt_level: Option<OptLevel>,

	/// The debug setting controls the `-C debuginfo` flag which controls the amount of debug information included in the compiled binary.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub debug: Option<DebugSetting>,

	/// The split-debuginfo setting controls the `-C split-debuginfo` flag which controls whether debug information, if generated, is either placed in the executable itself or adjacent to it.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub split_debuginfo: Option<String>,

	/// The rpath setting controls the `-C rpath` flag which controls whether or not rpath is enabled.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub rpath: Option<bool>,

	/// The lto setting controls rustc’s `-C lto`, `-C linker-plugin-lto`, and `-C embed-bitcode` options, which control LLVM’s link time optimizations.
	///
	/// LTO can produce better optimized code, using whole-program analysis, at the cost of longer linking time.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub lto: Option<LtoSetting>,

	/// The debug-assertions setting controls the `-C debug-assertions` flag which turns `cfg(debug_assertions)` conditional compilation on or off.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub debug_assertions: Option<bool>,

	/// The codegen-units setting controls the `-C codegen-units` flag which controls how many “code generation units” a crate will be split into.
	///
	/// More code generation units allows more of a crate to be processed in parallel possibly reducing compile time, but may produce slower code.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub codegen_units: Option<u16>,

	/// The panic setting controls the `-C panic` flag which controls which panic strategy to use.
	///
	/// The valid options are:
	/// - `unwind`: Unwind the stack upon panic.
	/// - `abort`: Terminate the process upon panic.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub panic: Option<String>,

	/// The incremental setting controls the `-C incremental` flag which controls whether or not incremental compilation is enabled.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub incremental: Option<bool>,

	/// The overflow-checks setting controls the `-C overflow-checks` flag which controls the behavior of runtime integer overflow.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub overflow_checks: Option<bool>,

	/// The strip option controls the `-C strip` flag, which directs rustc to strip either symbols or debuginfo from a binary.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub strip: Option<StripSetting>,

	/// Profile settings can be overridden for specific packages and build-time crates.
	///
	/// To override the settings for a specific package, use the package table to change the settings for the named package:
	/// ```toml
	/// # The `foo` package will use the -Copt-level=3 flag.
	/// [profile.dev.package.foo]
	/// opt-level = 3
	/// ```
	#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
	pub package: BTreeMap<String, Self>,

	/// To override the settings for build scripts, proc macros, and their dependencies, use the build-override table:
	/// ```toml
	/// # Set the settings for build scripts and proc-macros.
	/// [profile.dev.build-override]
	/// opt-level = 3
	/// ```
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub build_override: Option<Box<Self>>,

	/// Specifies which profile the custom profile inherits settings from when the setting is not specified.
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub inherits: Option<String>,
}

impl AsTomlValue for Profile {
	fn as_toml_value(&self) -> Item {
		let mut table = Table::new();

		table.set_implicit(true);

		add_value!(self, table => debug, lto, strip, opt_level, build_override);
		add_string!(self, table => split_debuginfo, panic, inherits);

		add_optional_bool!(self, table => rpath, debug_assertions, incremental, overflow_checks);

		if let Some(codegen_units) = self.codegen_units {
			table["codegen-units"] = i64::from(codegen_units).into();
		}

		if !self.package.is_empty() {
			let mut pkg_table = Table::from_iter(
				self.package
					.iter()
					.map(|(k, v)| (toml_edit::Key::from(k), v.as_toml_value())),
			);

			pkg_table.set_implicit(true);

			table.insert("package", pkg_table.into());
		};

		table.into()
	}
}

/// Custom build/optimization settings
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize, Merge)]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[merge(with = merge_options)]
pub struct Profiles {
	/// Used for `--release`
	#[serde(skip_serializing_if = "Option::is_none")]
	pub release: Option<Profile>,

	/// Used by default
	#[serde(skip_serializing_if = "Option::is_none")]
	pub dev: Option<Profile>,

	/// Used for `cargo test`
	#[serde(skip_serializing_if = "Option::is_none")]
	pub test: Option<Profile>,

	/// Used for `cargo bench`
	#[serde(skip_serializing_if = "Option::is_none")]
	pub bench: Option<Profile>,

	/// User-supplied for `cargo --profile=name`
	#[serde(flatten)]
	#[merge(with = BTreeMap::extend)]
	pub custom: BTreeMap<String, Profile>,
}

impl AsTomlValue for Profiles {
	fn as_toml_value(&self) -> Item {
		let mut table = Table::new();

		table.set_implicit(true);

		add_value!(self, table => dev, test, bench, release);
		add_table!(self, table => custom);

		table.into()
	}
}