1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use crate::{manifest::PackageManifestFile, pkg::PinnedId, source};
use serde::{Deserialize, Serialize};
use std::{
fmt,
path::{Path, PathBuf},
str::FromStr,
};
pub type Source = PathBuf;
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub struct Pinned {
pub path_root: PinnedId,
}
#[derive(Clone, Debug)]
pub struct SourcePathPinnedParseError;
impl Pinned {
pub const PREFIX: &'static str = "path";
}
impl fmt::Display for Pinned {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}+from-root-{}", Self::PREFIX, self.path_root)
}
}
impl FromStr for Pinned {
type Err = SourcePathPinnedParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.trim();
let prefix_plus = format!("{}+", Self::PREFIX);
if s.find(&prefix_plus) != Some(0) {
return Err(SourcePathPinnedParseError);
}
let s = &s[prefix_plus.len()..];
let path_root = s
.split("from-root-")
.nth(1)
.ok_or(SourcePathPinnedParseError)?
.parse()
.map_err(|_| SourcePathPinnedParseError)?;
Ok(Self { path_root })
}
}
impl source::Pin for Source {
type Pinned = Pinned;
fn pin(&self, ctx: source::PinCtx) -> anyhow::Result<(Self::Pinned, PathBuf)> {
let path_root = ctx.path_root();
let pinned = Pinned { path_root };
Ok((pinned, self.clone()))
}
}
impl source::Fetch for Pinned {
fn fetch(&self, _ctx: source::PinCtx, local: &Path) -> anyhow::Result<PackageManifestFile> {
let manifest = PackageManifestFile::from_dir(local)?;
Ok(manifest)
}
}
impl source::DepPath for Pinned {
fn dep_path(&self, _name: &str) -> anyhow::Result<source::DependencyPath> {
Ok(source::DependencyPath::Root(self.path_root))
}
}
impl From<Pinned> for source::Pinned {
fn from(p: Pinned) -> Self {
Self::Path(p)
}
}