1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-16 10:05:13 +00:00
starship/src/modules/rust.rs

75 lines
2.3 KiB
Rust
Raw Normal View History

2019-04-21 23:37:34 +00:00
use ansi_term::Color;
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:
/// - 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> {
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,
}
}
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");
}
}