forc_pkg/source/
path.rs

1use crate::manifest::GenericManifestFile;
2use crate::{manifest::PackageManifestFile, pkg::PinnedId, source};
3use serde::{Deserialize, Serialize};
4use std::{
5    fmt,
6    path::{Path, PathBuf},
7    str::FromStr,
8};
9
10/// A path to a directory with a `Forc.toml` manifest at its root.
11pub type Source = PathBuf;
12
13/// A pinned instance of a path source.
14#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
15pub struct Pinned {
16    /// The ID of the package that is the root of the subgraph of path dependencies that this
17    /// package is a part of.
18    ///
19    /// In other words, when traversing the parents of this package, this is the ID of the first
20    /// non-path ancestor package.
21    ///
22    /// As a result, this will always be either a git package or the root package.
23    ///
24    /// This allows for disambiguating path dependencies of the same name that have different path
25    /// roots.
26    pub path_root: PinnedId,
27}
28
29/// Error returned upon failed parsing of `SourcePathPinned::from_str`.
30#[derive(Clone, Debug)]
31pub struct SourcePathPinnedParseError;
32
33impl Pinned {
34    pub const PREFIX: &'static str = "path";
35}
36
37impl fmt::Display for Pinned {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        // path+from-root-<id>
40        write!(f, "{}+from-root-{}", Self::PREFIX, self.path_root)
41    }
42}
43
44impl FromStr for Pinned {
45    type Err = SourcePathPinnedParseError;
46    fn from_str(s: &str) -> Result<Self, Self::Err> {
47        // path+from-root-<id>
48        let s = s.trim();
49
50        // Check for prefix at the start.
51        let prefix_plus = format!("{}+", Self::PREFIX);
52        if s.find(&prefix_plus) != Some(0) {
53            return Err(SourcePathPinnedParseError);
54        }
55        let s = &s[prefix_plus.len()..];
56
57        // Parse the `from-root-*` section.
58        let path_root = s
59            .split("from-root-")
60            .nth(1)
61            .ok_or(SourcePathPinnedParseError)?
62            .parse()
63            .map_err(|_| SourcePathPinnedParseError)?;
64
65        Ok(Self { path_root })
66    }
67}
68
69impl source::Pin for Source {
70    type Pinned = Pinned;
71    fn pin(&self, ctx: source::PinCtx) -> anyhow::Result<(Self::Pinned, PathBuf)> {
72        let path_root = ctx.path_root();
73        let pinned = Pinned { path_root };
74        Ok((pinned, self.clone()))
75    }
76}
77
78impl source::Fetch for Pinned {
79    fn fetch(&self, _ctx: source::PinCtx, local: &Path) -> anyhow::Result<PackageManifestFile> {
80        let manifest = PackageManifestFile::from_dir(local)?;
81        Ok(manifest)
82    }
83}
84
85impl source::DepPath for Pinned {
86    fn dep_path(&self, _name: &str) -> anyhow::Result<source::DependencyPath> {
87        Ok(source::DependencyPath::Root(self.path_root))
88    }
89}
90
91impl From<Pinned> for source::Pinned {
92    fn from(p: Pinned) -> Self {
93        Self::Path(p)
94    }
95}