1use std::collections::BTreeMap;
9use std::collections::BTreeSet;
10#[cfg(feature = "unstable-schema")]
11use std::collections::HashMap;
12use std::fmt::{self, Display, Write};
13use std::path::PathBuf;
14use std::str;
15
16use serde::de::{self, IntoDeserializer as _, Unexpected};
17use serde::ser;
18use serde::{Deserialize, Serialize};
19use serde_untagged::UntaggedEnumVisitor;
20
21use crate::core::PackageIdSpec;
22use crate::restricted_names;
23
24mod rust_version;
25
26pub use crate::restricted_names::NameValidationError;
27pub use rust_version::RustVersion;
28pub use rust_version::RustVersionError;
29
30#[cfg(feature = "unstable-schema")]
31use crate::schema::TomlValueWrapper;
32
33#[derive(Default, Clone, Debug, Deserialize, Serialize)]
35#[serde(rename_all = "kebab-case")]
36#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
37pub struct TomlManifest {
38 pub cargo_features: Option<Vec<String>>,
39
40 pub package: Option<Box<TomlPackage>>,
42 pub project: Option<Box<TomlPackage>>,
43 pub badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
44 pub features: Option<BTreeMap<FeatureName, Vec<String>>>,
45 pub lib: Option<TomlLibTarget>,
46 pub bin: Option<Vec<TomlBinTarget>>,
47 pub example: Option<Vec<TomlExampleTarget>>,
48 pub test: Option<Vec<TomlTestTarget>>,
49 pub bench: Option<Vec<TomlTestTarget>>,
50 pub dependencies: Option<BTreeMap<PackageName, InheritableDependency>>,
51 pub dev_dependencies: Option<BTreeMap<PackageName, InheritableDependency>>,
52 #[serde(rename = "dev_dependencies")]
53 pub dev_dependencies2: Option<BTreeMap<PackageName, InheritableDependency>>,
54 pub build_dependencies: Option<BTreeMap<PackageName, InheritableDependency>>,
55 #[serde(rename = "build_dependencies")]
56 pub build_dependencies2: Option<BTreeMap<PackageName, InheritableDependency>>,
57 pub target: Option<BTreeMap<String, TomlPlatform>>,
58 pub lints: Option<InheritableLints>,
59 pub hints: Option<Hints>,
60
61 pub workspace: Option<TomlWorkspace>,
62 pub profile: Option<TomlProfiles>,
63 pub patch: Option<BTreeMap<String, BTreeMap<PackageName, TomlDependency>>>,
64 pub replace: Option<BTreeMap<String, TomlDependency>>,
65
66 #[serde(skip)]
69 pub _unused_keys: BTreeSet<String>,
70}
71
72impl TomlManifest {
73 pub fn requires_package(&self) -> impl Iterator<Item = &'static str> {
74 [
75 self.badges.as_ref().map(|_| "badges"),
76 self.features.as_ref().map(|_| "features"),
77 self.lib.as_ref().map(|_| "lib"),
78 self.bin.as_ref().map(|_| "bin"),
79 self.example.as_ref().map(|_| "example"),
80 self.test.as_ref().map(|_| "test"),
81 self.bench.as_ref().map(|_| "bench"),
82 self.dependencies.as_ref().map(|_| "dependencies"),
83 self.dev_dependencies().as_ref().map(|_| "dev-dependencies"),
84 self.build_dependencies()
85 .as_ref()
86 .map(|_| "build-dependencies"),
87 self.target.as_ref().map(|_| "target"),
88 self.lints.as_ref().map(|_| "lints"),
89 self.hints.as_ref().map(|_| "hints"),
90 ]
91 .into_iter()
92 .flatten()
93 }
94
95 pub fn has_profiles(&self) -> bool {
96 self.profile.is_some()
97 }
98
99 pub fn package(&self) -> Option<&Box<TomlPackage>> {
100 self.package.as_ref().or(self.project.as_ref())
101 }
102
103 pub fn dev_dependencies(&self) -> Option<&BTreeMap<PackageName, InheritableDependency>> {
104 self.dev_dependencies
105 .as_ref()
106 .or(self.dev_dependencies2.as_ref())
107 }
108
109 pub fn build_dependencies(&self) -> Option<&BTreeMap<PackageName, InheritableDependency>> {
110 self.build_dependencies
111 .as_ref()
112 .or(self.build_dependencies2.as_ref())
113 }
114
115 pub fn features(&self) -> Option<&BTreeMap<FeatureName, Vec<String>>> {
116 self.features.as_ref()
117 }
118
119 pub fn normalized_lints(&self) -> Result<Option<&TomlLints>, UnresolvedError> {
120 self.lints.as_ref().map(|l| l.normalized()).transpose()
121 }
122}
123
124#[derive(Debug, Default, Deserialize, Serialize, Clone)]
125#[serde(rename_all = "kebab-case")]
126#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
127pub struct TomlWorkspace {
128 pub members: Option<Vec<String>>,
129 pub exclude: Option<Vec<String>>,
130 pub default_members: Option<Vec<String>>,
131 pub resolver: Option<String>,
132
133 #[cfg_attr(
134 feature = "unstable-schema",
135 schemars(with = "Option<TomlValueWrapper>")
136 )]
137 pub metadata: Option<toml::Value>,
138
139 pub package: Option<InheritablePackage>,
141 pub dependencies: Option<BTreeMap<PackageName, TomlDependency>>,
142 pub lints: Option<TomlLints>,
143}
144
145#[derive(Clone, Debug, Default, Deserialize, Serialize)]
147#[serde(rename_all = "kebab-case")]
148#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
149pub struct InheritablePackage {
150 pub version: Option<semver::Version>,
151 pub authors: Option<Vec<String>>,
152 pub description: Option<String>,
153 pub homepage: Option<String>,
154 pub documentation: Option<String>,
155 pub readme: Option<StringOrBool>,
156 pub keywords: Option<Vec<String>>,
157 pub categories: Option<Vec<String>>,
158 pub license: Option<String>,
159 pub license_file: Option<String>,
160 pub repository: Option<String>,
161 pub publish: Option<VecStringOrBool>,
162 pub edition: Option<String>,
163 pub badges: Option<BTreeMap<String, BTreeMap<String, String>>>,
164 pub exclude: Option<Vec<String>>,
165 pub include: Option<Vec<String>>,
166 #[cfg_attr(feature = "unstable-schema", schemars(with = "Option<String>"))]
167 pub rust_version: Option<RustVersion>,
168}
169
170#[derive(Deserialize, Serialize, Clone, Debug, Default)]
177#[serde(rename_all = "kebab-case")]
178#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
179pub struct TomlPackage {
180 pub edition: Option<InheritableString>,
181 #[cfg_attr(feature = "unstable-schema", schemars(with = "Option<String>"))]
182 pub rust_version: Option<InheritableRustVersion>,
183 #[cfg_attr(feature = "unstable-schema", schemars(with = "Option<String>"))]
184 pub name: Option<PackageName>,
185 pub version: Option<InheritableSemverVersion>,
186 pub authors: Option<InheritableVecString>,
187 pub build: Option<TomlPackageBuild>,
188 pub metabuild: Option<StringOrVec>,
189 pub default_target: Option<String>,
190 pub forced_target: Option<String>,
191 pub links: Option<String>,
192 pub exclude: Option<InheritableVecString>,
193 pub include: Option<InheritableVecString>,
194 pub publish: Option<InheritableVecStringOrBool>,
195 pub workspace: Option<String>,
196 pub im_a_teapot: Option<bool>,
197 pub autolib: Option<bool>,
198 pub autobins: Option<bool>,
199 pub autoexamples: Option<bool>,
200 pub autotests: Option<bool>,
201 pub autobenches: Option<bool>,
202 pub default_run: Option<String>,
203
204 pub description: Option<InheritableString>,
206 pub homepage: Option<InheritableString>,
207 pub documentation: Option<InheritableString>,
208 pub readme: Option<InheritableStringOrBool>,
209 pub keywords: Option<InheritableVecString>,
210 pub categories: Option<InheritableVecString>,
211 pub license: Option<InheritableString>,
212 pub license_file: Option<InheritableString>,
213 pub repository: Option<InheritableString>,
214 pub resolver: Option<String>,
215
216 #[cfg_attr(
217 feature = "unstable-schema",
218 schemars(with = "Option<TomlValueWrapper>")
219 )]
220 pub metadata: Option<toml::Value>,
221
222 #[serde(rename = "cargo-features", skip_serializing)]
224 #[cfg_attr(feature = "unstable-schema", schemars(skip))]
225 pub _invalid_cargo_features: Option<InvalidCargoFeatures>,
226}
227
228impl TomlPackage {
229 pub fn new(name: PackageName) -> Self {
230 Self {
231 name: Some(name),
232 ..Default::default()
233 }
234 }
235
236 pub fn normalized_name(&self) -> Result<&PackageName, UnresolvedError> {
237 self.name.as_ref().ok_or(UnresolvedError)
238 }
239
240 pub fn normalized_edition(&self) -> Result<Option<&String>, UnresolvedError> {
241 self.edition.as_ref().map(|v| v.normalized()).transpose()
242 }
243
244 pub fn normalized_rust_version(&self) -> Result<Option<&RustVersion>, UnresolvedError> {
245 self.rust_version
246 .as_ref()
247 .map(|v| v.normalized())
248 .transpose()
249 }
250
251 pub fn normalized_version(&self) -> Result<Option<&semver::Version>, UnresolvedError> {
252 self.version.as_ref().map(|v| v.normalized()).transpose()
253 }
254
255 pub fn normalized_authors(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
256 self.authors.as_ref().map(|v| v.normalized()).transpose()
257 }
258
259 pub fn normalized_build(&self) -> Result<Option<&[String]>, UnresolvedError> {
260 let build = self.build.as_ref().ok_or(UnresolvedError)?;
261 match build {
262 TomlPackageBuild::Auto(false) => Ok(None),
263 TomlPackageBuild::Auto(true) => Err(UnresolvedError),
264 TomlPackageBuild::SingleScript(value) => Ok(Some(std::slice::from_ref(value))),
265 TomlPackageBuild::MultipleScript(scripts) => Ok(Some(scripts)),
266 }
267 }
268
269 pub fn normalized_exclude(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
270 self.exclude.as_ref().map(|v| v.normalized()).transpose()
271 }
272
273 pub fn normalized_include(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
274 self.include.as_ref().map(|v| v.normalized()).transpose()
275 }
276
277 pub fn normalized_publish(&self) -> Result<Option<&VecStringOrBool>, UnresolvedError> {
278 self.publish.as_ref().map(|v| v.normalized()).transpose()
279 }
280
281 pub fn normalized_description(&self) -> Result<Option<&String>, UnresolvedError> {
282 self.description
283 .as_ref()
284 .map(|v| v.normalized())
285 .transpose()
286 }
287
288 pub fn normalized_homepage(&self) -> Result<Option<&String>, UnresolvedError> {
289 self.homepage.as_ref().map(|v| v.normalized()).transpose()
290 }
291
292 pub fn normalized_documentation(&self) -> Result<Option<&String>, UnresolvedError> {
293 self.documentation
294 .as_ref()
295 .map(|v| v.normalized())
296 .transpose()
297 }
298
299 pub fn normalized_readme(&self) -> Result<Option<&String>, UnresolvedError> {
300 let readme = self.readme.as_ref().ok_or(UnresolvedError)?;
301 readme.normalized().and_then(|sb| match sb {
302 StringOrBool::Bool(false) => Ok(None),
303 StringOrBool::Bool(true) => Err(UnresolvedError),
304 StringOrBool::String(value) => Ok(Some(value)),
305 })
306 }
307
308 pub fn normalized_keywords(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
309 self.keywords.as_ref().map(|v| v.normalized()).transpose()
310 }
311
312 pub fn normalized_categories(&self) -> Result<Option<&Vec<String>>, UnresolvedError> {
313 self.categories.as_ref().map(|v| v.normalized()).transpose()
314 }
315
316 pub fn normalized_license(&self) -> Result<Option<&String>, UnresolvedError> {
317 self.license.as_ref().map(|v| v.normalized()).transpose()
318 }
319
320 pub fn normalized_license_file(&self) -> Result<Option<&String>, UnresolvedError> {
321 self.license_file
322 .as_ref()
323 .map(|v| v.normalized())
324 .transpose()
325 }
326
327 pub fn normalized_repository(&self) -> Result<Option<&String>, UnresolvedError> {
328 self.repository.as_ref().map(|v| v.normalized()).transpose()
329 }
330}
331
332#[derive(Serialize, Copy, Clone, Debug)]
334#[serde(untagged)]
335#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
336pub enum InheritableField<T> {
337 Value(T),
339 Inherit(TomlInheritedField),
341}
342
343impl<T> InheritableField<T> {
344 pub fn normalized(&self) -> Result<&T, UnresolvedError> {
345 self.as_value().ok_or(UnresolvedError)
346 }
347
348 pub fn as_value(&self) -> Option<&T> {
349 match self {
350 InheritableField::Inherit(_) => None,
351 InheritableField::Value(defined) => Some(defined),
352 }
353 }
354
355 pub fn into_value(self) -> Option<T> {
356 match self {
357 Self::Inherit(_) => None,
358 Self::Value(defined) => Some(defined),
359 }
360 }
361
362 pub fn is_inherited(&self) -> bool {
363 matches!(self, Self::Inherit(_))
364 }
365}
366
367pub type InheritableSemverVersion = InheritableField<semver::Version>;
369impl<'de> de::Deserialize<'de> for InheritableSemverVersion {
370 fn deserialize<D>(d: D) -> Result<Self, D::Error>
371 where
372 D: de::Deserializer<'de>,
373 {
374 UntaggedEnumVisitor::new()
375 .expecting("SemVer version")
376 .string(
377 |value| match value.trim().parse().map_err(de::Error::custom) {
378 Ok(parsed) => Ok(InheritableField::Value(parsed)),
379 Err(e) => Err(e),
380 },
381 )
382 .map(|value| value.deserialize().map(InheritableField::Inherit))
383 .deserialize(d)
384 }
385}
386
387pub type InheritableString = InheritableField<String>;
388impl<'de> de::Deserialize<'de> for InheritableString {
389 fn deserialize<D>(d: D) -> Result<Self, D::Error>
390 where
391 D: de::Deserializer<'de>,
392 {
393 struct Visitor;
394
395 impl<'de> de::Visitor<'de> for Visitor {
396 type Value = InheritableString;
397
398 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
399 f.write_str("a string or workspace")
400 }
401
402 fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
403 where
404 E: de::Error,
405 {
406 Ok(InheritableString::Value(value))
407 }
408
409 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
410 where
411 E: de::Error,
412 {
413 self.visit_string(value.to_owned())
414 }
415
416 fn visit_map<V>(self, map: V) -> Result<Self::Value, V::Error>
417 where
418 V: de::MapAccess<'de>,
419 {
420 let mvd = de::value::MapAccessDeserializer::new(map);
421 TomlInheritedField::deserialize(mvd).map(InheritableField::Inherit)
422 }
423 }
424
425 d.deserialize_any(Visitor)
426 }
427}
428
429pub type InheritableRustVersion = InheritableField<RustVersion>;
430impl<'de> de::Deserialize<'de> for InheritableRustVersion {
431 fn deserialize<D>(d: D) -> Result<Self, D::Error>
432 where
433 D: de::Deserializer<'de>,
434 {
435 struct Visitor;
436
437 impl<'de> de::Visitor<'de> for Visitor {
438 type Value = InheritableRustVersion;
439
440 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
441 f.write_str("a semver or workspace")
442 }
443
444 fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
445 where
446 E: de::Error,
447 {
448 let value = value.parse::<RustVersion>().map_err(|e| E::custom(e))?;
449 Ok(InheritableRustVersion::Value(value))
450 }
451
452 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
453 where
454 E: de::Error,
455 {
456 self.visit_string(value.to_owned())
457 }
458
459 fn visit_map<V>(self, map: V) -> Result<Self::Value, V::Error>
460 where
461 V: de::MapAccess<'de>,
462 {
463 let mvd = de::value::MapAccessDeserializer::new(map);
464 TomlInheritedField::deserialize(mvd).map(InheritableField::Inherit)
465 }
466 }
467
468 d.deserialize_any(Visitor)
469 }
470}
471
472pub type InheritableVecString = InheritableField<Vec<String>>;
473impl<'de> de::Deserialize<'de> for InheritableVecString {
474 fn deserialize<D>(d: D) -> Result<Self, D::Error>
475 where
476 D: de::Deserializer<'de>,
477 {
478 struct Visitor;
479
480 impl<'de> de::Visitor<'de> for Visitor {
481 type Value = InheritableVecString;
482
483 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
484 f.write_str("a vector of strings or workspace")
485 }
486 fn visit_seq<A>(self, v: A) -> Result<Self::Value, A::Error>
487 where
488 A: de::SeqAccess<'de>,
489 {
490 let seq = de::value::SeqAccessDeserializer::new(v);
491 Vec::deserialize(seq).map(InheritableField::Value)
492 }
493
494 fn visit_map<V>(self, map: V) -> Result<Self::Value, V::Error>
495 where
496 V: de::MapAccess<'de>,
497 {
498 let mvd = de::value::MapAccessDeserializer::new(map);
499 TomlInheritedField::deserialize(mvd).map(InheritableField::Inherit)
500 }
501 }
502
503 d.deserialize_any(Visitor)
504 }
505}
506
507pub type InheritableStringOrBool = InheritableField<StringOrBool>;
508impl<'de> de::Deserialize<'de> for InheritableStringOrBool {
509 fn deserialize<D>(d: D) -> Result<Self, D::Error>
510 where
511 D: de::Deserializer<'de>,
512 {
513 struct Visitor;
514
515 impl<'de> de::Visitor<'de> for Visitor {
516 type Value = InheritableStringOrBool;
517
518 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
519 f.write_str("a string, a bool, or workspace")
520 }
521
522 fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
523 where
524 E: de::Error,
525 {
526 let b = de::value::BoolDeserializer::new(v);
527 StringOrBool::deserialize(b).map(InheritableField::Value)
528 }
529
530 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
531 where
532 E: de::Error,
533 {
534 let string = de::value::StringDeserializer::new(v);
535 StringOrBool::deserialize(string).map(InheritableField::Value)
536 }
537
538 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
539 where
540 E: de::Error,
541 {
542 self.visit_string(value.to_owned())
543 }
544
545 fn visit_map<V>(self, map: V) -> Result<Self::Value, V::Error>
546 where
547 V: de::MapAccess<'de>,
548 {
549 let mvd = de::value::MapAccessDeserializer::new(map);
550 TomlInheritedField::deserialize(mvd).map(InheritableField::Inherit)
551 }
552 }
553
554 d.deserialize_any(Visitor)
555 }
556}
557
558pub type InheritableVecStringOrBool = InheritableField<VecStringOrBool>;
559impl<'de> de::Deserialize<'de> for InheritableVecStringOrBool {
560 fn deserialize<D>(d: D) -> Result<Self, D::Error>
561 where
562 D: de::Deserializer<'de>,
563 {
564 struct Visitor;
565
566 impl<'de> de::Visitor<'de> for Visitor {
567 type Value = InheritableVecStringOrBool;
568
569 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
570 f.write_str("a boolean, a vector of strings, or workspace")
571 }
572
573 fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
574 where
575 E: de::Error,
576 {
577 let b = de::value::BoolDeserializer::new(v);
578 VecStringOrBool::deserialize(b).map(InheritableField::Value)
579 }
580
581 fn visit_seq<A>(self, v: A) -> Result<Self::Value, A::Error>
582 where
583 A: de::SeqAccess<'de>,
584 {
585 let seq = de::value::SeqAccessDeserializer::new(v);
586 VecStringOrBool::deserialize(seq).map(InheritableField::Value)
587 }
588
589 fn visit_map<V>(self, map: V) -> Result<Self::Value, V::Error>
590 where
591 V: de::MapAccess<'de>,
592 {
593 let mvd = de::value::MapAccessDeserializer::new(map);
594 TomlInheritedField::deserialize(mvd).map(InheritableField::Inherit)
595 }
596 }
597
598 d.deserialize_any(Visitor)
599 }
600}
601
602pub type InheritableBtreeMap = InheritableField<BTreeMap<String, BTreeMap<String, String>>>;
603
604impl<'de> de::Deserialize<'de> for InheritableBtreeMap {
605 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
606 where
607 D: de::Deserializer<'de>,
608 {
609 let value = serde_value::Value::deserialize(deserializer)?;
610
611 if let Ok(w) = TomlInheritedField::deserialize(
612 serde_value::ValueDeserializer::<D::Error>::new(value.clone()),
613 ) {
614 return Ok(InheritableField::Inherit(w));
615 }
616 BTreeMap::deserialize(serde_value::ValueDeserializer::<D::Error>::new(value))
617 .map(InheritableField::Value)
618 }
619}
620
621#[derive(Deserialize, Serialize, Copy, Clone, Debug)]
622#[serde(rename_all = "kebab-case")]
623#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
624pub struct TomlInheritedField {
625 workspace: WorkspaceValue,
626}
627
628impl TomlInheritedField {
629 pub fn new() -> Self {
630 TomlInheritedField {
631 workspace: WorkspaceValue,
632 }
633 }
634}
635
636impl Default for TomlInheritedField {
637 fn default() -> Self {
638 Self::new()
639 }
640}
641
642#[derive(Deserialize, Serialize, Copy, Clone, Debug)]
643#[serde(try_from = "bool")]
644#[serde(into = "bool")]
645#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
646struct WorkspaceValue;
647
648impl TryFrom<bool> for WorkspaceValue {
649 type Error = String;
650 fn try_from(other: bool) -> Result<WorkspaceValue, Self::Error> {
651 if other {
652 Ok(WorkspaceValue)
653 } else {
654 Err("`workspace` cannot be false".to_owned())
655 }
656 }
657}
658
659impl From<WorkspaceValue> for bool {
660 fn from(_: WorkspaceValue) -> bool {
661 true
662 }
663}
664
665#[derive(Serialize, Clone, Debug)]
666#[serde(untagged)]
667#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
668pub enum InheritableDependency {
669 Value(TomlDependency),
671 Inherit(TomlInheritedDependency),
673}
674
675impl InheritableDependency {
676 pub fn unused_keys(&self) -> Vec<String> {
677 match self {
678 InheritableDependency::Value(d) => d.unused_keys(),
679 InheritableDependency::Inherit(w) => w._unused_keys.keys().cloned().collect(),
680 }
681 }
682
683 pub fn normalized(&self) -> Result<&TomlDependency, UnresolvedError> {
684 match self {
685 InheritableDependency::Value(d) => Ok(d),
686 InheritableDependency::Inherit(_) => Err(UnresolvedError),
687 }
688 }
689
690 pub fn is_inherited(&self) -> bool {
691 matches!(self, InheritableDependency::Inherit(_))
692 }
693}
694
695impl<'de> de::Deserialize<'de> for InheritableDependency {
696 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
697 where
698 D: de::Deserializer<'de>,
699 {
700 let value = serde_value::Value::deserialize(deserializer)?;
701
702 if let Ok(w) = TomlInheritedDependency::deserialize(serde_value::ValueDeserializer::<
703 D::Error,
704 >::new(value.clone()))
705 {
706 return if w.workspace {
707 Ok(InheritableDependency::Inherit(w))
708 } else {
709 Err(de::Error::custom("`workspace` cannot be false"))
710 };
711 }
712 TomlDependency::deserialize(serde_value::ValueDeserializer::<D::Error>::new(value))
713 .map(InheritableDependency::Value)
714 }
715}
716
717#[derive(Deserialize, Serialize, Clone, Debug)]
718#[serde(rename_all = "kebab-case")]
719#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
720pub struct TomlInheritedDependency {
721 pub workspace: bool,
722 pub features: Option<Vec<String>>,
723 pub default_features: Option<bool>,
724 #[serde(rename = "default_features")]
725 pub default_features2: Option<bool>,
726 pub optional: Option<bool>,
727 pub public: Option<bool>,
728
729 #[serde(skip_serializing)]
731 #[serde(flatten)]
732 #[cfg_attr(feature = "unstable-schema", schemars(skip))]
733 pub _unused_keys: BTreeMap<String, toml::Value>,
734}
735
736impl TomlInheritedDependency {
737 pub fn default_features(&self) -> Option<bool> {
738 self.default_features.or(self.default_features2)
739 }
740}
741
742#[derive(Clone, Debug, Serialize)]
743#[serde(untagged)]
744#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
745pub enum TomlDependency<P: Clone = String> {
746 Simple(String),
749 Detailed(TomlDetailedDependency<P>),
753}
754
755impl TomlDependency {
756 pub fn is_version_specified(&self) -> bool {
757 match self {
758 TomlDependency::Detailed(d) => d.version.is_some(),
759 TomlDependency::Simple(..) => true,
760 }
761 }
762
763 pub fn is_optional(&self) -> bool {
764 match self {
765 TomlDependency::Detailed(d) => d.optional.unwrap_or(false),
766 TomlDependency::Simple(..) => false,
767 }
768 }
769
770 pub fn is_public(&self) -> bool {
771 match self {
772 TomlDependency::Detailed(d) => d.public.unwrap_or(false),
773 TomlDependency::Simple(..) => false,
774 }
775 }
776
777 pub fn default_features(&self) -> Option<bool> {
778 match self {
779 TomlDependency::Detailed(d) => d.default_features(),
780 TomlDependency::Simple(..) => None,
781 }
782 }
783
784 pub fn unused_keys(&self) -> Vec<String> {
785 match self {
786 TomlDependency::Simple(_) => vec![],
787 TomlDependency::Detailed(detailed) => detailed._unused_keys.keys().cloned().collect(),
788 }
789 }
790}
791
792impl<'de, P: Deserialize<'de> + Clone> de::Deserialize<'de> for TomlDependency<P> {
793 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
794 where
795 D: de::Deserializer<'de>,
796 {
797 use serde::de::Error as _;
798 let expected = "a version string like \"0.9.8\" or a \
799 detailed dependency like { version = \"0.9.8\" }";
800 UntaggedEnumVisitor::new()
801 .expecting(expected)
802 .string(|value| Ok(TomlDependency::Simple(value.to_owned())))
803 .bool(|value| {
804 let expected = format!("invalid type: boolean `{value}`, expected {expected}");
805 let err = if value {
806 format!(
807 "{expected}\n\
808 note: if you meant to use a workspace member, you can write\n \
809 dep.workspace = {value}"
810 )
811 } else {
812 expected
813 };
814
815 Err(serde_untagged::de::Error::custom(err))
816 })
817 .map(|value| value.deserialize().map(TomlDependency::Detailed))
818 .deserialize(deserializer)
819 }
820}
821
822#[derive(Deserialize, Serialize, Clone, Debug)]
823#[serde(rename_all = "kebab-case")]
824#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
825pub struct TomlDetailedDependency<P: Clone = String> {
826 pub version: Option<String>,
827
828 #[cfg_attr(feature = "unstable-schema", schemars(with = "Option<String>"))]
829 pub registry: Option<RegistryName>,
830 pub registry_index: Option<String>,
837 pub path: Option<P>,
840 #[cfg_attr(feature = "unstable-schema", schemars(with = "Option<String>"))]
841 pub base: Option<PathBaseName>,
842 pub git: Option<String>,
843 pub branch: Option<String>,
844 pub tag: Option<String>,
845 pub rev: Option<String>,
846 pub features: Option<Vec<String>>,
847 pub optional: Option<bool>,
848 pub default_features: Option<bool>,
849 #[serde(rename = "default_features")]
850 pub default_features2: Option<bool>,
851 #[cfg_attr(feature = "unstable-schema", schemars(with = "Option<String>"))]
852 pub package: Option<PackageName>,
853 pub public: Option<bool>,
854
855 pub artifact: Option<StringOrVec>,
857 pub lib: Option<bool>,
859 pub target: Option<String>,
861
862 #[serde(skip_serializing)]
864 #[serde(flatten)]
865 #[cfg_attr(feature = "unstable-schema", schemars(skip))]
866 pub _unused_keys: BTreeMap<String, toml::Value>,
867}
868
869impl<P: Clone> TomlDetailedDependency<P> {
870 pub fn default_features(&self) -> Option<bool> {
871 self.default_features.or(self.default_features2)
872 }
873}
874
875impl<P: Clone> Default for TomlDetailedDependency<P> {
877 fn default() -> Self {
878 Self {
879 version: Default::default(),
880 registry: Default::default(),
881 registry_index: Default::default(),
882 path: Default::default(),
883 base: Default::default(),
884 git: Default::default(),
885 branch: Default::default(),
886 tag: Default::default(),
887 rev: Default::default(),
888 features: Default::default(),
889 optional: Default::default(),
890 default_features: Default::default(),
891 default_features2: Default::default(),
892 package: Default::default(),
893 public: Default::default(),
894 artifact: Default::default(),
895 lib: Default::default(),
896 target: Default::default(),
897 _unused_keys: Default::default(),
898 }
899 }
900}
901
902#[derive(Deserialize, Serialize, Clone, Debug, Default)]
903#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
904pub struct TomlProfiles(pub BTreeMap<ProfileName, TomlProfile>);
905
906impl TomlProfiles {
907 pub fn get_all(&self) -> &BTreeMap<ProfileName, TomlProfile> {
908 &self.0
909 }
910
911 pub fn get(&self, name: &str) -> Option<&TomlProfile> {
912 self.0.get(name)
913 }
914}
915
916#[derive(Deserialize, Serialize, Clone, Debug, Default, Eq, PartialEq)]
917#[serde(default, rename_all = "kebab-case")]
918#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
919pub struct TomlProfile {
920 pub opt_level: Option<TomlOptLevel>,
921 pub lto: Option<StringOrBool>,
922 pub codegen_backend: Option<String>,
923 pub codegen_units: Option<u32>,
924 pub debug: Option<TomlDebugInfo>,
925 pub split_debuginfo: Option<String>,
926 pub debug_assertions: Option<bool>,
927 pub rpath: Option<bool>,
928 pub panic: Option<String>,
929 pub overflow_checks: Option<bool>,
930 pub incremental: Option<bool>,
931 pub dir_name: Option<String>,
932 pub inherits: Option<String>,
933 pub strip: Option<StringOrBool>,
934 pub rustflags: Option<Vec<String>>,
936 pub package: Option<BTreeMap<ProfilePackageSpec, TomlProfile>>,
939 pub build_override: Option<Box<TomlProfile>>,
940 pub trim_paths: Option<TomlTrimPaths>,
942 pub hint_mostly_unused: Option<bool>,
944 pub frame_pointers: Option<String>,
945}
946
947impl TomlProfile {
948 pub fn merge(&mut self, profile: &Self) {
950 if let Some(v) = &profile.opt_level {
951 self.opt_level = Some(v.clone());
952 }
953
954 if let Some(v) = &profile.lto {
955 self.lto = Some(v.clone());
956 }
957
958 if let Some(v) = &profile.codegen_backend {
959 self.codegen_backend = Some(v.clone());
960 }
961
962 if let Some(v) = profile.codegen_units {
963 self.codegen_units = Some(v);
964 }
965
966 if let Some(v) = profile.debug {
967 self.debug = Some(v);
968 }
969
970 if let Some(v) = profile.debug_assertions {
971 self.debug_assertions = Some(v);
972 }
973
974 if let Some(v) = &profile.split_debuginfo {
975 self.split_debuginfo = Some(v.clone());
976 }
977
978 if let Some(v) = profile.rpath {
979 self.rpath = Some(v);
980 }
981
982 if let Some(v) = &profile.panic {
983 self.panic = Some(v.clone());
984 }
985
986 if let Some(v) = profile.overflow_checks {
987 self.overflow_checks = Some(v);
988 }
989
990 if let Some(v) = profile.incremental {
991 self.incremental = Some(v);
992 }
993
994 if let Some(v) = &profile.rustflags {
995 self.rustflags = Some(v.clone());
996 }
997
998 if let Some(other_package) = &profile.package {
999 match &mut self.package {
1000 Some(self_package) => {
1001 for (spec, other_pkg_profile) in other_package {
1002 match self_package.get_mut(spec) {
1003 Some(p) => p.merge(other_pkg_profile),
1004 None => {
1005 self_package.insert(spec.clone(), other_pkg_profile.clone());
1006 }
1007 }
1008 }
1009 }
1010 None => self.package = Some(other_package.clone()),
1011 }
1012 }
1013
1014 if let Some(other_bo) = &profile.build_override {
1015 match &mut self.build_override {
1016 Some(self_bo) => self_bo.merge(other_bo),
1017 None => self.build_override = Some(other_bo.clone()),
1018 }
1019 }
1020
1021 if let Some(v) = &profile.inherits {
1022 self.inherits = Some(v.clone());
1023 }
1024
1025 if let Some(v) = &profile.dir_name {
1026 self.dir_name = Some(v.clone());
1027 }
1028
1029 if let Some(v) = &profile.strip {
1030 self.strip = Some(v.clone());
1031 }
1032
1033 if let Some(v) = &profile.trim_paths {
1034 self.trim_paths = Some(v.clone())
1035 }
1036
1037 if let Some(v) = profile.hint_mostly_unused {
1038 self.hint_mostly_unused = Some(v);
1039 }
1040
1041 if let Some(v) = &profile.frame_pointers {
1042 self.frame_pointers = Some(v.clone());
1043 }
1044 }
1045}
1046
1047#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
1048#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1049pub enum ProfilePackageSpec {
1050 Spec(PackageIdSpec),
1051 All,
1052}
1053
1054impl fmt::Display for ProfilePackageSpec {
1055 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1056 match self {
1057 ProfilePackageSpec::Spec(spec) => spec.fmt(f),
1058 ProfilePackageSpec::All => f.write_str("*"),
1059 }
1060 }
1061}
1062
1063impl ser::Serialize for ProfilePackageSpec {
1064 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
1065 where
1066 S: ser::Serializer,
1067 {
1068 self.to_string().serialize(s)
1069 }
1070}
1071
1072impl<'de> de::Deserialize<'de> for ProfilePackageSpec {
1073 fn deserialize<D>(d: D) -> Result<ProfilePackageSpec, D::Error>
1074 where
1075 D: de::Deserializer<'de>,
1076 {
1077 let string = String::deserialize(d)?;
1078 if string == "*" {
1079 Ok(ProfilePackageSpec::All)
1080 } else {
1081 PackageIdSpec::parse(&string)
1082 .map_err(de::Error::custom)
1083 .map(ProfilePackageSpec::Spec)
1084 }
1085 }
1086}
1087
1088#[derive(Clone, Debug, Eq, PartialEq)]
1089#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1090pub struct TomlOptLevel(pub String);
1091
1092impl ser::Serialize for TomlOptLevel {
1093 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1094 where
1095 S: ser::Serializer,
1096 {
1097 match self.0.parse::<u32>() {
1098 Ok(n) => n.serialize(serializer),
1099 Err(_) => self.0.serialize(serializer),
1100 }
1101 }
1102}
1103
1104impl<'de> de::Deserialize<'de> for TomlOptLevel {
1105 fn deserialize<D>(d: D) -> Result<TomlOptLevel, D::Error>
1106 where
1107 D: de::Deserializer<'de>,
1108 {
1109 use serde::de::Error as _;
1110 UntaggedEnumVisitor::new()
1111 .expecting("an optimization level")
1112 .i64(|value| Ok(TomlOptLevel(value.to_string())))
1113 .string(|value| {
1114 if value == "s" || value == "z" {
1115 Ok(TomlOptLevel(value.to_string()))
1116 } else {
1117 Err(serde_untagged::de::Error::custom(format!(
1118 "must be `0`, `1`, `2`, `3`, `s` or `z`, \
1119 but found the string: \"{}\"",
1120 value
1121 )))
1122 }
1123 })
1124 .deserialize(d)
1125 }
1126}
1127
1128#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
1129#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1130pub enum TomlDebugInfo {
1131 None,
1132 LineDirectivesOnly,
1133 LineTablesOnly,
1134 Limited,
1135 Full,
1136}
1137
1138impl Display for TomlDebugInfo {
1139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1140 match self {
1141 TomlDebugInfo::None => f.write_char('0'),
1142 TomlDebugInfo::Limited => f.write_char('1'),
1143 TomlDebugInfo::Full => f.write_char('2'),
1144 TomlDebugInfo::LineDirectivesOnly => f.write_str("line-directives-only"),
1145 TomlDebugInfo::LineTablesOnly => f.write_str("line-tables-only"),
1146 }
1147 }
1148}
1149
1150impl ser::Serialize for TomlDebugInfo {
1151 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1152 where
1153 S: ser::Serializer,
1154 {
1155 match self {
1156 Self::None => 0.serialize(serializer),
1157 Self::LineDirectivesOnly => "line-directives-only".serialize(serializer),
1158 Self::LineTablesOnly => "line-tables-only".serialize(serializer),
1159 Self::Limited => 1.serialize(serializer),
1160 Self::Full => 2.serialize(serializer),
1161 }
1162 }
1163}
1164
1165impl<'de> de::Deserialize<'de> for TomlDebugInfo {
1166 fn deserialize<D>(d: D) -> Result<TomlDebugInfo, D::Error>
1167 where
1168 D: de::Deserializer<'de>,
1169 {
1170 use serde::de::Error as _;
1171 let expecting = "a boolean, 0, 1, 2, \"none\", \"limited\", \"full\", \"line-tables-only\", or \"line-directives-only\"";
1172 UntaggedEnumVisitor::new()
1173 .expecting(expecting)
1174 .bool(|value| {
1175 Ok(if value {
1176 TomlDebugInfo::Full
1177 } else {
1178 TomlDebugInfo::None
1179 })
1180 })
1181 .i64(|value| {
1182 let debuginfo = match value {
1183 0 => TomlDebugInfo::None,
1184 1 => TomlDebugInfo::Limited,
1185 2 => TomlDebugInfo::Full,
1186 _ => {
1187 return Err(serde_untagged::de::Error::invalid_value(
1188 Unexpected::Signed(value),
1189 &expecting,
1190 ));
1191 }
1192 };
1193 Ok(debuginfo)
1194 })
1195 .string(|value| {
1196 let debuginfo = match value {
1197 "none" => TomlDebugInfo::None,
1198 "limited" => TomlDebugInfo::Limited,
1199 "full" => TomlDebugInfo::Full,
1200 "line-directives-only" => TomlDebugInfo::LineDirectivesOnly,
1201 "line-tables-only" => TomlDebugInfo::LineTablesOnly,
1202 _ => {
1203 return Err(serde_untagged::de::Error::invalid_value(
1204 Unexpected::Str(value),
1205 &expecting,
1206 ));
1207 }
1208 };
1209 Ok(debuginfo)
1210 })
1211 .deserialize(d)
1212 }
1213}
1214
1215#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize)]
1216#[serde(untagged, rename_all = "kebab-case")]
1217#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1218pub enum TomlTrimPaths {
1219 Values(Vec<TomlTrimPathsValue>),
1220 All,
1221}
1222
1223impl TomlTrimPaths {
1224 pub fn none() -> Self {
1225 TomlTrimPaths::Values(Vec::new())
1226 }
1227
1228 pub fn is_none(&self) -> bool {
1229 match self {
1230 TomlTrimPaths::Values(v) => v.is_empty(),
1231 TomlTrimPaths::All => false,
1232 }
1233 }
1234}
1235
1236impl<'de> de::Deserialize<'de> for TomlTrimPaths {
1237 fn deserialize<D>(d: D) -> Result<TomlTrimPaths, D::Error>
1238 where
1239 D: de::Deserializer<'de>,
1240 {
1241 use serde::de::Error as _;
1242 let expecting = r#"a boolean, "none", "diagnostics", "macro", "object", "all", or an array with these options"#;
1243 UntaggedEnumVisitor::new()
1244 .expecting(expecting)
1245 .bool(|value| {
1246 Ok(if value {
1247 TomlTrimPaths::All
1248 } else {
1249 TomlTrimPaths::none()
1250 })
1251 })
1252 .string(|v| match v {
1253 "none" => Ok(TomlTrimPaths::none()),
1254 "all" => Ok(TomlTrimPaths::All),
1255 v => {
1256 let d = v.into_deserializer();
1257 let err = |_: D::Error| {
1258 serde_untagged::de::Error::custom(format!("expected {expecting}"))
1259 };
1260 TomlTrimPathsValue::deserialize(d)
1261 .map_err(err)
1262 .map(|v| v.into())
1263 }
1264 })
1265 .seq(|seq| {
1266 let seq: Vec<String> = seq.deserialize()?;
1267 let seq: Vec<_> = seq
1268 .into_iter()
1269 .map(|s| TomlTrimPathsValue::deserialize(s.into_deserializer()))
1270 .collect::<Result<_, _>>()?;
1271 Ok(seq.into())
1272 })
1273 .deserialize(d)
1274 }
1275}
1276
1277impl fmt::Display for TomlTrimPaths {
1278 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1279 match self {
1280 TomlTrimPaths::All => write!(f, "all"),
1281 TomlTrimPaths::Values(v) if v.is_empty() => write!(f, "none"),
1282 TomlTrimPaths::Values(v) => {
1283 let mut iter = v.iter();
1284 if let Some(value) = iter.next() {
1285 write!(f, "{value}")?;
1286 }
1287 for value in iter {
1288 write!(f, ",{value}")?;
1289 }
1290 Ok(())
1291 }
1292 }
1293 }
1294}
1295
1296impl From<TomlTrimPathsValue> for TomlTrimPaths {
1297 fn from(value: TomlTrimPathsValue) -> Self {
1298 TomlTrimPaths::Values(vec![value])
1299 }
1300}
1301
1302impl From<Vec<TomlTrimPathsValue>> for TomlTrimPaths {
1303 fn from(value: Vec<TomlTrimPathsValue>) -> Self {
1304 TomlTrimPaths::Values(value)
1305 }
1306}
1307
1308#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
1309#[serde(rename_all = "kebab-case")]
1310#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1311pub enum TomlTrimPathsValue {
1312 Diagnostics,
1313 Macro,
1314 Object,
1315}
1316
1317impl TomlTrimPathsValue {
1318 pub fn as_str(&self) -> &'static str {
1319 match self {
1320 TomlTrimPathsValue::Diagnostics => "diagnostics",
1321 TomlTrimPathsValue::Macro => "macro",
1322 TomlTrimPathsValue::Object => "object",
1323 }
1324 }
1325}
1326
1327impl fmt::Display for TomlTrimPathsValue {
1328 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1329 write!(f, "{}", self.as_str())
1330 }
1331}
1332
1333pub type TomlLibTarget = TomlTarget;
1334pub type TomlBinTarget = TomlTarget;
1335pub type TomlExampleTarget = TomlTarget;
1336pub type TomlTestTarget = TomlTarget;
1337pub type TomlBenchTarget = TomlTarget;
1338
1339#[derive(Default, Serialize, Deserialize, Debug, Clone)]
1340#[serde(rename_all = "kebab-case")]
1341#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1342pub struct TomlTarget {
1343 pub name: Option<String>,
1344
1345 pub crate_type: Option<Vec<String>>,
1348 #[serde(rename = "crate_type")]
1349 pub crate_type2: Option<Vec<String>>,
1350
1351 #[cfg_attr(feature = "unstable-schema", schemars(with = "Option<String>"))]
1352 pub path: Option<PathValue>,
1353 pub filename: Option<String>,
1355 pub test: Option<bool>,
1356 pub doctest: Option<bool>,
1357 pub bench: Option<bool>,
1358 pub doc: Option<bool>,
1359 pub doc_scrape_examples: Option<bool>,
1360 pub proc_macro: Option<bool>,
1361 #[serde(rename = "proc_macro")]
1362 pub proc_macro2: Option<bool>,
1363 pub harness: Option<bool>,
1364 pub required_features: Option<Vec<String>>,
1365 pub edition: Option<String>,
1366}
1367
1368impl TomlTarget {
1369 pub fn new() -> TomlTarget {
1370 TomlTarget::default()
1371 }
1372
1373 pub fn proc_macro(&self) -> Option<bool> {
1374 self.proc_macro.or(self.proc_macro2).or_else(|| {
1375 if let Some(types) = self.crate_types() {
1376 if types.contains(&"proc-macro".to_string()) {
1377 return Some(true);
1378 }
1379 }
1380 None
1381 })
1382 }
1383
1384 pub fn crate_types(&self) -> Option<&Vec<String>> {
1385 self.crate_type
1386 .as_ref()
1387 .or_else(|| self.crate_type2.as_ref())
1388 }
1389}
1390
1391macro_rules! str_newtype {
1392 ($name:ident) => {
1393 #[derive(Serialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1395 #[serde(transparent)]
1396 #[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1397 pub struct $name<T: AsRef<str> = String>(T);
1398
1399 impl<T: AsRef<str>> $name<T> {
1400 pub fn into_inner(self) -> T {
1401 self.0
1402 }
1403 }
1404
1405 impl<T: AsRef<str>> AsRef<str> for $name<T> {
1406 fn as_ref(&self) -> &str {
1407 self.0.as_ref()
1408 }
1409 }
1410
1411 impl<T: AsRef<str>> std::ops::Deref for $name<T> {
1412 type Target = T;
1413
1414 fn deref(&self) -> &Self::Target {
1415 &self.0
1416 }
1417 }
1418
1419 impl<T: AsRef<str>> std::borrow::Borrow<str> for $name<T> {
1420 fn borrow(&self) -> &str {
1421 self.0.as_ref()
1422 }
1423 }
1424
1425 impl<'a> std::str::FromStr for $name<String> {
1426 type Err = restricted_names::NameValidationError;
1427
1428 fn from_str(value: &str) -> Result<Self, Self::Err> {
1429 Self::new(value.to_owned())
1430 }
1431 }
1432
1433 impl<'de, T: AsRef<str> + serde::Deserialize<'de>> serde::Deserialize<'de> for $name<T> {
1434 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1435 where
1436 D: serde::Deserializer<'de>,
1437 {
1438 let inner = T::deserialize(deserializer)?;
1439 Self::new(inner).map_err(serde::de::Error::custom)
1440 }
1441 }
1442
1443 impl<T: AsRef<str>> Display for $name<T> {
1444 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1445 self.0.as_ref().fmt(f)
1446 }
1447 }
1448 };
1449}
1450
1451str_newtype!(PackageName);
1452
1453impl<T: AsRef<str>> PackageName<T> {
1454 pub fn new(name: T) -> Result<Self, NameValidationError> {
1456 restricted_names::validate_package_name(name.as_ref())?;
1457 Ok(Self(name))
1458 }
1459}
1460
1461impl PackageName {
1462 pub fn sanitize(name: impl AsRef<str>, placeholder: char) -> Self {
1466 PackageName(restricted_names::sanitize_package_name(
1467 name.as_ref(),
1468 placeholder,
1469 ))
1470 }
1471}
1472
1473str_newtype!(RegistryName);
1474
1475impl<T: AsRef<str>> RegistryName<T> {
1476 pub fn new(name: T) -> Result<Self, NameValidationError> {
1478 restricted_names::validate_registry_name(name.as_ref())?;
1479 Ok(Self(name))
1480 }
1481}
1482
1483str_newtype!(ProfileName);
1484
1485impl<T: AsRef<str>> ProfileName<T> {
1486 pub fn new(name: T) -> Result<Self, NameValidationError> {
1488 restricted_names::validate_profile_name(name.as_ref())?;
1489 Ok(Self(name))
1490 }
1491}
1492
1493str_newtype!(FeatureName);
1494
1495impl<T: AsRef<str>> FeatureName<T> {
1496 pub fn new(name: T) -> Result<Self, NameValidationError> {
1498 restricted_names::validate_feature_name(name.as_ref())?;
1499 Ok(Self(name))
1500 }
1501}
1502
1503str_newtype!(PathBaseName);
1504
1505impl<T: AsRef<str>> PathBaseName<T> {
1506 pub fn new(name: T) -> Result<Self, NameValidationError> {
1508 restricted_names::validate_path_base_name(name.as_ref())?;
1509 Ok(Self(name))
1510 }
1511}
1512
1513#[derive(Serialize, Deserialize, Debug, Clone)]
1515#[serde(rename_all = "kebab-case")]
1516#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1517pub struct TomlPlatform {
1518 pub dependencies: Option<BTreeMap<PackageName, InheritableDependency>>,
1519 pub build_dependencies: Option<BTreeMap<PackageName, InheritableDependency>>,
1520 #[serde(rename = "build_dependencies")]
1521 pub build_dependencies2: Option<BTreeMap<PackageName, InheritableDependency>>,
1522 pub dev_dependencies: Option<BTreeMap<PackageName, InheritableDependency>>,
1523 #[serde(rename = "dev_dependencies")]
1524 pub dev_dependencies2: Option<BTreeMap<PackageName, InheritableDependency>>,
1525}
1526
1527impl TomlPlatform {
1528 pub fn dev_dependencies(&self) -> Option<&BTreeMap<PackageName, InheritableDependency>> {
1529 self.dev_dependencies
1530 .as_ref()
1531 .or(self.dev_dependencies2.as_ref())
1532 }
1533
1534 pub fn build_dependencies(&self) -> Option<&BTreeMap<PackageName, InheritableDependency>> {
1535 self.build_dependencies
1536 .as_ref()
1537 .or(self.build_dependencies2.as_ref())
1538 }
1539}
1540
1541#[derive(Serialize, Debug, Clone)]
1542#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1543pub struct InheritableLints {
1544 #[serde(skip_serializing_if = "std::ops::Not::not")]
1545 #[cfg_attr(feature = "unstable-schema", schemars(default))]
1546 pub workspace: bool,
1547 #[serde(flatten)]
1548 pub lints: TomlLints,
1549}
1550
1551impl InheritableLints {
1552 pub fn normalized(&self) -> Result<&TomlLints, UnresolvedError> {
1553 if self.workspace {
1554 Err(UnresolvedError)
1555 } else {
1556 Ok(&self.lints)
1557 }
1558 }
1559}
1560
1561impl<'de> Deserialize<'de> for InheritableLints {
1562 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1563 where
1564 D: de::Deserializer<'de>,
1565 {
1566 struct InheritableLintsVisitor;
1567
1568 impl<'de> de::Visitor<'de> for InheritableLintsVisitor {
1569 type Value = InheritableLints;
1571
1572 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1574 formatter.write_str("a lints table")
1575 }
1576
1577 fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
1581 where
1582 M: de::MapAccess<'de>,
1583 {
1584 let mut lints = TomlLints::new();
1585 let mut workspace = false;
1586
1587 while let Some(key) = access.next_key()? {
1590 if key == "workspace" {
1591 workspace = match access.next_value()? {
1592 Some(WorkspaceValue) => true,
1593 None => false,
1594 };
1595 } else {
1596 let value = access.next_value()?;
1597 lints.insert(key, value);
1598 }
1599 }
1600
1601 Ok(InheritableLints { workspace, lints })
1602 }
1603 }
1604
1605 deserializer.deserialize_map(InheritableLintsVisitor)
1606 }
1607}
1608
1609pub type TomlLints = BTreeMap<String, TomlToolLints>;
1610
1611pub type TomlToolLints = BTreeMap<String, TomlLint>;
1612
1613#[derive(Serialize, Debug, Clone)]
1614#[serde(untagged)]
1615#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1616pub enum TomlLint {
1617 Level(TomlLintLevel),
1618 Config(TomlLintConfig),
1619}
1620
1621impl<'de> Deserialize<'de> for TomlLint {
1622 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1623 where
1624 D: de::Deserializer<'de>,
1625 {
1626 UntaggedEnumVisitor::new()
1627 .string(|string| {
1628 TomlLintLevel::deserialize(string.into_deserializer()).map(TomlLint::Level)
1629 })
1630 .map(|map| map.deserialize().map(TomlLint::Config))
1631 .deserialize(deserializer)
1632 }
1633}
1634
1635impl TomlLint {
1636 pub fn level(&self) -> TomlLintLevel {
1637 match self {
1638 Self::Level(level) => *level,
1639 Self::Config(config) => config.level,
1640 }
1641 }
1642
1643 pub fn priority(&self) -> i8 {
1644 match self {
1645 Self::Level(_) => 0,
1646 Self::Config(config) => config.priority,
1647 }
1648 }
1649
1650 pub fn config(&self) -> Option<&toml::Table> {
1651 match self {
1652 Self::Level(_) => None,
1653 Self::Config(config) => Some(&config.config),
1654 }
1655 }
1656}
1657
1658#[derive(Serialize, Deserialize, Debug, Clone)]
1659#[serde(rename_all = "kebab-case")]
1660#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1661pub struct TomlLintConfig {
1662 pub level: TomlLintLevel,
1663 #[serde(default)]
1664 pub priority: i8,
1665 #[serde(flatten)]
1666 #[cfg_attr(
1667 feature = "unstable-schema",
1668 schemars(with = "HashMap<String, TomlValueWrapper>")
1669 )]
1670 pub config: toml::Table,
1671}
1672
1673#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq)]
1674#[serde(rename_all = "kebab-case")]
1675#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1676pub enum TomlLintLevel {
1677 Forbid,
1678 Deny,
1679 Warn,
1680 Allow,
1681}
1682
1683#[derive(Serialize, Deserialize, Debug, Default, Clone)]
1684#[serde(rename_all = "kebab-case")]
1685#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1686pub struct Hints {
1687 #[cfg_attr(
1688 feature = "unstable-schema",
1689 schemars(with = "Option<TomlValueWrapper>")
1690 )]
1691 pub mostly_unused: Option<toml::Value>,
1692}
1693
1694#[derive(Copy, Clone, Debug)]
1695pub struct InvalidCargoFeatures {}
1696
1697impl<'de> de::Deserialize<'de> for InvalidCargoFeatures {
1698 fn deserialize<D>(_d: D) -> Result<Self, D::Error>
1699 where
1700 D: de::Deserializer<'de>,
1701 {
1702 use serde::de::Error as _;
1703
1704 Err(D::Error::custom(
1705 "the field `cargo-features` should be set at the top of Cargo.toml before any tables",
1706 ))
1707 }
1708}
1709
1710#[derive(Clone, Debug, Serialize, Eq, PartialEq, PartialOrd, Ord)]
1713#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1714pub struct StringOrVec(pub Vec<String>);
1715
1716impl StringOrVec {
1717 pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, String> {
1718 self.0.iter()
1719 }
1720}
1721
1722impl<'de> de::Deserialize<'de> for StringOrVec {
1723 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1724 where
1725 D: de::Deserializer<'de>,
1726 {
1727 UntaggedEnumVisitor::new()
1728 .expecting("string or list of strings")
1729 .string(|value| Ok(StringOrVec(vec![value.to_owned()])))
1730 .seq(|value| value.deserialize().map(StringOrVec))
1731 .deserialize(deserializer)
1732 }
1733}
1734
1735#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
1736#[serde(untagged)]
1737#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1738pub enum StringOrBool {
1739 String(String),
1740 Bool(bool),
1741}
1742
1743impl<'de> Deserialize<'de> for StringOrBool {
1744 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1745 where
1746 D: de::Deserializer<'de>,
1747 {
1748 UntaggedEnumVisitor::new()
1749 .bool(|b| Ok(StringOrBool::Bool(b)))
1750 .string(|s| Ok(StringOrBool::String(s.to_owned())))
1751 .deserialize(deserializer)
1752 }
1753}
1754
1755#[derive(Clone, Debug, Serialize, Eq, PartialEq)]
1756#[serde(untagged)]
1757#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1758pub enum TomlPackageBuild {
1759 Auto(bool),
1762
1763 SingleScript(String),
1765
1766 MultipleScript(Vec<String>),
1768}
1769
1770impl<'de> Deserialize<'de> for TomlPackageBuild {
1771 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1772 where
1773 D: de::Deserializer<'de>,
1774 {
1775 UntaggedEnumVisitor::new()
1776 .bool(|b| Ok(TomlPackageBuild::Auto(b)))
1777 .string(|s| Ok(TomlPackageBuild::SingleScript(s.to_owned())))
1778 .seq(|value| value.deserialize().map(TomlPackageBuild::MultipleScript))
1779 .deserialize(deserializer)
1780 }
1781}
1782
1783#[derive(PartialEq, Clone, Debug, Serialize)]
1784#[serde(untagged)]
1785#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1786pub enum VecStringOrBool {
1787 VecString(Vec<String>),
1788 Bool(bool),
1789}
1790
1791impl<'de> de::Deserialize<'de> for VecStringOrBool {
1792 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1793 where
1794 D: de::Deserializer<'de>,
1795 {
1796 UntaggedEnumVisitor::new()
1797 .expecting("a boolean or vector of strings")
1798 .bool(|value| Ok(VecStringOrBool::Bool(value)))
1799 .seq(|value| value.deserialize().map(VecStringOrBool::VecString))
1800 .deserialize(deserializer)
1801 }
1802}
1803
1804#[derive(Clone, PartialEq, Eq)]
1805pub struct PathValue(pub PathBuf);
1806
1807impl fmt::Debug for PathValue {
1808 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1809 self.0.fmt(f)
1810 }
1811}
1812
1813impl ser::Serialize for PathValue {
1814 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1815 where
1816 S: ser::Serializer,
1817 {
1818 self.0.serialize(serializer)
1819 }
1820}
1821
1822impl<'de> de::Deserialize<'de> for PathValue {
1823 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1824 where
1825 D: de::Deserializer<'de>,
1826 {
1827 Ok(PathValue(String::deserialize(deserializer)?.into()))
1828 }
1829}
1830
1831#[derive(Debug, thiserror::Error)]
1833#[error("manifest field was not resolved")]
1834#[non_exhaustive]
1835#[cfg_attr(feature = "unstable-schema", derive(schemars::JsonSchema))]
1836pub struct UnresolvedError;
1837
1838#[cfg(feature = "unstable-schema")]
1839#[test]
1840fn dump_manifest_schema() {
1841 let schema = schemars::schema_for!(crate::manifest::TomlManifest);
1842 let dump = serde_json::to_string_pretty(&schema).unwrap();
1843 snapbox::assert_data_eq!(dump, snapbox::file!("../../manifest.schema.json").raw());
1844}