chore: Make some of the deps of gpui optional (#16986)

Minor bookkeeping, that takes down dep count of gpui from 454 to 430 for
me.

Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2024-08-28 10:05:50 +02:00 committed by GitHub
parent 22a791d9c7
commit 4ec1f29df0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 53 additions and 46 deletions

View file

@ -13,7 +13,7 @@ path = "src/util.rs"
doctest = true
[features]
test-support = ["tempfile", "git2"]
test-support = ["tempfile", "git2", "rand"]
[dependencies]
anyhow.workspace = true
@ -23,7 +23,7 @@ futures.workspace = true
git2 = { workspace = true, optional = true }
globset.workspace = true
log.workspace = true
rand.workspace = true
rand = {workspace = true, optional = true}
regex.workspace = true
rust-embed.workspace = true
serde.workspace = true
@ -40,3 +40,4 @@ tendril = "0.4.3"
[dev-dependencies]
git2.workspace = true
tempfile.workspace = true
rand.workspace = true

View file

@ -6,7 +6,7 @@ pub mod serde;
pub mod test;
use futures::Future;
use rand::{seq::SliceRandom, Rng};
use regex::Regex;
use std::sync::OnceLock;
use std::{
@ -517,54 +517,59 @@ pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
Deferred(Some(f))
}
pub struct RandomCharIter<T: Rng> {
rng: T,
simple_text: bool,
}
#[cfg(any(test, feature = "test-support"))]
mod rng {
use rand::{seq::SliceRandom, Rng};
pub struct RandomCharIter<T: Rng> {
rng: T,
simple_text: bool,
}
impl<T: Rng> RandomCharIter<T> {
pub fn new(rng: T) -> Self {
Self {
rng,
simple_text: std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()),
impl<T: Rng> RandomCharIter<T> {
pub fn new(rng: T) -> Self {
Self {
rng,
simple_text: std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()),
}
}
pub fn with_simple_text(mut self) -> Self {
self.simple_text = true;
self
}
}
pub fn with_simple_text(mut self) -> Self {
self.simple_text = true;
self
}
}
impl<T: Rng> Iterator for RandomCharIter<T> {
type Item = char;
impl<T: Rng> Iterator for RandomCharIter<T> {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
if self.simple_text {
return if self.rng.gen_range(0..100) < 5 {
Some('\n')
} else {
Some(self.rng.gen_range(b'a'..b'z' + 1).into())
};
}
fn next(&mut self) -> Option<Self::Item> {
if self.simple_text {
return if self.rng.gen_range(0..100) < 5 {
Some('\n')
} else {
Some(self.rng.gen_range(b'a'..b'z' + 1).into())
};
}
match self.rng.gen_range(0..100) {
// whitespace
0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.rng).copied(),
// two-byte greek letters
20..=32 => char::from_u32(self.rng.gen_range(('α' as u32)..('ω' as u32 + 1))),
// // three-byte characters
33..=45 => ['✋', '✅', '❌', '❎', '⭐']
.choose(&mut self.rng)
.copied(),
// // four-byte characters
46..=58 => ['🍐', '🏀', '🍗', '🎉'].choose(&mut self.rng).copied(),
// ascii letters
_ => Some(self.rng.gen_range(b'a'..b'z' + 1).into()),
match self.rng.gen_range(0..100) {
// whitespace
0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.rng).copied(),
// two-byte greek letters
20..=32 => char::from_u32(self.rng.gen_range(('α' as u32)..('ω' as u32 + 1))),
// // three-byte characters
33..=45 => ['✋', '✅', '❌', '❎', '⭐']
.choose(&mut self.rng)
.copied(),
// // four-byte characters
46..=58 => ['🍐', '🏀', '🍗', '🎉'].choose(&mut self.rng).copied(),
// ascii letters
_ => Some(self.rng.gen_range(b'a'..b'z' + 1).into()),
}
}
}
}
#[cfg(any(test, feature = "test-support"))]
pub use rng::RandomCharIter;
/// Get an embedded file as a string.
pub fn asset_str<A: rust_embed::RustEmbed>(path: &str) -> Cow<'static, str> {
match A::get(path).unwrap().data {