assistant: Update current project context to work with Cargo workspaces (#11935)

This PR updates the current project context to work with Cargo
workspaces.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-05-16 16:59:57 -04:00 committed by GitHub
parent f8672289fc
commit 55f08c0511
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,3 +1,4 @@
use std::fmt::Write;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@ -92,32 +93,54 @@ impl CurrentProjectContext {
let cargo_toml: cargo_toml::Manifest = toml::from_str(&buffer)?; let cargo_toml: cargo_toml::Manifest = toml::from_str(&buffer)?;
let mut message = String::new(); let mut message = String::new();
writeln!(message, "You are in a Rust project.")?;
let name = cargo_toml if let Some(workspace) = cargo_toml.workspace {
.package writeln!(
.as_ref() message,
.map(|package| package.name.as_str()); "The project is a Cargo workspace with the following members:"
if let Some(name) = name { )?;
message.push_str(&format!(" named \"{name}\"")); for member in workspace.members {
} writeln!(message, "- {member}")?;
message.push_str(". "); }
let description = cargo_toml if !workspace.default_members.is_empty() {
.package writeln!(message, "The default members are:")?;
.as_ref() for member in workspace.default_members {
.and_then(|package| package.description.as_ref()) writeln!(message, "- {member}")?;
.and_then(|description| description.get().ok().cloned()); }
if let Some(description) = description.as_ref() { }
message.push_str("It describes itself as ");
message.push_str(&format!("\"{description}\""));
message.push_str(". ");
}
let dependencies = cargo_toml.dependencies.keys().cloned().collect::<Vec<_>>(); if !workspace.dependencies.is_empty() {
if !dependencies.is_empty() { writeln!(
message.push_str("The following dependencies are installed: "); message,
message.push_str(&dependencies.join(", ")); "The following workspace dependencies are installed:"
message.push_str(". "); )?;
for dependency in workspace.dependencies.keys() {
writeln!(message, "- {dependency}")?;
}
}
} else if let Some(package) = cargo_toml.package {
writeln!(
message,
"The project name is \"{name}\".",
name = package.name
)?;
let description = package
.description
.as_ref()
.and_then(|description| description.get().ok().cloned());
if let Some(description) = description.as_ref() {
writeln!(message, "It describes itself as \"{description}\".")?;
}
if !cargo_toml.dependencies.is_empty() {
writeln!(message, "The following dependencies are installed:")?;
for dependency in cargo_toml.dependencies.keys() {
writeln!(message, "- {dependency}")?;
}
}
} }
Ok(message) Ok(message)