Skip to content

Commit 968b629

Browse files
authored
Merge pull request #77 from fornwall/cleanup-remove
Small cleanups
2 parents 8579044 + 8e8ff26 commit 968b629

File tree

4 files changed

+25
-96
lines changed

4 files changed

+25
-96
lines changed

src/arguments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl Args {
164164
// "channel"
165165
.short('c')
166166
.num_args(1)
167-
// FIXME: remove if benchmarking is stabilized
167+
// Benchmarking currently requires nightly:
168168
.conflicts_with("bench")
169169
);
170170

src/main.rs

Lines changed: 15 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn try_main() -> MainResult<i32> {
111111
let input = match (args.script.clone().unwrap(), args.expr, args.loop_) {
112112
(script, false, false) => {
113113
let (path, mut file) =
114-
find_script(&script).ok_or(format!("could not find script: {}", script))?;
114+
find_script(script.as_ref()).ok_or(format!("could not find script: {}", script))?;
115115

116116
let script_name = path
117117
.file_stem()
@@ -316,8 +316,8 @@ struct InputAction {
316316
*/
317317
toolchain_version: Option<String>,
318318

319-
/// The package metadata structure for the current invocation.
320-
metadata: PackageMetadata,
319+
/// If script should be built in debug mode.
320+
debug: bool,
321321

322322
/// The package manifest contents.
323323
manifest: String,
@@ -342,7 +342,7 @@ impl InputAction {
342342
}
343343

344344
fn cargo(&self, script_args: &[String]) -> MainResult<Command> {
345-
let release_mode = !self.metadata.debug && !matches!(self.build_kind, BuildKind::Bench);
345+
let release_mode = !self.debug && !matches!(self.build_kind, BuildKind::Bench);
346346

347347
let built_binary_path = platform::binary_cache_path()
348348
.join(if release_mode { "release" } else { "debug" })
@@ -426,27 +426,6 @@ impl InputAction {
426426
}
427427
}
428428

429-
/**
430-
The metadata here serves two purposes:
431-
432-
1. It records everything necessary for compilation and execution of a package.
433-
2. It records everything that must be exactly the same in order for a cached executable to still be valid, in addition to the content hash.
434-
*/
435-
#[derive(Clone, Debug, Eq, PartialEq)]
436-
struct PackageMetadata {
437-
/// Path to the script file.
438-
path: Option<String>,
439-
440-
/// Was the script compiled in debug mode?
441-
debug: bool,
442-
443-
/// Sorted list of dependencies.
444-
deps: Vec<(String, String)>,
445-
446-
/// Sorted list of injected prelude items.
447-
prelude: Vec<String>,
448-
}
449-
450429
/**
451430
For the given input, this constructs the package metadata and checks the cache to see what should be done.
452431
*/
@@ -460,12 +439,11 @@ fn decide_action_for(
460439
let deps_iter = deps.iter().map(|(n, v)| (n as &str, v as &str));
461440
input.compute_id(deps_iter)
462441
};
442+
info!("id: {:?}", input_id);
463443

464444
let pkg_name = input.package_name();
465445
let bin_name = format!("{}_{}", &*pkg_name, input_id.to_str().unwrap());
466446

467-
info!("id: {:?}", input_id);
468-
469447
let (pkg_path, using_cache) = args
470448
.pkg_path
471449
.as_ref()
@@ -480,26 +458,12 @@ fn decide_action_for(
480458
let (mani_str, script_str) = manifest::split_input(input, &deps, &prelude, &bin_name)?;
481459

482460
// Forcibly override some flags based on build kind.
483-
let (debug, force) = match args.build_kind {
484-
BuildKind::Normal => (args.debug, args.force),
485-
BuildKind::Test => (true, false),
486-
BuildKind::Bench => (false, false),
461+
let debug = match args.build_kind {
462+
BuildKind::Normal => args.debug,
463+
BuildKind::Test => true,
464+
BuildKind::Bench => false,
487465
};
488466

489-
let input_meta = {
490-
let path = match input {
491-
Input::File(_, path, _) => Some(path.to_string_lossy().into_owned()),
492-
_ => None,
493-
};
494-
PackageMetadata {
495-
path,
496-
debug,
497-
deps,
498-
prelude,
499-
}
500-
};
501-
info!("input_meta: {:?}", input_meta);
502-
503467
let toolchain_version = args
504468
.toolchain_version
505469
.clone()
@@ -508,46 +472,23 @@ fn decide_action_for(
508472
_ => None,
509473
});
510474

511-
let mut action = InputAction {
475+
Ok(InputAction {
512476
cargo_output: args.cargo_output,
513-
force_compile: force,
514-
execute: true,
477+
force_compile: args.force,
478+
execute: !args.gen_pkg_only,
515479
pkg_path,
516480
using_cache,
517481
toolchain_version,
518-
metadata: input_meta,
482+
debug,
519483
manifest: mani_str,
520484
script: script_str,
521485
build_kind: args.build_kind,
522486
bin_name,
523-
};
524-
525-
// If we were told to only generate the package, we need to stop *now*
526-
if args.gen_pkg_only {
527-
action.execute = false;
528-
return Ok(action);
529-
}
530-
531-
// If we're not doing a regular build, stop.
532-
match action.build_kind {
533-
BuildKind::Normal => (),
534-
BuildKind::Test | BuildKind::Bench => {
535-
info!("not recompiling because: user asked for test/bench");
536-
action.force_compile = false;
537-
return Ok(action);
538-
}
539-
}
540-
541-
Ok(action)
487+
})
542488
}
543489

544490
/// Attempts to locate the script specified by the given path.
545-
fn find_script<P>(path: P) -> Option<(PathBuf, fs::File)>
546-
where
547-
P: AsRef<Path>,
548-
{
549-
let path = path.as_ref();
550-
491+
fn find_script(path: &Path) -> Option<(PathBuf, fs::File)> {
551492
if let Ok(file) = fs::File::open(path) {
552493
return Some((path.into(), file));
553494
}

src/manifest.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ pub fn split_input(
5252
|| line.starts_with("pub async fn main(")
5353
}
5454

55-
let template_buf;
5655
let (part_mani, source, template, sub_prelude) = match input {
5756
Input::File(_, _, content) => {
5857
assert_eq!(prelude_items.len(), 0);
@@ -68,10 +67,10 @@ pub fn split_input(
6867
(manifest, source, templates::get_template("file")?, false)
6968
}
7069
Input::Expr(content) => {
71-
template_buf = templates::get_template("expr")?;
72-
let (manifest, template_src) = find_embedded_manifest(&template_buf)
73-
.unwrap_or((Manifest::Toml(""), &template_buf));
74-
(manifest, content.to_string(), template_src.into(), true)
70+
let template_buf = templates::get_template("expr")?;
71+
let (manifest, template_src) =
72+
find_embedded_manifest(template_buf).unwrap_or((Manifest::Toml(""), template_buf));
73+
(manifest, content.to_string(), template_src, true)
7574
}
7675
Input::Loop(content, count) => {
7776
let templ = if *count { "loop-count" } else { "loop" };
@@ -99,13 +98,10 @@ pub fn split_input(
9998
subs.insert(consts::SCRIPT_PRELUDE_SUB, &prelude_str[..]);
10099
}
101100

102-
let source = templates::expand(&template, &subs)?;
103-
104-
info!("part_mani: {:?}", part_mani);
105-
info!("source: {:?}", source);
106-
101+
let source = templates::expand(template, &subs)?;
107102
let part_mani = part_mani.into_toml()?;
108103
info!("part_mani: {:?}", part_mani);
104+
info!("source: {:?}", source);
109105

110106
// It's-a mergin' time!
111107
let def_mani = default_manifest(input, bin_name)?;

src/templates.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::consts;
55
use crate::error::{MainError, MainResult};
66
use lazy_static::lazy_static;
77
use regex::Regex;
8-
use std::borrow::Cow;
98
use std::collections::HashMap;
109

1110
lazy_static! {
@@ -49,19 +48,12 @@ pub fn expand(src: &str, subs: &HashMap<&str, &str>) -> MainResult<String> {
4948
/**
5049
Attempts to locate and load the contents of the specified template.
5150
*/
52-
pub fn get_template(name: &str) -> MainResult<Cow<'static, str>> {
53-
if let Some(text) = builtin_template(name) {
54-
return Ok(text.into());
55-
}
56-
panic!("No such template: {name}");
57-
}
58-
59-
fn builtin_template(name: &str) -> Option<&'static str> {
60-
Some(match name {
51+
pub fn get_template(name: &str) -> MainResult<&'static str> {
52+
Ok(match name {
6153
"expr" => consts::EXPR_TEMPLATE,
6254
"file" => consts::FILE_TEMPLATE,
6355
"loop" => consts::LOOP_TEMPLATE,
6456
"loop-count" => consts::LOOP_COUNT_TEMPLATE,
65-
_ => return None,
57+
_ => panic!("No such template: {name}"),
6658
})
6759
}

0 commit comments

Comments
 (0)