Skip to main content

shared_target_core/
error.rs

1use std::path::PathBuf;
2
3#[derive(Debug, thiserror::Error)]
4pub enum Error {
5    #[error("no target directory at {0}")]
6    SourceMissing(PathBuf),
7
8    #[error("{0} already exists")]
9    DestinationExists(PathBuf),
10
11    #[error("a previous run left a partial tree at {0}; remove it and run again")]
12    StagingExists(PathBuf),
13
14    /// Reading a target directory a build is writing to yields a tree whose
15    /// fingerprints and artifacts were captured at different moments — and
16    /// Cargo reads that tree as fresh. Refused while the build holds its lock.
17    #[error("a build is running here: {path} is locked; seed between builds")]
18    SourceBusy { path: PathBuf },
19
20    #[error("{0} has no parent directory to be created in")]
21    DestinationHasNoParent(PathBuf),
22
23    /// Walking the source while writing into it feeds the walk its own output,
24    /// and what that fills is the disk. Refused rather than detected partway.
25    #[error("{dest} is inside {src}, which is the directory being read")]
26    DestinationInsideSource { src: PathBuf, dest: PathBuf },
27
28    /// Neither a block clone nor a hard link crosses a filesystem boundary, and
29    /// the copy that would remain is the multi-gigabyte one this tool exists to
30    /// avoid. Said rather than silently done.
31    #[error(
32        "{src} and {dest} are on different filesystems, so there is nothing to share between them"
33    )]
34    CrossDevice { src: PathBuf, dest: PathBuf },
35
36    #[error("{op} {path}")]
37    Io {
38        op: &'static str,
39        path: PathBuf,
40        #[source]
41        source: std::io::Error,
42    },
43
44    #[error("walking {path}")]
45    Walk {
46        path: PathBuf,
47        #[source]
48        source: walkdir::Error,
49    },
50
51    #[error("running `cargo metadata`")]
52    MetadataSpawn(#[source] std::io::Error),
53
54    #[error("`cargo metadata` failed: {0}")]
55    MetadataFailed(String),
56
57    #[error("reading the output of `cargo metadata`")]
58    MetadataParse(#[source] serde_json::Error),
59
60    #[error("`cargo metadata` reported no target directory")]
61    MetadataNoTargetDir,
62}
63
64pub type Result<T> = std::result::Result<T, Error>;
65
66impl Error {
67    /// Builds an [`Error::Io`] closure for `map_err`, so the path and the verb
68    /// travel with the failure instead of being lost at the call site.
69    pub(crate) fn io(
70        op: &'static str,
71        path: impl Into<PathBuf>,
72    ) -> impl FnOnce(std::io::Error) -> Error {
73        let path = path.into();
74        move |source| Error::Io { op, path, source }
75    }
76}