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
10pub type Source = PathBuf;
12
13#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
15pub struct Pinned {
16 pub path_root: PinnedId,
27}
28
29#[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 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 let s = s.trim();
49
50 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 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}