#![doc(html_root_url = "https://docs.rs/build-plan/0.1.1")]
#![warn(missing_debug_implementations)]
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate semver;
use serde::de::{self, Error};
use std::collections::BTreeMap;
use std::path::PathBuf;
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Kind {
Host,
Target,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum LibKind {
Lib,
Rlib,
Dylib,
ProcMacro,
Other(String),
}
impl LibKind {
pub fn from_str(string: &str) -> LibKind {
match string {
"lib" => LibKind::Lib,
"rlib" => LibKind::Rlib,
"dylib" => LibKind::Dylib,
"proc-macro" => LibKind::ProcMacro,
s => LibKind::Other(s.to_string()),
}
}
pub fn crate_type(&self) -> &str {
match *self {
LibKind::Lib => "lib",
LibKind::Rlib => "rlib",
LibKind::Dylib => "dylib",
LibKind::ProcMacro => "proc-macro",
LibKind::Other(ref s) => s,
}
}
pub fn linkable(&self) -> bool {
match *self {
LibKind::Lib | LibKind::Rlib | LibKind::Dylib | LibKind::ProcMacro => true,
LibKind::Other(..) => false,
}
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum TargetKind {
Lib(Vec<LibKind>),
Bin,
Test,
Bench,
ExampleLib(Vec<LibKind>),
ExampleBin,
CustomBuild,
}
impl<'de> de::Deserialize<'de> for TargetKind {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: de::Deserializer<'de> {
use self::TargetKind::*;
let raw = Vec::<&str>::deserialize(deserializer)?;
Ok(match *raw {
[] => return Err(D::Error::invalid_length(0, &"at least one target kind")),
["bin"] => Bin,
["example"] => ExampleBin, ["test"] => Test,
["custom-build"] => CustomBuild,
["bench"] => Bench,
ref lib_kinds => Lib(lib_kinds.iter().cloned().map(LibKind::from_str).collect()),
})
}
}
#[derive(Debug, Deserialize)]
pub struct Invocation {
pub package_name: String,
pub package_version: semver::Version,
pub target_kind: TargetKind,
pub kind: Kind,
pub deps: Vec<usize>,
pub outputs: Vec<PathBuf>,
pub links: BTreeMap<PathBuf, PathBuf>,
pub program: String,
pub args: Vec<String>,
pub env: BTreeMap<String, String>,
pub cwd: Option<PathBuf>,
}
#[derive(Debug, Deserialize)]
pub struct BuildPlan {
pub invocations: Vec<Invocation>,
pub inputs: Vec<PathBuf>,
}
impl BuildPlan {
pub fn from_cargo_output<S: AsRef<[u8]>>(output: S) -> serde_json::Result<Self> {
serde_json::from_slice(output.as_ref())
}
}