Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add listing of binaries in the project #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ use toml::de;
use config::Config;
use manifest::Manifest;

pub use manifest::Binary;

/// Information about a Cargo project
pub struct Project {
name: String,
Expand All @@ -42,6 +44,8 @@ pub struct Project {
target_dir: PathBuf,

toml: PathBuf,

binaries: Vec<Binary>,
}

/// Errors
Expand All @@ -58,8 +62,8 @@ impl Project {
/// `path` doesn't need to be the directory that contains the `Cargo.toml` file; it can be any
/// point within the Cargo project.
pub fn query<P>(path: P) -> Result<Self, failure::Error>
where
P: AsRef<Path>,
where
P: AsRef<Path>,
{
let path = path.as_ref().canonicalize()?;
let root = search(&path, "Cargo.toml").ok_or(Error::NotACargoProject)?;
Expand Down Expand Up @@ -127,11 +131,24 @@ impl Project {

target_dir = target_dir.or_else(|| workspace.map(|path| path.join("target")));

let mut binaries = manifest.bin;

if binaries.is_empty() {
// see if src/main.rs exists, and if so add to automatically generated bin
if root.join("src/main.rs").exists() {
binaries.push(Binary {
name: manifest.package.name.clone(),
path: String::from("src/main.rs"),
});
}
}

Ok(Project {
name: manifest.package.name,
target,
target_dir: target_dir.unwrap_or(root.join("target")),
toml,
binaries,
})
}

Expand Down Expand Up @@ -226,6 +243,13 @@ impl Project {
pub fn target_dir(&self) -> &Path {
&self.target_dir
}

/// Get the binaries defined in the project.
///
/// If no binary is explicitly defined, the default binary is returned
pub fn binaries(&self) -> impl Iterator<Item=&Binary> {
self.binaries.iter()
}
}

/// Build artifact
Expand Down Expand Up @@ -265,8 +289,8 @@ fn search<'p, P: AsRef<Path>>(path: &'p Path, file: P) -> Option<&'p Path> {
}

fn parse<T>(path: &Path) -> Result<T, failure::Error>
where
T: for<'de> Deserialize<'de>,
where
T: for<'de> Deserialize<'de>,
{
let mut s = String::new();
File::open(path)?.read_to_string(&mut s)?;
Expand Down Expand Up @@ -324,4 +348,12 @@ mod tests {

assert!(p.ends_with(&format!("target/{}/debug/examples/bar.wasm", wasm)));
}

#[test]
fn binaries() {
let project = Project::query(env::current_dir().unwrap()).unwrap();

// being a library project there is no automatically inferred binary
assert_eq!(project.binaries().count(), 0);
}
}
38 changes: 38 additions & 0 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#[derive(Deserialize)]
pub struct Manifest {
pub package: Package,
#[serde(default)]
pub bin: Vec<Binary>,
}

#[derive(Deserialize)]
Expand All @@ -12,6 +14,15 @@ pub struct Package {
pub description: Option<String>
}

#[derive(Deserialize)]
/// A binary target in the project
pub struct Binary {
/// The name of the binary target
pub name: String,
/// The source path of the target
pub path: String,
}

#[cfg(test)]
mod tests {
use toml;
Expand Down Expand Up @@ -58,4 +69,31 @@ description = "Test description"

assert_eq!(manifest.package.description.unwrap(), "Test description");
}

#[test]
fn binaries() {
let manifest: Manifest = toml::from_str(
r#"
[package]
name = "foo"
version = "0.1"

[[bin]]
name = "foobar"
path = "src/foo.rs"

[[bin]]
name = "barfoo"
path = "src/bar.rs"
"#,
).unwrap();

assert_eq!(manifest.bin.len(), 2);

assert_eq!(manifest.bin[0].name, "foobar");
assert_eq!(manifest.bin[0].path, "src/foo.rs");

assert_eq!(manifest.bin[1].name, "barfoo");
assert_eq!(manifest.bin[1].path, "src/bar.rs");
}
}
3 changes: 3 additions & 0 deletions test-projects/default-binary/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "foo"
version = "0.1"
3 changes: 3 additions & 0 deletions test-projects/default-binary/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {

}
3 changes: 3 additions & 0 deletions test-projects/no-default-binary/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "foo"
version = "0.1"
3 changes: 3 additions & 0 deletions test-projects/no-default-binary/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {

}
21 changes: 21 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extern crate cargo_project;

use cargo_project::Project;

#[test]
fn test_inferred_binary() {
let project = Project::query("./test-projects/default-binary").unwrap();

let binaries = project.binaries().collect::<Vec<_>>();
assert_eq!(binaries.len(), 1);
assert_eq!(binaries[0].name, "foo");
assert_eq!(binaries[0].path, "src/main.rs");
}

#[test]
fn test_no_inferred_binary() {
let project = Project::query("./test-projects/no-default-binary").unwrap();

let binaries = project.binaries().collect::<Vec<_>>();
assert_eq!(binaries.len(), 0);
}