mirror of
https://github.com/fanzeyi/cargo-play
synced 2024-11-10 05:04:13 +00:00
allow user to override infer result
This commit is contained in:
parent
8aff17b192
commit
ed322db23f
7 changed files with 91 additions and 14 deletions
13
fixtures/infer-failure.rs
Normal file
13
fixtures/infer-failure.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use regex_syntax::hir::{self, Hir};
|
||||
use regex_syntax::Parser;
|
||||
|
||||
fn main() {
|
||||
let hir = Parser::new().parse("a|b").unwrap();
|
||||
assert_eq!(
|
||||
hir,
|
||||
Hir::alternation(vec![
|
||||
Hir::literal(hir::Literal::Unicode('a')),
|
||||
Hir::literal(hir::Literal::Unicode('b')),
|
||||
])
|
||||
);
|
||||
}
|
15
fixtures/infer-override.rs
Normal file
15
fixtures/infer-override.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
//# regex-syntax = "*"
|
||||
|
||||
use regex_syntax::hir::{self, Hir};
|
||||
use regex_syntax::Parser;
|
||||
|
||||
fn main() {
|
||||
let hir = Parser::new().parse("a|b").unwrap();
|
||||
assert_eq!(
|
||||
hir,
|
||||
Hir::alternation(vec![
|
||||
Hir::literal(hir::Literal::Unicode('a')),
|
||||
Hir::literal(hir::Literal::Unicode('b')),
|
||||
])
|
||||
);
|
||||
}
|
30
src/cargo.rs
30
src/cargo.rs
|
@ -1,5 +1,7 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
use serde::Serialize;
|
||||
use toml::value::Table;
|
||||
use toml::value::{Table, Value};
|
||||
|
||||
use crate::errors::CargoPlayError;
|
||||
use crate::opt::RustEdition;
|
||||
|
@ -24,6 +26,7 @@ impl CargoPackage {
|
|||
#[derive(Clone, Debug, Serialize)]
|
||||
pub(crate) struct CargoManifest {
|
||||
package: CargoPackage,
|
||||
#[serde(serialize_with = "toml::ser::tables_last")]
|
||||
dependencies: Table,
|
||||
}
|
||||
|
||||
|
@ -54,4 +57,29 @@ impl CargoManifest {
|
|||
dependencies,
|
||||
})
|
||||
}
|
||||
|
||||
fn normalize_crate_name(name: &str) -> String {
|
||||
name.replace("-", "_")
|
||||
}
|
||||
|
||||
fn normalized_dependencies(&self) -> HashSet<String> {
|
||||
self.dependencies
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|(key, _)| Self::normalize_crate_name(&key))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(crate) fn add_infers(&mut self, infers: HashSet<String>) {
|
||||
let existing = self.normalized_dependencies();
|
||||
|
||||
// we don't need to normalize crate name here (in filter) since it's impossible to have
|
||||
// dash in use statments.
|
||||
self.dependencies.extend(
|
||||
infers
|
||||
.into_iter()
|
||||
.filter(|key| !existing.contains(key))
|
||||
.map(|key| (key, Value::String("*".into()))),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
mod cargo;
|
||||
mod errors;
|
||||
mod infer;
|
||||
pub mod opt;
|
||||
pub mod steps;
|
||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -4,6 +4,7 @@ mod infer;
|
|||
mod opt;
|
||||
mod steps;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use std::iter::Iterator;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::vec::Vec;
|
||||
|
@ -45,21 +46,19 @@ fn main() -> Result<(), CargoPlayError> {
|
|||
}
|
||||
|
||||
let files = parse_inputs(&opt.src)?;
|
||||
let mut dependencies = extract_headers(&files);
|
||||
let dependencies = extract_headers(&files);
|
||||
|
||||
if opt.infer {
|
||||
let mut infers = infer::analyze_sources(&opt.src)?
|
||||
.into_iter()
|
||||
.map(|crat| format!("{} = \"*\"", crat))
|
||||
.collect();
|
||||
dependencies.append(&mut infers);
|
||||
}
|
||||
let infers = if opt.infer {
|
||||
infer::analyze_sources(&opt.src)?
|
||||
} else {
|
||||
HashSet::new()
|
||||
};
|
||||
|
||||
if opt.clean {
|
||||
rmtemp(&temp);
|
||||
}
|
||||
mktemp(&temp);
|
||||
write_cargo_toml(&temp, src_hash.clone(), dependencies, opt.edition)?;
|
||||
write_cargo_toml(&temp, src_hash.clone(), dependencies, opt.edition, infers)?;
|
||||
copy_sources(&temp, &opt.src)?;
|
||||
|
||||
let end = if let Some(save) = opt.save {
|
||||
|
|
11
src/steps.rs
11
src/steps.rs
|
@ -1,5 +1,4 @@
|
|||
use log::debug;
|
||||
use pathdiff::diff_paths;
|
||||
use std::collections::HashSet;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
|
@ -8,6 +7,9 @@ use std::path::{Path, PathBuf};
|
|||
use std::process::{Command, ExitStatus, Stdio};
|
||||
use std::vec::Vec;
|
||||
|
||||
use log::debug;
|
||||
use pathdiff::diff_paths;
|
||||
|
||||
use crate::cargo::CargoManifest;
|
||||
use crate::errors::CargoPlayError;
|
||||
use crate::opt::RustEdition;
|
||||
|
@ -67,10 +69,13 @@ pub fn write_cargo_toml(
|
|||
name: String,
|
||||
dependencies: Vec<String>,
|
||||
edition: RustEdition,
|
||||
infers: HashSet<String>,
|
||||
) -> Result<(), CargoPlayError> {
|
||||
let manifest = CargoManifest::new(name, dependencies, edition)?;
|
||||
let mut manifest = CargoManifest::new(name, dependencies, edition)?;
|
||||
let mut cargo = File::create(dir.join("Cargo.toml"))?;
|
||||
|
||||
manifest.add_infers(infers);
|
||||
|
||||
cargo.write_all(&toml::to_vec(&manifest).map_err(CargoPlayError::from_serde)?)?;
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -218,3 +218,21 @@ fn simple_infer() -> Result<()> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infer_failure() -> Result<()> {
|
||||
let rt = TestRuntime::new()?;
|
||||
let output = rt.run(&["--infer", "fixtures/infer-failure.rs"])?;
|
||||
assert_ne!(output.status.code().unwrap(), 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infer_override() -> Result<()> {
|
||||
let rt = TestRuntime::new()?;
|
||||
let output = rt.run(&["--infer", "fixtures/infer-override.rs"])?;
|
||||
assert_eq!(output.status.code().unwrap(), 0);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue