Skip to content

Commit 1f52870

Browse files
committed
Old messy WIP
1 parent 97481bd commit 1f52870

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

src/artifact.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use std::path::Path;
1+
use std::path::{Path, PathBuf};
22

33
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
44
pub enum Artifact {
55
Root(String),
66
Example(String),
7+
// TODO: root, with a ty, because examples can also have a ty
8+
Bin { name: String, path: PathBuf },
79
}
810

911
impl Artifact {

src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub enum Error {
1313
Glob(GlobError),
1414
Io(PathBuf, IoError),
1515
Toml(PathBuf, TomlError),
16+
BinNotFound(String),
1617
}
1718

1819
impl Display for Error {
@@ -25,6 +26,7 @@ impl Display for Error {
2526
Self::Glob(error) => return error.fmt(f),
2627
Self::Io(path, error) => return write!(f, "{}: {}", path.display(), error),
2728
Self::Toml(file, error) => return write!(f, "{}: {}", file.display(), error),
29+
Self::BinNotFound(name) => return write!(f, "Can't find `{name}` bin at `src/bin/{name}.rs` or `src/bin/{name}/main.rs`. Please specify bin.path if you want to use a non-default path.", name = name),
2830
})
2931
}
3032
}

src/manifest.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::error::Error;
22
use serde::Deserialize;
3-
use std::path::Path;
3+
use std::path::{Path, PathBuf};
44

55
#[derive(Debug, Deserialize)]
66
pub struct Manifest {
@@ -9,6 +9,8 @@ pub struct Manifest {
99
pub lib: Option<Lib>,
1010
#[serde(default, rename = "bin")]
1111
pub bins: Vec<Bin>,
12+
#[serde(default, rename = "example")]
13+
pub examples: Vec<Example>,
1214
}
1315

1416
impl Manifest {
@@ -30,16 +32,33 @@ const fn default_true() -> bool {
3032
#[derive(Debug, Deserialize)]
3133
pub struct Package {
3234
pub name: String,
35+
36+
// https://doc.rust-lang.org/cargo/reference/cargo-targets.html#target-auto-discovery
3337
#[serde(default = "default_true")]
3438
pub autobins: bool,
39+
#[serde(default = "default_true")]
40+
pub autoexamples: bool,
41+
// #[serde(default = "default_true")]
42+
// pub autotests: bool,
43+
// #[serde(default = "default_true")]
44+
// pub autobenches: bool,
3545
}
3646

3747
#[derive(Debug, Deserialize)]
3848
pub struct Lib {
3949
pub name: Option<String>,
50+
pub path: Option<PathBuf>,
4051
}
4152

4253
#[derive(Debug, Deserialize)]
4354
pub struct Bin {
4455
pub name: String,
56+
pub path: Option<PathBuf>,
57+
}
58+
59+
#[derive(Debug, Deserialize)]
60+
pub struct Example {
61+
pub name: String,
62+
pub path: Option<PathBuf>,
63+
// crate_type?
4564
}

src/subcommand.rs

+39
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,45 @@ impl Subcommand {
8282
// https://doc.rust-lang.org/cargo/reference/cargo-targets.html#target-auto-discovery
8383

8484
let mut artifacts = vec![];
85+
86+
fn find_main_file(dir: &Path, name: &str) -> Option<PathBuf> {
87+
let alt_path = dir.join(format!("{}.rs", name));
88+
alt_path.is_file().then(|| alt_path).or_else(|| {
89+
let alt_path = dir.join(format!("{}/main.rs", name));
90+
alt_path.is_file().then(|| alt_path)
91+
})
92+
}
93+
94+
// Add all explicitly configured binaries
95+
for bin in &parsed_manifest.bins {
96+
let path = bin
97+
.path
98+
.clone()
99+
.or_else(|| find_main_file(&root_dir.join("src/bin"), &bin.name))
100+
.ok_or_else(|| Error::BinNotFound(bin.name.clone()))?;
101+
102+
artifacts.push(Artifact::Bin {
103+
name: bin.name.clone(),
104+
path,
105+
});
106+
}
107+
108+
// Add all explicitly configured examples
109+
for example in &parsed_manifest.examples {
110+
let path = example
111+
.path
112+
.clone()
113+
.or_else(|| find_main_file(&root_dir.join("src/bin"), &example.name))
114+
.ok_or_else(|| Error::BinNotFound(example.name.clone()))?;
115+
116+
artifacts.push(Artifact::Example {
117+
name: example.name.clone(),
118+
path,
119+
// ty: example.crate_type?
120+
});
121+
}
122+
123+
// TODO: autoexamples
85124
if args.examples {
86125
for file in utils::list_rust_files(&root_dir.join("examples"))? {
87126
artifacts.push(Artifact::Example(file));

0 commit comments

Comments
 (0)