2019-04-21 23:37:34 +00:00
|
|
|
use ansi_term::Color;
|
2019-04-23 18:51:08 +00:00
|
|
|
use std::path::PathBuf;
|
2019-04-21 23:37:34 +00:00
|
|
|
use std::process::Command;
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
use super::{Context, Module};
|
|
|
|
|
2019-04-21 23:37:34 +00:00
|
|
|
/// Creates a segment with the current Rust version
|
|
|
|
///
|
|
|
|
/// Will display the Rust version if any of the following criteria are met:
|
2019-04-23 18:51:08 +00:00
|
|
|
/// - Current directory contains a `.rs` file
|
|
|
|
/// - Current directory contains a `Cargo.toml` file
|
2019-05-01 20:34:24 +00:00
|
|
|
pub fn segment(context: &Context) -> Option<Module> {
|
2019-04-23 18:51:08 +00:00
|
|
|
let is_rs_project = context.dir_files.iter().any(has_rs_files);
|
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 = "🦀 ";
|
|
|
|
let module_color = Color::Red.bold();
|
2019-04-21 23:37:34 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
let mut module = Module::new("rust");
|
|
|
|
module.set_style(module_color);
|
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);
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-23 18:51:08 +00:00
|
|
|
fn has_rs_files(dir_entry: &PathBuf) -> bool {
|
|
|
|
let is_rs_file =
|
|
|
|
|d: &PathBuf| -> bool { d.is_file() && d.extension().unwrap_or_default() == "rs" };
|
|
|
|
let is_cargo_toml =
|
|
|
|
|d: &PathBuf| -> bool { d.is_file() && d.file_name().unwrap_or_default() == "Cargo.toml" };
|
2019-04-21 23:37:34 +00:00
|
|
|
|
|
|
|
is_rs_file(&dir_entry) || is_cargo_toml(&dir_entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_rust_version() -> Option<String> {
|
|
|
|
match Command::new("rustc").arg("-V").output() {
|
|
|
|
Ok(output) => Some(String::from_utf8(output.stdout).unwrap()),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_rustc_version(mut rustc_stdout: String) -> String {
|
|
|
|
let offset = &rustc_stdout.find('(').unwrap();
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|