cargo_metadata/
dependency.rs

1//! This module contains `Dependency` and the types/functions it uses for deserialization.
2
3use std::fmt;
4
5use camino::Utf8PathBuf;
6#[cfg(feature = "builder")]
7use derive_builder::Builder;
8use semver::VersionReq;
9use serde::{Deserialize, Deserializer, Serialize};
10
11#[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
16    Normal,
17    #[serde(rename = "dev")]
18    /// Those used in tests only
19    Development,
20    #[serde(rename = "build")]
21    /// Those used in build scripts only
22    Build,
23    #[doc(hidden)]
24    #[serde(other)]
25    Unknown,
26}
27
28impl Default for DependencyKind {
29    fn default() -> DependencyKind {
30        DependencyKind::Normal
31    }
32}
33
34impl fmt::Display for DependencyKind {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        let s = serde_json::to_string(self).unwrap();
37        // skip opening and closing quotes
38        f.write_str(&s[1..s.len() - 1])
39    }
40}
41
42/// 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
45    D: Deserializer<'de>,
46{
47    Deserialize::deserialize(d).map(|x: Option<_>| x.unwrap_or_default())
48}
49
50#[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`
57    pub name: String,
58    /// The source of dependency
59    pub source: Option<String>,
60    /// The required version
61    pub req: VersionReq,
62    /// The kind of dependency this is
63    #[serde(deserialize_with = "parse_dependency_kind")]
64    pub kind: DependencyKind,
65    /// Whether this dependency is required or optional
66    pub optional: bool,
67    /// Whether the default features in this dependency are used.
68    pub uses_default_features: bool,
69    /// The list of features enabled for this dependency.
70    pub 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
76    pub 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.
79    pub 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.
83    pub registry: Option<String>,
84    /// The file system path for a local path dependency.
85    ///
86    /// Only produced on cargo 1.51+
87    pub path: Option<Utf8PathBuf>,
88}
89
90pub use cargo_platform::Platform;