Skip to content

Commit 651e392

Browse files
authored
manifest: Be more specific about not supporting multiple packages yet (#21)
When running `cargo-subcommand` without `-p` package selection against a workspace currently only the first found package is returned. This should be implemented over time to return all packages (and artifacts adhering to target selection), but return a more specific error for the time being instead of making it seem like there's only one package.
1 parent 5c451c0 commit 651e392

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub enum EnvError {
1717
Var(VarError),
1818
}
1919

20+
pub type Result<T, E = EnvError> = std::result::Result<T, E>;
21+
2022
impl From<VarError> for EnvError {
2123
fn from(var: VarError) -> Self {
2224
Self::Var(var)
@@ -34,8 +36,6 @@ impl Display for EnvError {
3436

3537
impl std::error::Error for EnvError {}
3638

37-
type Result<T, E = EnvError> = std::result::Result<T, E>;
38-
3939
#[derive(Clone, Debug, Deserialize, PartialEq)]
4040
#[serde(rename_all = "kebab-case")]
4141
pub struct Config {

src/error.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use glob::{GlobError, PatternError};
2-
use std::fmt::{Display, Formatter, Result};
2+
use std::fmt::{Display, Formatter, Result as FmtResult};
33
use std::io::Error as IoError;
44
use std::path::PathBuf;
55
use toml::de::Error as TomlError;
@@ -10,18 +10,24 @@ pub enum Error {
1010
ManifestNotFound,
1111
RustcNotFound,
1212
ManifestPathNotFound,
13+
MultiplePackagesNotSupported,
1314
GlobPatternError(&'static str),
1415
Glob(GlobError),
1516
Io(PathBuf, IoError),
1617
Toml(PathBuf, TomlError),
1718
}
1819

20+
pub type Result<T, E = Error> = std::result::Result<T, E>;
21+
1922
impl Display for Error {
20-
fn fmt(&self, f: &mut Formatter) -> Result {
23+
fn fmt(&self, f: &mut Formatter) -> FmtResult {
2124
f.write_str(match self {
2225
Self::InvalidArgs => "Invalid args.",
2326
Self::ManifestNotFound => "Didn't find Cargo.toml.",
2427
Self::ManifestPathNotFound => "The manifest-path must be a path to a Cargo.toml file",
28+
Self::MultiplePackagesNotSupported => {
29+
"Multiple packages are not yet supported (ie. in a `[workspace]`). Use `-p` to select a specific package."
30+
}
2531
Self::RustcNotFound => "Didn't find rustc.",
2632
Self::GlobPatternError(error) => error,
2733
Self::Glob(error) => return error.fmt(f),

src/manifest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::error::Error;
1+
use crate::error::{Error, Result};
22
use serde::Deserialize;
33
use std::path::Path;
44

@@ -9,7 +9,7 @@ pub struct Manifest {
99
}
1010

1111
impl Manifest {
12-
pub fn parse_from_toml(path: &Path) -> Result<Self, Error> {
12+
pub fn parse_from_toml(path: &Path) -> Result<Self> {
1313
let contents = std::fs::read_to_string(path).map_err(|e| Error::Io(path.to_owned(), e))?;
1414
toml::from_str(&contents).map_err(|e| Error::Toml(path.to_owned(), e))
1515
}

src/subcommand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::args::Args;
22
use crate::artifact::{Artifact, CrateType};
3-
use crate::error::Error;
3+
use crate::error::{Error, Result};
44
use crate::profile::Profile;
55
use crate::{utils, LocalizedConfig};
66
use std::ffi::OsStr;
@@ -21,7 +21,7 @@ pub struct Subcommand {
2121
}
2222

2323
impl Subcommand {
24-
pub fn new(args: Args) -> Result<Self, Error> {
24+
pub fn new(args: Args) -> Result<Self> {
2525
// TODO: support multiple packages properly
2626
assert!(
2727
args.package.len() < 2,

src/utils.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::config::Config;
2-
use crate::error::Error;
2+
use crate::error::{Error, Result};
33
use crate::manifest::Manifest;
44
use std::ffi::OsStr;
55
use std::path::{Path, PathBuf};
66

7-
pub fn list_rust_files(dir: &Path) -> Result<Vec<String>, Error> {
7+
pub fn list_rust_files(dir: &Path) -> Result<Vec<String>> {
88
let mut files = Vec::new();
99
if dir.exists() && dir.is_dir() {
1010
let entries = std::fs::read_dir(dir).map_err(|e| Error::Io(dir.to_owned(), e))?;
@@ -19,7 +19,7 @@ pub fn list_rust_files(dir: &Path) -> Result<Vec<String>, Error> {
1919
Ok(files)
2020
}
2121

22-
fn member(manifest: &Path, members: &[String], package: &str) -> Result<Option<PathBuf>, Error> {
22+
fn member(manifest: &Path, members: &[String], package: &str) -> Result<Option<PathBuf>> {
2323
let workspace_dir = manifest.parent().unwrap();
2424
for member in members {
2525
for manifest_dir in glob::glob(workspace_dir.join(member).to_str().unwrap())? {
@@ -35,7 +35,7 @@ fn member(manifest: &Path, members: &[String], package: &str) -> Result<Option<P
3535
Ok(None)
3636
}
3737

38-
pub fn find_package(path: &Path, name: Option<&str>) -> Result<(PathBuf, String), Error> {
38+
pub fn find_package(path: &Path, name: Option<&str>) -> Result<(PathBuf, String)> {
3939
let path = dunce::canonicalize(path).map_err(|e| Error::Io(path.to_owned(), e))?;
4040
for manifest_path in path
4141
.ancestors()
@@ -44,15 +44,17 @@ pub fn find_package(path: &Path, name: Option<&str>) -> Result<(PathBuf, String)
4444
{
4545
let manifest = Manifest::parse_from_toml(&manifest_path)?;
4646
if let Some(p) = manifest.package.as_ref() {
47-
if let (Some(n1), n2) = (name, &p.name) {
48-
if n1 == n2 {
47+
if let Some(name) = name {
48+
if name == p.name {
4949
return Ok((manifest_path, p.name.clone()));
5050
}
5151
} else {
5252
return Ok((manifest_path, p.name.clone()));
5353
}
5454
}
55-
if let (Some(w), Some(name)) = (manifest.workspace.as_ref(), name) {
55+
if let Some(w) = manifest.workspace.as_ref() {
56+
// TODO: This should also work if name is None - then all packages should simply be returned
57+
let name = name.ok_or(Error::MultiplePackagesNotSupported)?;
5658
if let Some(manifest_path) = member(&manifest_path, &w.members, name)? {
5759
return Ok((manifest_path, name.to_string()));
5860
}
@@ -61,7 +63,7 @@ pub fn find_package(path: &Path, name: Option<&str>) -> Result<(PathBuf, String)
6163
Err(Error::ManifestNotFound)
6264
}
6365

64-
pub fn find_workspace(manifest: &Path, name: &str) -> Result<Option<PathBuf>, Error> {
66+
pub fn find_workspace(manifest: &Path, name: &str) -> Result<Option<PathBuf>> {
6567
let dir = manifest.parent().unwrap();
6668
for manifest_path in dir
6769
.ancestors()
@@ -81,7 +83,7 @@ pub fn find_workspace(manifest: &Path, name: &str) -> Result<Option<PathBuf>, Er
8183
/// Returns the [`target-dir`] configured in `.cargo/config.toml` or `"target"` if not set.
8284
///
8385
/// [`target-dir`]: https://doc.rust-lang.org/cargo/reference/config.html#buildtarget-dir
84-
pub fn get_target_dir_name(config: Option<&Config>) -> Result<String, Error> {
86+
pub fn get_target_dir_name(config: Option<&Config>) -> Result<String> {
8587
if let Some(config) = config {
8688
if let Some(build) = config.build.as_ref() {
8789
if let Some(target_dir) = &build.target_dir {

0 commit comments

Comments
 (0)