Skip to content

Commit

Permalink
refactor: Add macro to join futures
Browse files Browse the repository at this point in the history
  • Loading branch information
lj3954 authored and flexiondotorg committed Sep 30, 2024
1 parent 5e5c3ad commit db70182
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 202 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
/join_futures/target
9 changes: 7 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 @@ -20,3 +20,4 @@ serde = "1.0.202"
serde_json = "1.0.117"
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread", "sync"] }
zstd = "0.13.1"
join_futures = { path = "join_futures" }
7 changes: 7 additions & 0 deletions join_futures/Cargo.lock

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

10 changes: 10 additions & 0 deletions join_futures/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "join_futures"
version = "0.1.0"
edition = "2021"

[profile.dev]
opt-level = 3

[lib]
proc-macro = true
30 changes: 30 additions & 0 deletions join_futures/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use proc_macro::{TokenStream, TokenTree};

#[proc_macro]
pub fn join_futures(input: TokenStream) -> TokenStream {
let mut tokens = input.into_iter();

let variable = match tokens.find(|t| matches!(t, TokenTree::Ident(_))) {
Some(TokenTree::Ident(variable)) => variable.to_string(),
_ => panic!("You must provide a variable containing futures to join"),
};
let flatten_amount = match tokens.find(|t| matches!(t, TokenTree::Literal(_))) {
Some(TokenTree::Literal(flatten_amount)) => flatten_amount.to_string().trim().parse::<usize>().unwrap(),
_ => 0,
};

let tokens = tokens.skip_while(|t| matches!(t, TokenTree::Punct(_)));
let value_type = match tokens.map(|t| t.to_string()).collect::<String>() {
s if s.is_empty() => "Vec<Config>".to_string(),
s => s,
};

let mut value = format!("futures::future::join_all({variable}).await");
if flatten_amount > 0 {
value.push_str(&format!(
".into_iter(){}.collect::<{value_type}>()",
".flatten()".repeat(flatten_amount),
));
}
value.parse().unwrap()
}
23 changes: 5 additions & 18 deletions src/bsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::store_data::{ArchiveFormat, ChecksumSeparation, Config, Disk, Distro, Source, WebSource};
use crate::utils::capture_page;
use join_futures::join_futures;
use quickemu::config::{Arch, GuestOS};
use regex::Regex;
use std::sync::Arc;
Expand Down Expand Up @@ -84,22 +85,14 @@ impl Distro for FreeBSD {
[normal_editions, vm_image]
})
.collect::<Vec<_>>();
Some(futures::future::join_all(futures).await)
Some(join_futures!(futures))
} else {
log::warn!("Failed to fetch FreeBSD {arch} releases");
None
}
}
});
futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.flatten()
.flatten()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 4))
}
}

Expand Down Expand Up @@ -205,16 +198,10 @@ impl Distro for GhostBSD {
}
})
.collect::<Vec<_>>();
Some(futures::future::join_all(futures).await)
Some(join_futures!(futures))
}
});

futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 2))
}
}
44 changes: 9 additions & 35 deletions src/linux/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
store_data::{ChecksumSeparation, Config, Distro, Source, WebSource},
utils::{capture_page, GatherData, GithubAPI},
};
use join_futures::join_futures;
use regex::Regex;
use serde::Deserialize;
use std::sync::Arc;
Expand Down Expand Up @@ -39,12 +40,7 @@ impl Distro for Archcraft {
})
}
});
futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 1))
}
}

Expand Down Expand Up @@ -147,17 +143,11 @@ impl Distro for ArcoLinux {
}
})
.collect::<Vec<_>>();
Some(futures::future::join_all(futures).await)
Some(join_futures!(futures))
}
})
.collect::<Vec<_>>();
futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 2))
}
}

Expand Down Expand Up @@ -235,12 +225,7 @@ impl Distro for AthenaOS {
})
});

futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 1))
}
}

Expand Down Expand Up @@ -302,17 +287,11 @@ impl Distro for CachyOS {
})
.collect::<Vec<_>>();

Some(futures::future::join_all(futures).await)
Some(join_futures!(futures))
}
});

futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 2))
}
}

Expand Down Expand Up @@ -341,7 +320,7 @@ impl Distro for EndeavourOS {
}
}
});
futures::future::join_all(futures).await.into()
Some(join_futures!(futures))
}
}

Expand Down Expand Up @@ -383,11 +362,6 @@ impl Distro for Garuda {
}
});

futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 1))
}
}
3 changes: 2 additions & 1 deletion src/linux/arch/manjaro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
store_data::{Config, Distro, Source, WebSource},
utils::capture_page,
};
use join_futures::join_futures;
use regex::Regex;

const BIGLINUX_MIRROR: &str = "https://iso.biglinux.com.br/";
Expand Down Expand Up @@ -38,6 +39,6 @@ impl Distro for BigLinux {
}
});

futures::future::join_all(futures).await.into()
Some(join_futures!(futures))
}
}
67 changes: 10 additions & 57 deletions src/linux/debian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
store_data::{ChecksumSeparation, Config, Disk, Distro, Source, WebSource},
utils::{capture_page, GatherData, GithubAPI},
};
use join_futures::join_futures;
use quickemu::config::{Arch, DiskFormat};
use quickget_core::data_structures::ArchiveFormat;
use regex::Regex;
Expand Down Expand Up @@ -68,13 +69,7 @@ impl Distro for Antix {
}
});

futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 2))
}
}

Expand All @@ -96,12 +91,7 @@ impl Distro for BunsenLabs {
let url = format!("{BUNSENLABS_MIRROR}{}", &c[1]);
async move { ChecksumSeparation::Whitespace.build(&url).await }
});
let mut checksums = futures::future::join_all(checksum_futures)
.await
.into_iter()
.flatten()
.flatten()
.collect::<HashMap<String, String>>();
let mut checksums = join_futures!(checksum_futures, 2, HashMap<String, String>);

release_regex
.captures_iter(&html)
Expand Down Expand Up @@ -254,14 +244,7 @@ impl Distro for Debian {
})
.flatten();

futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.flatten()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 3))
}
}

Expand Down Expand Up @@ -310,13 +293,7 @@ impl Distro for Devuan {
)
}
});
futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 2))
}
}

Expand Down Expand Up @@ -360,22 +337,10 @@ impl Distro for EasyOS {
}
});

Some(
futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.collect::<Vec<_>>(),
)
Some(join_futures!(futures))
}
});
let mut releases = futures::future::join_all(release_futures)
.await
.into_iter()
.flatten()
.flatten()
.flatten()
.collect::<Vec<_>>();
let mut releases = join_futures!(release_futures, 4, Vec<(String, String)>);

releases.sort_by(|(a, _), (b, _)| {
if let (Ok(a), Ok(b)) = (
Expand Down Expand Up @@ -425,12 +390,7 @@ impl Distro for EasyOS {
})
}
});
futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.collect::<Vec<Config>>()
.into()
Some(join_futures!(futures, 1))
}
}

Expand Down Expand Up @@ -478,17 +438,10 @@ impl Distro for EndlessOS {
})
}
});
Some(futures::future::join_all(futures).await)
Some(join_futures!(futures))
}
});

futures::future::join_all(futures)
.await
.into_iter()
.flatten()
.flatten()
.flatten()
.collect::<Vec<_>>()
.into()
Some(join_futures!(futures, 3))
}
}
Loading

0 comments on commit db70182

Please sign in to comment.