1use cargo_platform::Platform;
2use semver::VersionReq;
3use serde::Serialize;
4use serde::ser;
5use std::borrow::Cow;
6use std::fmt;
7use std::fmt::{Display, Formatter};
8use std::path::{Path, PathBuf};
9use std::sync::Arc;
10use tracing::trace;
11
12use crate::core::compiler::{CompileKind, CompileTarget};
13use crate::core::{CliUnstable, Feature, Features, PackageId, SourceId, Summary};
14use crate::util::OptVersionReq;
15use crate::util::context::Definition;
16use crate::util::errors::CargoResult;
17use crate::util::interning::InternedString;
18
19#[derive(PartialEq, Eq, Hash, Clone, Debug)]
22pub struct Dependency {
23 inner: Arc<Inner>,
24}
25
26#[derive(PartialEq, Eq, Hash, Clone, Debug)]
28struct Inner {
29 name: InternedString,
30 source_id: SourceId,
31 registry_id: Option<SourceId>,
38 req: OptVersionReq,
39 specified_req: bool,
40 kind: DepKind,
41 only_match_name: bool,
42 explicit_name_in_toml: Option<InternedString>,
43
44 optional: bool,
45 public: bool,
46 default_features: bool,
47 features: Vec<InternedString>,
48 artifact: Option<Artifact>,
50
51 platform: Option<Platform>,
54}
55
56#[derive(Serialize)]
57pub struct SerializedDependency {
58 name: InternedString,
59 source: SourceId,
60 req: String,
61 kind: DepKind,
62 rename: Option<InternedString>,
63
64 optional: bool,
65 uses_default_features: bool,
66 features: Vec<InternedString>,
67 #[serde(skip_serializing_if = "Option::is_none")]
68 artifact: Option<Artifact>,
69 target: Option<Platform>,
70 registry: Option<String>,
73
74 #[serde(skip_serializing_if = "Option::is_none")]
76 path: Option<PathBuf>,
77
78 #[serde(skip_serializing_if = "Option::is_none")]
82 public: Option<bool>,
83}
84
85#[derive(PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Debug, Copy)]
86pub enum DepKind {
87 Normal,
88 Development,
89 Build,
90}
91
92impl DepKind {
93 pub fn kind_table(&self) -> &'static str {
94 match self {
95 DepKind::Normal => "dependencies",
96 DepKind::Development => "dev-dependencies",
97 DepKind::Build => "build-dependencies",
98 }
99 }
100}
101
102impl ser::Serialize for DepKind {
103 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
104 where
105 S: ser::Serializer,
106 {
107 match *self {
108 DepKind::Normal => None,
109 DepKind::Development => Some("dev"),
110 DepKind::Build => Some("build"),
111 }
112 .serialize(s)
113 }
114}
115
116impl Dependency {
117 pub fn parse(
119 name: impl Into<InternedString>,
120 version: Option<&str>,
121 source_id: SourceId,
122 ) -> CargoResult<Dependency> {
123 let name = name.into();
124 let (specified_req, version_req) = match version {
125 Some(v) => match VersionReq::parse(v) {
126 Ok(req) => (true, OptVersionReq::Req(req)),
127 Err(err) => {
128 return Err(anyhow::Error::new(err).context(format!(
129 "failed to parse the version requirement `{}` for dependency `{}`",
130 v, name,
131 )));
132 }
133 },
134 None => (false, OptVersionReq::Any),
135 };
136
137 let mut ret = Dependency::new_override(name, source_id);
138 {
139 let ptr = Arc::make_mut(&mut ret.inner);
140 ptr.only_match_name = false;
141 ptr.req = version_req;
142 ptr.specified_req = specified_req;
143 }
144 Ok(ret)
145 }
146
147 pub fn new_override(name: InternedString, source_id: SourceId) -> Dependency {
148 assert!(!name.is_empty());
149 Dependency {
150 inner: Arc::new(Inner {
151 name,
152 source_id,
153 registry_id: None,
154 req: OptVersionReq::Any,
155 kind: DepKind::Normal,
156 only_match_name: true,
157 optional: false,
158 public: false,
159 features: Vec::new(),
160 default_features: true,
161 specified_req: false,
162 platform: None,
163 explicit_name_in_toml: None,
164 artifact: None,
165 }),
166 }
167 }
168
169 pub fn serialized(
170 &self,
171 unstable_flags: &CliUnstable,
172 features: &Features,
173 ) -> SerializedDependency {
174 SerializedDependency {
175 name: self.package_name(),
176 source: self.source_id(),
177 req: self.version_req().to_string(),
178 kind: self.kind(),
179 optional: self.is_optional(),
180 uses_default_features: self.uses_default_features(),
181 features: self.features().to_vec(),
182 target: self.inner.platform.clone(),
183 rename: self.explicit_name_in_toml(),
184 registry: self.registry_id().as_ref().map(|sid| sid.url().to_string()),
185 path: self.source_id().local_path(),
186 artifact: self.inner.artifact.clone(),
187 public: if unstable_flags.public_dependency
188 || features.is_enabled(Feature::public_dependency())
189 {
190 Some(self.inner.public)
191 } else {
192 None
193 },
194 }
195 }
196
197 pub fn version_req(&self) -> &OptVersionReq {
198 &self.inner.req
199 }
200
201 pub fn name_in_toml(&self) -> InternedString {
222 self.explicit_name_in_toml().unwrap_or(self.inner.name)
223 }
224
225 pub fn package_name(&self) -> InternedString {
244 self.inner.name
245 }
246
247 pub fn source_id(&self) -> SourceId {
248 self.inner.source_id
249 }
250
251 pub fn registry_id(&self) -> Option<SourceId> {
252 self.inner.registry_id
253 }
254
255 pub fn set_registry_id(&mut self, registry_id: SourceId) -> &mut Dependency {
256 Arc::make_mut(&mut self.inner).registry_id = Some(registry_id);
257 self
258 }
259
260 pub fn kind(&self) -> DepKind {
261 self.inner.kind
262 }
263
264 pub fn is_public(&self) -> bool {
265 self.inner.public
266 }
267
268 pub fn set_public(&mut self, public: bool) -> &mut Dependency {
270 if public {
271 assert_eq!(self.kind(), DepKind::Normal);
273 }
274 Arc::make_mut(&mut self.inner).public = public;
275 self
276 }
277
278 pub fn specified_req(&self) -> bool {
279 self.inner.specified_req
280 }
281
282 pub fn platform(&self) -> Option<&Platform> {
285 self.inner.platform.as_ref()
286 }
287
288 pub fn explicit_name_in_toml(&self) -> Option<InternedString> {
293 self.inner.explicit_name_in_toml
294 }
295
296 pub fn set_kind(&mut self, kind: DepKind) -> &mut Dependency {
297 if self.is_public() {
298 assert_eq!(kind, DepKind::Normal);
300 }
301 Arc::make_mut(&mut self.inner).kind = kind;
302 self
303 }
304
305 pub fn set_features(
307 &mut self,
308 features: impl IntoIterator<Item = impl Into<InternedString>>,
309 ) -> &mut Dependency {
310 Arc::make_mut(&mut self.inner).features = features.into_iter().map(|s| s.into()).collect();
311 self
312 }
313
314 pub fn set_default_features(&mut self, default_features: bool) -> &mut Dependency {
316 Arc::make_mut(&mut self.inner).default_features = default_features;
317 self
318 }
319
320 pub fn set_optional(&mut self, optional: bool) -> &mut Dependency {
322 Arc::make_mut(&mut self.inner).optional = optional;
323 self
324 }
325
326 pub fn set_source_id(&mut self, id: SourceId) -> &mut Dependency {
328 Arc::make_mut(&mut self.inner).source_id = id;
329 self
330 }
331
332 pub fn set_version_req(&mut self, req: OptVersionReq) -> &mut Dependency {
334 Arc::make_mut(&mut self.inner).req = req;
335 self
336 }
337
338 pub fn set_platform(&mut self, platform: Option<Platform>) -> &mut Dependency {
339 Arc::make_mut(&mut self.inner).platform = platform;
340 self
341 }
342
343 pub fn set_explicit_name_in_toml(
344 &mut self,
345 name: impl Into<InternedString>,
346 ) -> &mut Dependency {
347 Arc::make_mut(&mut self.inner).explicit_name_in_toml = Some(name.into());
348 self
349 }
350
351 pub fn lock_to(&mut self, id: PackageId) -> &mut Dependency {
353 assert_eq!(self.inner.source_id, id.source_id());
354 trace!(
355 "locking dep from `{}` with `{}` at {} to {}",
356 self.package_name(),
357 self.version_req(),
358 self.source_id(),
359 id
360 );
361 let me = Arc::make_mut(&mut self.inner);
362 me.req.lock_to(id.version());
363
364 me.source_id = me.source_id.with_precise_from(id.source_id());
368 self
369 }
370
371 pub fn lock_version(&mut self, version: &semver::Version) -> &mut Dependency {
376 let me = Arc::make_mut(&mut self.inner);
377 me.req.lock_to(version);
378 self
379 }
380
381 pub fn is_locked(&self) -> bool {
384 self.inner.req.is_locked()
385 }
386
387 pub fn is_transitive(&self) -> bool {
389 match self.inner.kind {
390 DepKind::Normal | DepKind::Build => true,
391 DepKind::Development => false,
392 }
393 }
394
395 pub fn is_build(&self) -> bool {
396 matches!(self.inner.kind, DepKind::Build)
397 }
398
399 pub fn is_optional(&self) -> bool {
400 self.inner.optional
401 }
402
403 pub fn uses_default_features(&self) -> bool {
405 self.inner.default_features
406 }
407 pub fn features(&self) -> &[InternedString] {
409 &self.inner.features
410 }
411
412 pub fn matches(&self, sum: &Summary) -> bool {
414 self.matches_id(sum.package_id())
415 }
416
417 pub fn matches_prerelease(&self, sum: &Summary) -> bool {
418 let id = sum.package_id();
419 self.inner.name == id.name()
420 && (self.inner.only_match_name
421 || (self.inner.req.matches_prerelease(id.version())
422 && self.inner.source_id == id.source_id()))
423 }
424
425 pub fn matches_ignoring_source(&self, id: PackageId) -> bool {
427 self.package_name() == id.name() && self.version_req().matches(id.version())
428 }
429
430 pub fn matches_id(&self, id: PackageId) -> bool {
432 self.inner.name == id.name()
433 && (self.inner.only_match_name
434 || (self.inner.req.matches(id.version()) && self.inner.source_id == id.source_id()))
435 }
436
437 pub fn map_source(mut self, to_replace: SourceId, replace_with: SourceId) -> Dependency {
438 if self.source_id() == to_replace {
439 self.set_source_id(replace_with);
440 }
441 self
442 }
443
444 pub(crate) fn set_artifact(&mut self, artifact: Artifact) {
445 Arc::make_mut(&mut self.inner).artifact = Some(artifact);
446 }
447
448 pub fn artifact(&self) -> Option<&Artifact> {
449 self.inner.artifact.as_ref()
450 }
451
452 pub(crate) fn maybe_lib(&self) -> bool {
456 self.artifact().map(|a| a.is_lib).unwrap_or(true)
457 }
458}
459
460#[derive(PartialEq, Eq, Hash, Clone, Debug)]
467pub struct Artifact {
468 inner: Arc<Vec<ArtifactKind>>,
469 is_lib: bool,
470 target: Option<ArtifactTarget>,
471}
472
473#[derive(Serialize)]
474pub struct SerializedArtifact<'a> {
475 kinds: &'a [ArtifactKind],
476 lib: bool,
477 target: Option<&'a str>,
478}
479
480impl ser::Serialize for Artifact {
481 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
482 where
483 S: ser::Serializer,
484 {
485 SerializedArtifact {
486 kinds: self.kinds(),
487 lib: self.is_lib,
488 target: self.target.as_ref().map(ArtifactTarget::as_str),
489 }
490 .serialize(s)
491 }
492}
493
494impl Artifact {
495 pub(crate) fn parse(
496 artifacts: &[impl AsRef<str>],
497 is_lib: bool,
498 target: Option<&str>,
499 unstable_json: bool,
500 ) -> CargoResult<Self> {
501 let kinds = ArtifactKind::validate(
502 artifacts
503 .iter()
504 .map(|s| ArtifactKind::parse(s.as_ref()))
505 .collect::<Result<Vec<_>, _>>()?,
506 )?;
507 Ok(Artifact {
508 inner: Arc::new(kinds),
509 is_lib,
510 target: target
511 .map(|name| ArtifactTarget::parse(name, unstable_json))
512 .transpose()?,
513 })
514 }
515
516 pub fn kinds(&self) -> &[ArtifactKind] {
517 &self.inner
518 }
519
520 pub fn is_lib(&self) -> bool {
521 self.is_lib
522 }
523
524 pub fn target(&self) -> Option<ArtifactTarget> {
525 self.target
526 }
527}
528
529#[derive(PartialEq, Eq, Hash, Copy, Clone, Ord, PartialOrd, Debug)]
530pub enum ArtifactTarget {
531 BuildDependencyAssumeTarget,
535 Force(CompileTarget),
539}
540
541impl ArtifactTarget {
542 pub fn parse(target: &str, unstable_json: bool) -> CargoResult<ArtifactTarget> {
543 Ok(match target {
544 "target" => ArtifactTarget::BuildDependencyAssumeTarget,
545 name => ArtifactTarget::Force(CompileTarget::new(name, unstable_json)?),
546 })
547 }
548
549 pub fn as_str(&self) -> &str {
550 match self {
551 ArtifactTarget::BuildDependencyAssumeTarget => "target",
552 ArtifactTarget::Force(target) => target.rustc_target().as_str(),
553 }
554 }
555
556 pub fn to_compile_kind(&self) -> Option<CompileKind> {
557 self.to_compile_target().map(CompileKind::Target)
558 }
559
560 pub fn to_compile_target(&self) -> Option<CompileTarget> {
561 match self {
562 ArtifactTarget::BuildDependencyAssumeTarget => None,
563 ArtifactTarget::Force(target) => Some(*target),
564 }
565 }
566
567 pub(crate) fn to_resolved_compile_kind(
568 &self,
569 root_unit_compile_kind: CompileKind,
570 ) -> CompileKind {
571 match self {
572 ArtifactTarget::Force(target) => CompileKind::Target(*target),
573 ArtifactTarget::BuildDependencyAssumeTarget => root_unit_compile_kind,
574 }
575 }
576
577 pub(crate) fn to_resolved_compile_target(
578 &self,
579 root_unit_compile_kind: CompileKind,
580 ) -> Option<CompileTarget> {
581 match self.to_resolved_compile_kind(root_unit_compile_kind) {
582 CompileKind::Host => None,
583 CompileKind::Target(target) => Some(target),
584 }
585 }
586}
587
588#[derive(PartialEq, Eq, Hash, Copy, Clone, Ord, PartialOrd, Debug)]
589pub enum ArtifactKind {
590 AllBinaries,
592 SelectedBinary(InternedString),
594 Cdylib,
595 Staticlib,
596}
597
598impl ser::Serialize for ArtifactKind {
599 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
600 where
601 S: ser::Serializer,
602 {
603 self.as_str().serialize(s)
604 }
605}
606
607impl fmt::Display for ArtifactKind {
608 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
609 f.write_str(&self.as_str())
610 }
611}
612
613impl ArtifactKind {
614 pub fn crate_type(&self) -> &'static str {
618 match self {
619 ArtifactKind::AllBinaries | ArtifactKind::SelectedBinary(_) => "bin",
620 ArtifactKind::Cdylib => "cdylib",
621 ArtifactKind::Staticlib => "staticlib",
622 }
623 }
624
625 pub fn as_str(&self) -> Cow<'static, str> {
626 match *self {
627 ArtifactKind::SelectedBinary(name) => format!("bin:{}", name.as_str()).into(),
628 _ => self.crate_type().into(),
629 }
630 }
631
632 pub fn parse(kind: &str) -> CargoResult<Self> {
633 Ok(match kind {
634 "bin" => ArtifactKind::AllBinaries,
635 "cdylib" => ArtifactKind::Cdylib,
636 "staticlib" => ArtifactKind::Staticlib,
637 _ => {
638 return kind
639 .strip_prefix("bin:")
640 .map(|bin_name| ArtifactKind::SelectedBinary(bin_name.into()))
641 .ok_or_else(|| {
642 anyhow::anyhow!("'{}' is not a valid artifact specifier", kind)
643 });
644 }
645 })
646 }
647
648 fn validate(kinds: Vec<ArtifactKind>) -> CargoResult<Vec<ArtifactKind>> {
649 if kinds.iter().any(|k| matches!(k, ArtifactKind::AllBinaries))
650 && kinds
651 .iter()
652 .any(|k| matches!(k, ArtifactKind::SelectedBinary(_)))
653 {
654 anyhow::bail!(
655 "Cannot specify both 'bin' and 'bin:<name>' binary artifacts, as 'bin' selects all available binaries."
656 );
657 }
658 let mut kinds_without_dupes = kinds.clone();
659 kinds_without_dupes.sort();
660 kinds_without_dupes.dedup();
661 let num_dupes = kinds.len() - kinds_without_dupes.len();
662 if num_dupes != 0 {
663 anyhow::bail!(
664 "Found {} duplicate binary artifact{}",
665 num_dupes,
666 (num_dupes > 1).then(|| "s").unwrap_or("")
667 );
668 }
669 Ok(kinds)
670 }
671}
672
673#[derive(Clone, Debug)]
676pub struct Patch {
677 pub dep: Dependency,
678 pub loc: PatchLocation,
679}
680
681#[derive(Clone, Debug)]
683pub enum PatchLocation {
684 Manifest(PathBuf),
686 Config(Definition),
688}
689
690impl Display for PatchLocation {
691 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
692 match self {
693 PatchLocation::Manifest(p) => Path::display(p).fmt(f),
694 PatchLocation::Config(def) => def.fmt(f),
695 }
696 }
697}