Skip to content

Commit

Permalink
Remove itertools dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez authored and gdesmott committed Aug 17, 2021
1 parent 5fc5a15 commit 6ae93fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pkg-config = "0.3"
toml = { version = "0.5", default-features = false }
version-compare = "0.0.11"
heck = "0.3"
itertools = "0.10"
cfg-expr = "0.8"

[dev-dependencies]
Expand Down
50 changes: 26 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ extern crate lazy_static;
mod test;

use heck::{ShoutySnakeCase, SnakeCase};
use itertools::Itertools;
use std::collections::HashMap;
use std::env;
use std::fmt;
Expand Down Expand Up @@ -276,65 +275,68 @@ impl Dependencies {
self.libs.iter().map(|(k, v)| (k.as_str(), v))
}

fn aggregate_str<F: Fn(&Library) -> &Vec<String>>(
&self,
getter: F,
) -> impl Iterator<Item = &str> {
self.libs
fn aggregate_str<F: Fn(&Library) -> &Vec<String>>(&self, getter: F) -> Vec<&str> {
let mut v = self
.libs
.values()
.map(|l| getter(l))
.flatten()
.map(|s| s.as_str())
.sorted()
.dedup()
.collect::<Vec<_>>();
v.sort();
v.dedup();
v
}

fn aggregate_path_buf<F: Fn(&Library) -> &Vec<PathBuf>>(
&self,
getter: F,
) -> impl Iterator<Item = &PathBuf> {
self.libs
fn aggregate_path_buf<F: Fn(&Library) -> &Vec<PathBuf>>(&self, getter: F) -> Vec<&PathBuf> {
let mut v = self
.libs
.values()
.map(|l| getter(l))
.flatten()
.sorted()
.dedup()
.collect::<Vec<_>>();
v.sort();
v.dedup();
v
}

/// An iterator returning each [Library::libs] of each library, removing duplicates.
pub fn all_libs(&self) -> impl Iterator<Item = &str> {
pub fn all_libs(&self) -> Vec<&str> {
self.aggregate_str(|l| &l.libs)
}

/// An iterator returning each [Library::link_paths] of each library, removing duplicates.
pub fn all_link_paths(&self) -> impl Iterator<Item = &PathBuf> {
pub fn all_link_paths(&self) -> Vec<&PathBuf> {
self.aggregate_path_buf(|l| &l.link_paths)
}

/// An iterator returning each [Library::frameworks] of each library, removing duplicates.
pub fn all_frameworks(&self) -> impl Iterator<Item = &str> {
pub fn all_frameworks(&self) -> Vec<&str> {
self.aggregate_str(|l| &l.frameworks)
}

/// An iterator returning each [Library::framework_paths] of each library, removing duplicates.
pub fn all_framework_paths(&self) -> impl Iterator<Item = &PathBuf> {
pub fn all_framework_paths(&self) -> Vec<&PathBuf> {
self.aggregate_path_buf(|l| &l.framework_paths)
}

/// An iterator returning each [Library::include_paths] of each library, removing duplicates.
pub fn all_include_paths(&self) -> impl Iterator<Item = &PathBuf> {
pub fn all_include_paths(&self) -> Vec<&PathBuf> {
self.aggregate_path_buf(|l| &l.include_paths)
}

/// An iterator returning each [Library::defines] of each library, removing duplicates.
pub fn all_defines(&self) -> impl Iterator<Item = (&str, &Option<String>)> {
self.libs
pub fn all_defines(&self) -> Vec<(&str, &Option<String>)> {
let mut v = self
.libs
.values()
.map(|l| l.defines.iter())
.flatten()
.map(|(k, v)| (k.as_str(), v))
.sorted()
.dedup()
.collect::<Vec<_>>();
v.sort();
v.dedup();
v
}

fn add(&mut self, name: &str, lib: Library) {
Expand Down
15 changes: 6 additions & 9 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,31 +815,28 @@ fn optional() {
fn aggregate() {
let (libraries, _) = toml("toml-two-libs", vec![]).unwrap();

assert_eq!(libraries.all_libs(), vec!["test", "test2"]);
assert_eq!(
libraries.all_libs().collect::<Vec<&str>>(),
vec!["test", "test2"]
);
assert_eq!(
libraries.all_link_paths().collect::<Vec<&PathBuf>>(),
libraries.all_link_paths(),
vec![Path::new("/usr/lib"), Path::new("/usr/lib64")]
);
assert_eq!(
libraries.all_frameworks().collect::<Vec<&str>>(),
libraries.all_frameworks(),
vec!["someframework", "someotherframework"]
);
assert_eq!(
libraries.all_framework_paths().collect::<Vec<&PathBuf>>(),
libraries.all_framework_paths(),
vec![Path::new("/usr/lib"), Path::new("/usr/lib64")]
);
assert_eq!(
libraries.all_include_paths().collect::<Vec<&PathBuf>>(),
libraries.all_include_paths(),
vec![
Path::new("/usr/include/testanotherlib"),
Path::new("/usr/include/testlib")
]
);
assert_eq!(
libraries.all_defines().collect::<Vec<_>>(),
libraries.all_defines(),
vec![
("AWESOME", &None),
("BADGER", &Some("yes".into())),
Expand Down

0 comments on commit 6ae93fc

Please sign in to comment.