1#![doc(html_root_url = "https://docs.rs/build-plan/0.1.1")]
10#![warn(missing_debug_implementations)]
11
12#[macro_use]
13extern crate serde_derive;
14extern crate serde;
15extern crate serde_json;
16extern crate semver;
17
18use serde::de::{self, Error};
19use std::collections::BTreeMap;
20use std::path::PathBuf;
21
22#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, PartialOrd, Ord, Serialize, Deserialize)]
26pub enum Kind {
27 Host,
28 Target,
29}
30
31#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
33pub enum LibKind {
34 Lib,
35 Rlib,
36 Dylib,
37 ProcMacro,
38 Other(String),
39}
40
41impl LibKind {
42 pub fn from_str(string: &str) -> LibKind {
43 match string {
44 "lib" => LibKind::Lib,
45 "rlib" => LibKind::Rlib,
46 "dylib" => LibKind::Dylib,
47 "proc-macro" => LibKind::ProcMacro,
48 s => LibKind::Other(s.to_string()),
49 }
50 }
51
52 pub fn crate_type(&self) -> &str {
54 match *self {
55 LibKind::Lib => "lib",
56 LibKind::Rlib => "rlib",
57 LibKind::Dylib => "dylib",
58 LibKind::ProcMacro => "proc-macro",
59 LibKind::Other(ref s) => s,
60 }
61 }
62
63 pub fn linkable(&self) -> bool {
64 match *self {
65 LibKind::Lib | LibKind::Rlib | LibKind::Dylib | LibKind::ProcMacro => true,
66 LibKind::Other(..) => false,
67 }
68 }
69}
70
71#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
73pub enum TargetKind {
74 Lib(Vec<LibKind>),
75 Bin,
76 Test,
77 Bench,
78 ExampleLib(Vec<LibKind>),
79 ExampleBin,
80 CustomBuild,
81}
82
83impl<'de> de::Deserialize<'de> for TargetKind {
84 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
85 D: de::Deserializer<'de> {
86 use self::TargetKind::*;
87
88 let raw = Vec::<&str>::deserialize(deserializer)?;
89 Ok(match *raw {
90 [] => return Err(D::Error::invalid_length(0, &"at least one target kind")),
91 ["bin"] => Bin,
92 ["example"] => ExampleBin, ["test"] => Test,
94 ["custom-build"] => CustomBuild,
95 ["bench"] => Bench,
96 ref lib_kinds => Lib(lib_kinds.iter().cloned().map(LibKind::from_str).collect()),
97 })
98 }
99}
100
101#[derive(Debug, Deserialize)]
103pub struct Invocation {
104 pub package_name: String,
106 pub package_version: semver::Version,
108 pub target_kind: TargetKind,
110 pub kind: Kind,
112 pub deps: Vec<usize>,
118 pub outputs: Vec<PathBuf>,
120 pub links: BTreeMap<PathBuf, PathBuf>,
122 pub program: String,
124 pub args: Vec<String>,
126 pub env: BTreeMap<String, String>,
128 pub cwd: Option<PathBuf>,
130}
131
132#[derive(Debug, Deserialize)]
134pub struct BuildPlan {
135 pub invocations: Vec<Invocation>,
137 pub inputs: Vec<PathBuf>,
139}
140
141impl BuildPlan {
142 pub fn from_cargo_output<S: AsRef<[u8]>>(output: S) -> serde_json::Result<Self> {
147 serde_json::from_slice(output.as_ref())
148 }
149}