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