Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let the user select what features will be added #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 100 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ strum_macros = "0.26"
open = "5.1.3"
throbber-widgets-tui = "0.5.0"
tokio = { version = "1.37.0", features = ["full"] }
crates_io_api = "0.11.0"
21 changes: 13 additions & 8 deletions src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use strum::{Display, EnumIter, FromRepr};

use crate::view::widgets::{CrateItemList, ItemListStatus};
use crate::view::widgets::{CrateItemList, FeatureItemList, ItemListStatus};

pub mod backend;

Expand All @@ -26,16 +26,21 @@ impl From<crate::backend::Table> for Vec<CrateItemList> {
fn from(val: crate::backend::Table) -> Self {
let mut items: Vec<CrateItemList> = vec![];

val.entries.iter().for_each(|entr| {
entr.crates.iter().for_each(|cr| {
for entr in val.entries {
for krate in entr.crates {
items.push(CrateItemList::new(
cr.name.clone(),
cr.description.clone(),
krate.name.clone(),
krate.description.clone(),
ItemListStatus::default(),
cr.features.clone(),
krate.features.clone().map(|features| {
features
.iter()
.map(|feat| FeatureItemList::new(feat.clone()))
.collect()
}),
));
});
});
}
}

items
}
Expand Down
2 changes: 1 addition & 1 deletion src/content_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl From<&Group> for Table {
.notes
.clone()
.unwrap_or("No description".to_string()),
features: None,
features: None
});
}

Expand Down
31 changes: 29 additions & 2 deletions src/dependency_builder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::fmt::Write;
use std::{io, ops::Deref, process::Command};

use crate::view::widgets::{CrateItemList, ItemListStatus};

pub mod dependency_builder;

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct CrateToAdd {
pub crate_name: String,
pub features: Option<Vec<String>>,
Expand All @@ -15,6 +18,26 @@ impl Deref for CrateToAdd {
}
}

impl From<CrateItemList> for CrateToAdd {
fn from(value: CrateItemList) -> Self {
Self {
crate_name: value.name,
features: value.features.map(|features| {
features
.iter()
.filter_map(|feature_item| {
if feature_item.status == ItemListStatus::Selected {
Some(feature_item.name.clone())
} else {
None
}
})
.collect()
}),
}
}
}

pub struct DependenciesBuilder {
crates_to_add: Vec<CrateToAdd>,
}
Expand All @@ -27,7 +50,11 @@ impl DependenciesBuilder {
pub fn add_dependencies(&self) -> io::Result<()> {
for dependency in self.crates_to_add.clone() {
if let Some(features) = dependency.features {
let features: String = features.iter().map(|feat| format!(" {} ", feat)).collect();
let features: String =
features.iter().fold(String::new(), |mut output, feature| {
let _ = write!(output, " {feature} ");
output
});
Command::new("cargo")
.arg("add")
.arg(dependency.crate_name)
Expand Down
Loading