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
use glob::{GlobError, PatternError};
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::io::Error as IoError;
use std::path::PathBuf;
use toml::de::Error as TomlError;

#[derive(Debug)]
pub enum Error {
    InvalidArgs,
    ManifestNotAWorkspace,
    ManifestNotFound,
    RustcNotFound,
    ManifestPathNotFound,
    GlobPatternError(&'static str),
    Glob(GlobError),
    UnexpectedWorkspace(PathBuf),
    NoPackageInManifest(PathBuf),
    PackageNotFound(PathBuf, String),
    ManifestNotInWorkspace {
        manifest: PathBuf,
        workspace_manifest: PathBuf,
    },
    Io(PathBuf, IoError),
    Toml(PathBuf, TomlError),
}

pub type Result<T, E = Error> = std::result::Result<T, E>;

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.write_str(match self {
            Self::InvalidArgs => "Invalid args.",
            Self::ManifestNotAWorkspace => {
                "The provided Cargo.toml does not contain a `[workspace]`"
            }
            Self::ManifestNotFound => "Didn't find Cargo.toml.",
            Self::ManifestPathNotFound => "The manifest-path must be a path to a Cargo.toml file",
            Self::RustcNotFound => "Didn't find rustc.",
            Self::GlobPatternError(error) => error,
            Self::Glob(error) => return error.fmt(f),
            Self::UnexpectedWorkspace(path) => {
                return write!(f, "Did not expect a `[workspace]` at `{}`", path.display())
            }
            Self::NoPackageInManifest(manifest) => {
                return write!(
                    f,
                    "Failed to parse manifest at `{}`: virtual manifests must be configured with `[workspace]`",
                    manifest.display()
                )
            }
            Self::PackageNotFound(workspace, name) => {
                return write!(
                    f,
                    "package `{}` not found in workspace `{}`",
                    name,
                    workspace.display()
                )
            }
            Self::ManifestNotInWorkspace {
                manifest,
                workspace_manifest,
            } => {
                return write!(f, "current package believes it's in a workspace when it's not:
current:   {}
workspace: {workspace_manifest_path}

this may be fixable by adding `{package_subpath}` to the `workspace.members` array of the manifest located at: {workspace_manifest_path}
Alternatively, to keep it out of the workspace, add an empty `[workspace]` table to the package's manifest.",
                    // TODO: Parse workspace.exclude and add back "add the package to the `workspace.exclude` array, or"
                    manifest.display(),
                    package_subpath = manifest.parent().unwrap().strip_prefix(workspace_manifest.parent().unwrap()).unwrap().display(),
                    workspace_manifest_path = workspace_manifest.display(),
                )
            },
            Self::Io(path, error) => return write!(f, "{}: {}", path.display(), error),
            Self::Toml(file, error) => return write!(f, "{}: {}", file.display(), error),
        })
    }
}

impl std::error::Error for Error {}

impl From<PatternError> for Error {
    fn from(error: PatternError) -> Self {
        Self::GlobPatternError(error.msg)
    }
}

impl From<GlobError> for Error {
    fn from(error: GlobError) -> Self {
        Self::Glob(error)
    }
}