mirror of
https://github.com/fanzeyi/cargo-play
synced 2024-11-14 14:57:09 +00:00
add edition support
This commit is contained in:
parent
951058c428
commit
fde3a2ec24
4 changed files with 55 additions and 7 deletions
13
src/cargo.rs
13
src/cargo.rs
|
@ -2,6 +2,7 @@ use serde::Serialize;
|
|||
use toml::value::Table;
|
||||
|
||||
use crate::errors::CargoPlayError;
|
||||
use crate::opt::RustEdition;
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
struct CargoPackage {
|
||||
|
@ -11,11 +12,11 @@ struct CargoPackage {
|
|||
}
|
||||
|
||||
impl CargoPackage {
|
||||
fn new(name: String) -> Self {
|
||||
fn new(name: String, edition: RustEdition) -> Self {
|
||||
Self {
|
||||
name: name.to_lowercase(),
|
||||
version: "0.1.0".into(),
|
||||
edition: "2018".into(),
|
||||
edition: edition.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +28,11 @@ pub(crate) struct CargoManifest {
|
|||
}
|
||||
|
||||
impl CargoManifest {
|
||||
pub(crate) fn new(name: String, dependencies: Vec<String>) -> Result<Self, CargoPlayError> {
|
||||
pub(crate) fn new(
|
||||
name: String,
|
||||
dependencies: Vec<String>,
|
||||
edition: RustEdition,
|
||||
) -> Result<Self, CargoPlayError> {
|
||||
let dependencies = dependencies
|
||||
.into_iter()
|
||||
.map(|dependency| dependency.parse::<toml::Value>())
|
||||
|
@ -45,7 +50,7 @@ impl CargoManifest {
|
|||
.collect();
|
||||
|
||||
Ok(Self {
|
||||
package: CargoPackage::new(name),
|
||||
package: CargoPackage::new(name, edition),
|
||||
dependencies,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -12,6 +12,9 @@ pub enum CargoPlayError {
|
|||
#[fail(display = "Unable to compute relative path of {:?}", _0)]
|
||||
DiffPathError(std::path::PathBuf),
|
||||
|
||||
#[fail(display = "Unexpected edition {:?}. Edition must be 2015/2018.", _0)]
|
||||
InvalidEdition(String),
|
||||
|
||||
/// Helper error kind only exists for development purpose.
|
||||
#[fail(display = "{:?}", _0)]
|
||||
_Message(String),
|
||||
|
|
|
@ -5,7 +5,6 @@ mod errors;
|
|||
mod opt;
|
||||
|
||||
use log::debug;
|
||||
use opt::Opt;
|
||||
use pathdiff::diff_paths;
|
||||
use std::env::temp_dir;
|
||||
use std::fs::File;
|
||||
|
@ -18,6 +17,7 @@ use structopt::StructOpt;
|
|||
|
||||
use crate::cargo::CargoManifest;
|
||||
use crate::errors::CargoPlayError;
|
||||
use crate::opt::{Opt, RustEdition};
|
||||
|
||||
fn parse_inputs(inputs: &Vec<PathBuf>) -> Result<Vec<String>, CargoPlayError> {
|
||||
inputs
|
||||
|
@ -65,8 +65,9 @@ fn write_cargo_toml(
|
|||
dir: &PathBuf,
|
||||
name: String,
|
||||
dependencies: Vec<String>,
|
||||
edition: RustEdition,
|
||||
) -> Result<(), CargoPlayError> {
|
||||
let manifest = CargoManifest::new(name, dependencies)?;
|
||||
let manifest = CargoManifest::new(name, dependencies, edition)?;
|
||||
let mut cargo = File::create(dir.join("Cargo.toml"))?;
|
||||
|
||||
cargo.write_all(&toml::to_vec(&manifest).map_err(CargoPlayError::from_serde)?)?;
|
||||
|
@ -140,7 +141,7 @@ fn main() -> Result<(), CargoPlayError> {
|
|||
let dependencies = extract_headers(&files);
|
||||
let temp = mktemp(opt.temp_dirname());
|
||||
|
||||
write_cargo_toml(&temp, opt.src_hash(), dependencies)?;
|
||||
write_cargo_toml(&temp, opt.src_hash(), dependencies, opt.edition)?;
|
||||
copy_sources(&temp, &opt.src)?;
|
||||
|
||||
match run_cargo_build(&temp)?.code() {
|
||||
|
|
39
src/opt.rs
39
src/opt.rs
|
@ -2,9 +2,41 @@ use base64;
|
|||
use sha1;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
use std::vec::Vec;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use crate::errors::CargoPlayError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum RustEdition {
|
||||
E2015,
|
||||
E2018,
|
||||
}
|
||||
|
||||
impl FromStr for RustEdition {
|
||||
type Err = CargoPlayError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s == "2018" {
|
||||
Ok(Self::E2018)
|
||||
} else if s == "2015" {
|
||||
Ok(Self::E2015)
|
||||
} else {
|
||||
Err(CargoPlayError::InvalidEdition(s.into()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<String> for RustEdition {
|
||||
fn into(self) -> String {
|
||||
match self {
|
||||
RustEdition::E2015 => "2015".into(),
|
||||
RustEdition::E2018 => "2018".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "cargo-play", about = "Single file cargo runner.")]
|
||||
pub(crate) struct Opt {
|
||||
|
@ -17,6 +49,13 @@ pub(crate) struct Opt {
|
|||
raw(required = "true", validator = "file_exist")
|
||||
)]
|
||||
pub src: Vec<PathBuf>,
|
||||
#[structopt(
|
||||
short = "e",
|
||||
long = "edition",
|
||||
default_value = "2018",
|
||||
raw(possible_values = r#"&["2015", "2018"]"#)
|
||||
)]
|
||||
pub edition: RustEdition,
|
||||
}
|
||||
|
||||
impl Opt {
|
||||
|
|
Loading…
Reference in a new issue