From f2def22c6ee6b01a33ed5a54bf4b184a60a432f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Katona?= Date: Sun, 17 Jul 2022 10:53:57 -0700 Subject: [PATCH] improve MSRV info --- src/main.rs | 2 +- src/vcs.rs | 39 +++++++++++++++++++++++++++++++++++---- src/vcs/project.rs | 21 +++++++++++++-------- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7a43f23..ac3e9c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,7 @@ fn main() { if verbose { vcs.list(); } else { - vcs.info().expect("Error summarizing info"); + vcs.status().expect("Error summarizing info"); } } } diff --git a/src/vcs.rs b/src/vcs.rs index 44bd642..66515fd 100644 --- a/src/vcs.rs +++ b/src/vcs.rs @@ -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) { diff --git a/src/vcs/project.rs b/src/vcs/project.rs index 7af5591..a21c4b2 100644 --- a/src/vcs/project.rs +++ b/src/vcs/project.rs @@ -74,6 +74,10 @@ impl Project { } } + pub fn msrv(&self) -> Option<&Version> { + self.msrv.as_ref() + } + fn parse_msrv(path: &Path) -> Result, 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) } }