#![deny(missing_docs)]
#![deny(warnings)]
#![deny(unused_extern_crates)]
use std::io;
use std::str::FromStr;
macro_rules! rust_target_def {
( $( $( #[$attr:meta] )* => $release:ident => $value:expr; )* ) => {
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Hash)]
#[allow(non_camel_case_types)]
pub enum RustTarget {
$(
$(
#[$attr]
)*
$release,
)*
}
impl Default for RustTarget {
fn default() -> RustTarget {
LATEST_STABLE_RUST
}
}
impl FromStr for RustTarget {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.as_ref() {
$(
stringify!($value) => Ok(RustTarget::$release),
)*
_ => Err(
io::Error::new(
io::ErrorKind::InvalidInput,
concat!(
"Got an invalid rust target. Accepted values ",
"are of the form ",
"\"1.0\" or \"nightly\"."))),
}
}
}
impl From<RustTarget> for String {
fn from(target: RustTarget) -> Self {
match target {
$(
RustTarget::$release => stringify!($value),
)*
}.into()
}
}
}
}
macro_rules! rust_target_values_def {
( $( $( #[$attr:meta] )* => $release:ident => $value:expr; )* ) => {
pub static RUST_TARGET_STRINGS: &'static [&str] = &[
$(
stringify!($value),
)*
];
}
}
macro_rules! rust_target_base {
( $x_macro:ident ) => {
$x_macro!(
=> Stable_1_0 => 1.0;
=> Stable_1_19 => 1.19;
=> Stable_1_20 => 1.20;
=> Stable_1_21 => 1.21;
=> Stable_1_25 => 1.25;
=> Nightly => nightly;
);
}
}
rust_target_base!(rust_target_def);
rust_target_base!(rust_target_values_def);
pub const LATEST_STABLE_RUST: RustTarget = RustTarget::Stable_1_21;
macro_rules! rust_feature_def {
( $( $( #[$attr:meta] )* => $feature:ident; )* ) => {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct RustFeatures {
$(
$(
#[$attr]
)*
pub $feature: bool,
)*
}
impl RustFeatures {
fn new() -> Self {
RustFeatures {
$(
$feature: false,
)*
}
}
}
}
}
rust_feature_def!(
=> untagged_union;
=> thiscall_abi;
=> builtin_clone_impls;
=> repr_align;
=> associated_const;
);
impl From<RustTarget> for RustFeatures {
fn from(rust_target: RustTarget) -> Self {
let mut features = RustFeatures::new();
if rust_target >= RustTarget::Stable_1_19 {
features.untagged_union = true;
}
if rust_target >= RustTarget::Stable_1_20 {
features.associated_const = true;
}
if rust_target >= RustTarget::Stable_1_21 {
features.builtin_clone_impls = true;
}
if rust_target >= RustTarget::Stable_1_25 {
features.repr_align = true;
}
if rust_target >= RustTarget::Nightly {
features.thiscall_abi = true;
}
features
}
}
impl Default for RustFeatures {
fn default() -> Self {
let default_rust_target: RustTarget = Default::default();
Self::from(default_rust_target)
}
}
#[cfg(test)]
mod test {
#![allow(unused_imports)]
use super::*;
fn test_target(target_str: &str, target: RustTarget) {
let target_string: String = target.into();
assert_eq!(target_str, target_string);
assert_eq!(target, RustTarget::from_str(target_str).unwrap());
}
#[test]
fn str_to_target() {
test_target("1.0", RustTarget::Stable_1_0);
test_target("1.19", RustTarget::Stable_1_19);
test_target("1.21", RustTarget::Stable_1_21);
test_target("1.25", RustTarget::Stable_1_25);
test_target("nightly", RustTarget::Nightly);
}
}