tintify 1.0.1

A lightweight library for terminal colors and styling
use std::env;
use std::ffi::OsString;
use std::process::Command;
use std::process::Output;
use std::str;
use std::str::Split;

fn main() -> () {
    println!("cargo:rustc-check-cfg=cfg(doc_cfg)");
    println!("cargo:rustc-check-cfg=cfg(const_mut_refs)");

    if let Some(compiler) = rustc_version() {
        if compiler.is_newer_than_minor(83) {
            println!("cargo:rustc-cfg=const_mut_refs");
        }
    }
}

struct Compiler {
    minor: u32,
    channel: ReleaseChannel
}

impl Compiler {
    fn is_newer_than_minor(&self, minor: u32) -> bool {
        match self.channel {
            ReleaseChannel::Stable | ReleaseChannel::Beta => self.minor >= minor,

            ReleaseChannel::Nightly                       => {
                self.minor > minor
            }
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ReleaseChannel {
    Stable,
    Beta,
    Nightly
}

fn rustc_version() -> Option<Compiler> {
    let rustc: OsString = env::var_os("RUSTC")?;
    let output: Output = Command::new(rustc).arg("--version").output().ok()?;
    let version: &str = str::from_utf8(&output.stdout).ok()?;
    let mut pieces: Split<char> = version.split('.');

    if pieces.next() != Some("rustc 1") {
        return None;
    }

    let minor: u32 = pieces.next()?.parse().ok()?;

    let channel: ReleaseChannel = if version.contains("nightly") {
        ReleaseChannel::Nightly
    } else if version.contains("beta") {
        ReleaseChannel::Beta
    } else {
        ReleaseChannel::Stable
    };

    Some(Compiler {
        minor,
        channel
    })
}