use std::fmt;
use std::default::Default;
use std::convert::TryFrom;
use crate::error::Error;
#[derive(Debug, Default, Clone, PartialEq)]
pub struct LibTarget {
common: CommonTarget,
crate_type: Option<CrateType>,
}
#[derive(Debug, Default, Clone, PartialEq)]
pub struct LibTargetBuilder {
name: Option<String>,
path: Option<String>,
test: Option<bool>,
doctest: Option<bool>,
bench: Option<bool>,
doc: Option<bool>,
plugin: Option<bool>,
proc_macro: Option<bool>,
harness: Option<bool>,
crate_type: Option<CrateType>,
}
impl LibTarget {
pub fn new() -> LibTargetBuilder {
Default::default()
}
pub fn name(&self) -> Option<&String> {
self.common.name.as_ref()
}
pub fn path(&self) -> Option<&String> {
self.common.path.as_ref()
}
pub fn test(&self) -> Option<bool> {
self.common.test
}
pub fn doctest(&self) -> Option<bool> {
self.common.doctest
}
pub fn bench(&self) -> Option<bool> {
self.common.bench
}
pub fn doc(&self) -> Option<bool> {
self.common.doc
}
pub fn plugin(&self) -> Option<bool> {
self.common.plugin
}
pub fn proc_macro(&self) -> Option<bool> {
self.common.proc_macro
}
pub fn harness(&self) -> Option<bool> {
self.common.harness
}
pub fn crate_type(&self) -> Option<CrateType> {
self.crate_type
}
}
impl LibTargetBuilder {
pub fn name(&mut self, name: &str) -> &mut Self {
self.name = Some(name.into());
self
}
pub fn path(&mut self, path: &str) -> &mut Self {
self.path = Some(path.into());
self
}
pub fn test(&mut self, test: bool) -> &mut Self {
self.test = Some(test);
self
}
pub fn doctest(&mut self, doctest: bool) -> &mut Self {
self.doctest = Some(doctest);
self
}
pub fn bench(&mut self, bench: bool) -> &mut Self {
self.bench = Some(bench);
self
}
pub fn doc(&mut self, doc: bool) -> &mut Self {
self.doc = Some(doc);
self
}
pub fn plugin(&mut self, plugin: bool) -> &mut Self {
self.plugin = Some(plugin);
self
}
pub fn proc_macro(&mut self, proc_macro: bool) -> &mut Self {
self.proc_macro = Some(proc_macro);
self
}
pub fn harness(&mut self, harness: bool) -> &mut Self {
self.harness = Some(harness);
self
}
pub fn crate_type(&mut self, crate_type: CrateType) -> &mut Self {
self.crate_type = Some(crate_type);
self
}
pub fn build(&self) -> LibTarget {
let common = CommonTarget {
name: self.name.clone(),
path: self.path.clone(),
test: self.test.clone(),
doctest: self.doctest.clone(),
bench: self.bench.clone(),
doc: self.doc.clone(),
plugin: self.plugin.clone(),
proc_macro: self.proc_macro.clone(),
harness: self.harness.clone(),
};
LibTarget {
common: common,
crate_type: self.crate_type.clone(),
}
}
}
impl<'a> From<&'a mut LibTargetBuilder> for LibTarget {
fn from(target: &'a mut LibTargetBuilder) -> LibTarget {
target.build()
}
}
#[derive(Debug, Default, Clone, PartialEq)]
struct CommonTarget {
name: Option<String>,
path: Option<String>,
test: Option<bool>,
doctest: Option<bool>,
bench: Option<bool>,
doc: Option<bool>,
plugin: Option<bool>,
proc_macro: Option<bool>,
harness: Option<bool>,
}
#[derive(Debug, Default, Clone, PartialEq)]
pub struct NonLibTarget {
common: CommonTarget,
required_features: Option<Vec<String>>,
}
#[derive(Debug, Default, Clone, PartialEq)]
pub struct NonLibTargetBuilder {
name: Option<String>,
path: Option<String>,
test: Option<bool>,
doctest: Option<bool>,
bench: Option<bool>,
doc: Option<bool>,
plugin: Option<bool>,
proc_macro: Option<bool>,
harness: Option<bool>,
required_features: Option<Vec<String>>,
}
impl NonLibTarget {
pub fn new() -> NonLibTargetBuilder {
Default::default()
}
pub fn name(&self) -> Option<&String> {
self.common.name.as_ref()
}
pub fn path(&self) -> Option<&String> {
self.common.path.as_ref()
}
pub fn test(&self) -> Option<bool> {
self.common.test
}
pub fn doctest(&self) -> Option<bool> {
self.common.doctest
}
pub fn bench(&self) -> Option<bool> {
self.common.bench
}
pub fn doc(&self) -> Option<bool> {
self.common.doc
}
pub fn plugin(&self) -> Option<bool> {
self.common.plugin
}
pub fn proc_macro(&self) -> Option<bool> {
self.common.proc_macro
}
pub fn harness(&self) -> Option<bool> {
self.common.harness
}
pub fn required_features(&self) -> Option<&Vec<String>> {
self.required_features.as_ref()
}
}
impl NonLibTargetBuilder {
pub fn name(&mut self, name: &str) -> &mut Self {
self.name = Some(name.into());
self
}
pub fn path(&mut self, path: &str) -> &mut Self {
self.path = Some(path.into());
self
}
pub fn test(&mut self, test: bool) -> &mut Self {
self.test = Some(test);
self
}
pub fn doctest(&mut self, doctest: bool) -> &mut Self {
self.doctest = Some(doctest);
self
}
pub fn bench(&mut self, bench: bool) -> &mut Self {
self.bench = Some(bench);
self
}
pub fn doc(&mut self, doc: bool) -> &mut Self {
self.doc = Some(doc);
self
}
pub fn plugin(&mut self, plugin: bool) -> &mut Self {
self.plugin = Some(plugin);
self
}
pub fn proc_macro(&mut self, proc_macro: bool) -> &mut Self {
self.proc_macro = Some(proc_macro);
self
}
pub fn harness(&mut self, harness: bool) -> &mut Self {
self.harness = Some(harness);
self
}
pub fn required_feature(&mut self, feature: &str) -> &mut Self {
if let Some(ref mut v) = self.required_features {
v.push(feature.into());
} else {
self.required_features = Some(vec![feature.into()]);
}
self
}
pub fn build(&self) -> NonLibTarget {
let common = CommonTarget {
name: self.name.clone(),
path: self.path.clone(),
test: self.test.clone(),
doctest: self.doctest.clone(),
bench: self.bench.clone(),
doc: self.doc.clone(),
plugin: self.plugin.clone(),
proc_macro: self.proc_macro.clone(),
harness: self.harness.clone(),
};
NonLibTarget {
common: common,
required_features: self.required_features.clone(),
}
}
}
impl<'a> From<&'a mut NonLibTargetBuilder> for NonLibTarget {
fn from(t: &'a mut NonLibTargetBuilder) -> NonLibTarget {
t.build()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CrateType {
Dylib,
Rlib,
Staticlib,
Cdylib,
ProcMacro,
}
impl<'a> TryFrom<&'a str> for CrateType {
type Error = Error;
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
Ok(match s.to_lowercase().as_str() {
"dylib" => CrateType::Dylib,
"rlib" => CrateType::Rlib,
"staticlib" => CrateType::Staticlib,
"cdylib" => CrateType::Cdylib,
"proc-macro" => CrateType::ProcMacro,
_ => return Err("not a valid crate type".into()),
})
}
}
impl TryFrom<String> for CrateType {
type Error = Error;
fn try_from(s: String) -> Result<Self, Self::Error> {
TryFrom::try_from(s.as_str())
}
}
impl fmt::Display for CrateType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", match *self {
CrateType::Dylib => "dylib",
CrateType::Rlib => "rlib",
CrateType::Staticlib => "staticlib",
CrateType::Cdylib => "cdylib",
CrateType::ProcMacro => "proc-macro",
})
}
}
pub type BinTarget = NonLibTarget;
pub type BenchTarget = NonLibTarget;
pub type TestTarget = NonLibTarget;
pub type ExampleTarget = NonLibTarget;