| 1 | //! This module contains `Dependency` and the types/functions it uses for deserialization. | 
| 2 |  | 
|---|
| 3 | use std::fmt; | 
|---|
| 4 |  | 
|---|
| 5 | use camino::Utf8PathBuf; | 
|---|
| 6 | #[ cfg(feature = "builder")] | 
|---|
| 7 | use derive_builder::Builder; | 
|---|
| 8 | use semver::VersionReq; | 
|---|
| 9 | use serde::{Deserialize, Deserializer, Serialize}; | 
|---|
| 10 |  | 
|---|
| 11 | #[ derive(Eq, PartialEq, Clone, Debug, Copy, Hash, Serialize, Deserialize)] | 
|---|
| 12 | /// Dependencies can come in three kinds | 
|---|
| 13 | pub 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 |  | 
|---|
| 28 | impl Default for DependencyKind { | 
|---|
| 29 | fn default() -> DependencyKind { | 
|---|
| 30 | DependencyKind::Normal | 
|---|
| 31 | } | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | impl fmt::Display for DependencyKind { | 
|---|
| 35 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 
|---|
| 36 | let s: String = 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`. | 
|---|
| 43 | pub(super) fn parse_dependency_kind<'de, D>(d: D) -> Result<DependencyKind, D::Error> | 
|---|
| 44 | where | 
|---|
| 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 | 
|---|
| 55 | pub 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 |  | 
|---|
| 90 | pub use cargo_platform::Platform; | 
|---|
| 91 |  | 
|---|