improve MSRV info

master
Aleš Katona 2 years ago
parent 07c144fef4
commit f2def22c6e
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

@ -39,7 +39,7 @@ fn main() {
if verbose {
vcs.list();
} else {
vcs.info().expect("Error summarizing info");
vcs.status().expect("Error summarizing info");
}
}
}

@ -1,4 +1,5 @@
use crossterm::style::Stylize;
use semver::Version;
use std::{
collections::HashMap,
fs,
@ -94,19 +95,26 @@ impl Vcs {
}
}
pub fn info(&self) -> Result<(), Error> {
pub fn status(&self) -> Result<(), Error> {
if let Some(profile_name) = self.current_profile()? {
println!(
"{}{} [{}]",
"{}{} [{}] {}",
"Workspace is set to profile: ".with(SUCCESS_COLOR),
profile_name.bold().with(PROFILE_COLOR),
format!("{}", self.work_dir.display()).italic(),
self.min_msrv_str(),
);
} else {
if self.profiles.is_empty() {
println!("{}", "No profiles defined, use 'cargo vcs save' to generate one".with(ERROR_COLOR));
println!(
"{}",
"No profiles defined, use 'cargo vcs save' to generate one\n".with(ERROR_COLOR)
);
} else {
println!("{}", "Mismatching projects and profiles".with(ERROR_COLOR));
println!(
"{}",
"Mismatching projects and profiles\n".with(ERROR_COLOR)
);
}
self.list();
}
@ -118,6 +126,29 @@ impl Vcs {
self.projects.iter()
}
fn min_msrv_str(&self) -> String {
if let Some(msrv) = self.min_msrv() {
format!("MSRV: {}", msrv).with(MSRV_COLOR).to_string()
} else {
String::new()
}
}
fn min_msrv(&self) -> Option<&Version> {
let mut min_msrv = None;
for project in &self.projects {
let msrv = project.msrv();
if min_msrv.is_none() {
min_msrv = msrv;
} else if msrv.is_some() && msrv < min_msrv {
min_msrv = msrv;
}
}
min_msrv
}
pub fn save_profile(&self, profile_name: &str) -> Result<(), Error> {
let vcs_path = self.work_dir.join("Cargo_vcs.toml");
let vcs = match fs::read_to_string(&vcs_path) {

@ -74,6 +74,10 @@ impl Project {
}
}
pub fn msrv(&self) -> Option<&Version> {
self.msrv.as_ref()
}
fn parse_msrv(path: &Path) -> Result<Option<Version>, Error> {
let toml_path = path.join("Cargo.toml");
let toml_contents = std::fs::read_to_string(toml_path)?;
@ -127,6 +131,15 @@ impl Project {
root.push(profiles_tree);
}
if let Some(msrv) = &self.msrv {
let leaf = Tree::new(msrv.to_string().with(MSRV_COLOR).to_string());
let mut msrv_tree = Tree::new("[MSRV]".with(MSRV_COLOR).to_string());
msrv_tree.push(leaf);
root.push(msrv_tree);
}
if changes {
let statuses = self.repo.uncommitted_changes()?;
let uncommitted = statuses
@ -144,14 +157,6 @@ impl Project {
}
}
if let Some(msrv) = &self.msrv {
let leaf = Tree::new(msrv.to_string().with(MSRV_COLOR).to_string());
let mut msrv_tree = Tree::new("[MSRV]".with(MSRV_COLOR).to_string());
msrv_tree.push(leaf);
root.push(msrv_tree);
}
Ok(root)
}
}

Loading…
Cancel
Save