2019-04-21 23:37:34 +00:00
|
|
|
use ansi_term::Color;
|
|
|
|
use std::process::Command;
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
use super::{Context, Module};
|
|
|
|
|
2019-07-19 20:18:52 +00:00
|
|
|
/// Creates a module with the current Rust version
|
2019-04-21 23:37:34 +00:00
|
|
|
///
|
|
|
|
/// Will display the Rust version if any of the following criteria are met:
|
2019-05-12 17:37:23 +00:00
|
|
|
/// - Current directory contains a file with a `.rs` extension
|
2019-04-23 18:51:08 +00:00
|
|
|
/// - Current directory contains a `Cargo.toml` file
|
2019-07-02 20:12:53 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-05-12 17:37:23 +00:00
|
|
|
let is_rs_project = context
|
|
|
|
.new_scan_dir()
|
|
|
|
.set_files(&["Cargo.toml"])
|
|
|
|
.set_extensions(&["rs"])
|
|
|
|
.scan();
|
|
|
|
|
2019-04-21 23:37:34 +00:00
|
|
|
if !is_rs_project {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
match get_rust_version() {
|
|
|
|
Some(rust_version) => {
|
2019-05-01 20:34:24 +00:00
|
|
|
const RUST_CHAR: &str = "🦀 ";
|
2019-04-21 23:37:34 +00:00
|
|
|
|
2019-07-02 20:12:53 +00:00
|
|
|
let mut module = context.new_module("rust")?;
|
2019-09-08 00:33:06 +00:00
|
|
|
let module_style = module
|
|
|
|
.config_value_style("style")
|
|
|
|
.unwrap_or_else(|| Color::Red.bold());
|
|
|
|
module.set_style(module_style);
|
2019-04-21 23:37:34 +00:00
|
|
|
|
|
|
|
let formatted_version = format_rustc_version(rust_version);
|
2019-05-01 20:34:24 +00:00
|
|
|
module.new_segment("symbol", RUST_CHAR);
|
2019-07-14 15:15:47 +00:00
|
|
|
module.new_segment("version", &formatted_version);
|
2019-04-21 23:37:34 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
Some(module)
|
2019-04-21 23:37:34 +00:00
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_rust_version() -> Option<String> {
|
2019-07-02 20:12:53 +00:00
|
|
|
match Command::new("rustc").arg("--version").output() {
|
2019-04-21 23:37:34 +00:00
|
|
|
Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_rustc_version(mut rustc_stdout: String) -> String {
|
2019-07-14 21:54:45 +00:00
|
|
|
let offset = &rustc_stdout.find('(').unwrap_or_else(|| rustc_stdout.len());
|
2019-04-21 23:37:34 +00:00
|
|
|
let formatted_version: String = rustc_stdout.drain(..offset).collect();
|
|
|
|
|
|
|
|
format!("v{}", formatted_version.replace("rustc", "").trim())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_rustc_version() {
|
|
|
|
let nightly_input = String::from("rustc 1.34.0-nightly (b139669f3 2019-04-10)");
|
|
|
|
assert_eq!(format_rustc_version(nightly_input), "v1.34.0-nightly");
|
|
|
|
|
|
|
|
let beta_input = String::from("rustc 1.34.0-beta.1 (2bc1d406d 2019-04-10)");
|
|
|
|
assert_eq!(format_rustc_version(beta_input), "v1.34.0-beta.1");
|
|
|
|
|
|
|
|
let stable_input = String::from("rustc 1.34.0 (91856ed52 2019-04-10)");
|
|
|
|
assert_eq!(format_rustc_version(stable_input), "v1.34.0");
|
2019-07-02 20:12:53 +00:00
|
|
|
|
|
|
|
let version_without_hash = String::from("rustc 1.34.0");
|
|
|
|
assert_eq!(format_rustc_version(version_without_hash), "v1.34.0");
|
2019-04-21 23:37:34 +00:00
|
|
|
}
|
|
|
|
}
|