cargo_clone_core/
source.rs

1// Copyright 2015 Jan Likar.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use cargo::util::GlobalContext;
10use cargo::{CargoResult, core::SourceId, util::IntoUrl};
11use url::Url;
12
13/// Where to clone the crate from.
14#[derive(Debug, Default)]
15pub struct ClonerSource {
16    pub(crate) cargo_source: CargoSource,
17}
18
19#[derive(Debug, Default)]
20pub(crate) enum CargoSource {
21    #[default]
22    CratesIo,
23    Index(Url),
24    LocalRegistry(String),
25    Registry(String),
26}
27
28impl ClonerSource {
29    /// Creates a [`ClonerSource`] from the name of the remote registry.
30    pub fn registry(key: impl Into<String>) -> Self {
31        Self {
32            cargo_source: CargoSource::Registry(key.into()),
33        }
34    }
35
36    /// Creates a [`ClonerSource`] from a local registry path.
37    pub fn local_registry(path: impl Into<String>) -> Self {
38        Self {
39            cargo_source: CargoSource::LocalRegistry(path.into()),
40        }
41    }
42
43    /// Creates a [`ClonerSource`] from a remote registry URL.
44    pub fn index(index: impl AsRef<str>) -> CargoResult<Self> {
45        let index: &str = index.as_ref();
46        let cargo_source = CargoSource::Index(index.into_url()?);
47        Ok(Self { cargo_source })
48    }
49
50    /// Creates a [`ClonerSource`] from a remote registry URL.
51    pub fn index_from_url(url: Url) -> Self {
52        let cargo_source = CargoSource::Index(url);
53        Self { cargo_source }
54    }
55
56    /// Creates a [`ClonerSource`] from [crates.io](https://crates.io/).
57    pub fn crates_io() -> Self {
58        Self {
59            cargo_source: CargoSource::CratesIo,
60        }
61    }
62}
63
64impl CargoSource {
65    pub(crate) fn to_source_id(&self, context: &GlobalContext) -> CargoResult<SourceId> {
66        match self {
67            CargoSource::CratesIo => SourceId::crates_io(context),
68            CargoSource::Index(url) => SourceId::for_registry(url),
69            CargoSource::LocalRegistry(path) => {
70                SourceId::for_local_registry(&context.cwd().join(path))
71            }
72            CargoSource::Registry(key) => SourceId::alt_registry(context, key),
73        }
74    }
75}