1//! This module contains `Dependency` and the types/functions it uses for deserialization.
23use std::fmt;
45use camino::Utf8PathBuf;
6#[cfg(feature = "builder")]
7use derive_builder::Builder;
8use semver::VersionReq;
9use serde::{Deserialize, Deserializer, Serialize};
1011#[derive(Eq, PartialEq, Clone, Debug, Copy, Hash, Serialize, Deserialize)]
12/// Dependencies can come in three kinds
13pub enum DependencyKind {
14#[serde(rename = "normal")]
15/// The 'normal' kind
16Normal,
17#[serde(rename = "dev")]
18/// Those used in tests only
19Development,
20#[serde(rename = "build")]
21/// Those used in build scripts only
22Build,
23#[doc(hidden)]
24 #[serde(other)]
25Unknown,
26}
2728impl Default for DependencyKind {
29fn default() -> DependencyKind {
30 DependencyKind::Normal
31 }
32}
3334impl fmt::Display for DependencyKind {
35fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36let s = serde_json::to_string(self).unwrap();
37// skip opening and closing quotes
38f.write_str(&s[1..s.len() - 1])
39 }
40}
4142/// The `kind` can be `null`, which is interpreted as the default - `Normal`.
43pub(super) fn parse_dependency_kind<'de, D>(d: D) -> Result<DependencyKind, D::Error>
44where
45D: Deserializer<'de>,
46{
47 Deserialize::deserialize(d).map(|x: Option<_>| x.unwrap_or_default())
48}
4950#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
51#[cfg_attr(feature = "builder", derive(Builder))]
52#[non_exhaustive]
53#[cfg_attr(feature = "builder", builder(pattern = "owned", setter(into)))]
54/// A dependency of the main crate
55pub struct Dependency {
56/// Name as given in the `Cargo.toml`
57pub name: String,
58/// The source of dependency
59pub source: Option<String>,
60/// The required version
61pub req: VersionReq,
62/// The kind of dependency this is
63#[serde(deserialize_with = "parse_dependency_kind")]
64pub kind: DependencyKind,
65/// Whether this dependency is required or optional
66pub optional: bool,
67/// Whether the default features in this dependency are used.
68pub uses_default_features: bool,
69/// The list of features enabled for this dependency.
70pub features: Vec<String>,
71/// The target this dependency is specific to.
72 ///
73 /// Use the [`Display`] trait to access the contents.
74 ///
75 /// [`Display`]: std::fmt::Display
76pub target: Option<Platform>,
77/// If the dependency is renamed, this is the new name for the dependency
78 /// as a string. None if it is not renamed.
79pub rename: Option<String>,
80/// The URL of the index of the registry where this dependency is from.
81 ///
82 /// If None, the dependency is from crates.io.
83pub registry: Option<String>,
84/// The file system path for a local path dependency.
85 ///
86 /// Only produced on cargo 1.51+
87pub path: Option<Utf8PathBuf>,
88}
8990pub use cargo_platform::Platform;