util.rs

  1pub mod arc_cow;
  2pub mod fs;
  3pub mod github;
  4pub mod http;
  5pub mod paths;
  6#[cfg(any(test, feature = "test-support"))]
  7pub mod test;
  8
  9use futures::Future;
 10use lazy_static::lazy_static;
 11use rand::{seq::SliceRandom, Rng};
 12use std::{
 13    borrow::Cow,
 14    cmp::{self, Ordering},
 15    env,
 16    ops::{AddAssign, Range, RangeInclusive},
 17    panic::Location,
 18    pin::Pin,
 19    task::{Context, Poll},
 20    time::Instant,
 21};
 22use unicase::UniCase;
 23
 24pub use take_until::*;
 25
 26#[macro_export]
 27macro_rules! debug_panic {
 28    ( $($fmt_arg:tt)* ) => {
 29        if cfg!(debug_assertions) {
 30            panic!( $($fmt_arg)* );
 31        } else {
 32            let backtrace = std::backtrace::Backtrace::capture();
 33            log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
 34        }
 35    };
 36}
 37
 38pub fn truncate(s: &str, max_chars: usize) -> &str {
 39    match s.char_indices().nth(max_chars) {
 40        None => s,
 41        Some((idx, _)) => &s[..idx],
 42    }
 43}
 44
 45pub fn http_proxy_from_env() -> Option<isahc::http::Uri> {
 46    macro_rules! try_env {
 47        ($($env:literal),+) => {
 48            $(
 49                if let Ok(env) = std::env::var($env) {
 50                    return env.parse::<isahc::http::Uri>().ok();
 51                }
 52            )+
 53        };
 54    }
 55
 56    try_env!(
 57        "ALL_PROXY",
 58        "all_proxy",
 59        "HTTPS_PROXY",
 60        "https_proxy",
 61        "HTTP_PROXY",
 62        "http_proxy"
 63    );
 64    None
 65}
 66
 67/// Removes characters from the end of the string if its length is greater than `max_chars` and
 68/// appends "..." to the string. Returns string unchanged if its length is smaller than max_chars.
 69pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {
 70    debug_assert!(max_chars >= 5);
 71
 72    let truncation_ix = s.char_indices().map(|(i, _)| i).nth(max_chars);
 73    match truncation_ix {
 74        Some(length) => s[..length].to_string() + "",
 75        None => s.to_string(),
 76    }
 77}
 78
 79/// Removes characters from the front of the string if its length is greater than `max_chars` and
 80/// prepends the string with "...". Returns string unchanged if its length is smaller than max_chars.
 81pub fn truncate_and_remove_front(s: &str, max_chars: usize) -> String {
 82    debug_assert!(max_chars >= 5);
 83
 84    let truncation_ix = s.char_indices().map(|(i, _)| i).nth_back(max_chars);
 85    match truncation_ix {
 86        Some(length) => "".to_string() + &s[length..],
 87        None => s.to_string(),
 88    }
 89}
 90
 91/// Takes only `max_lines` from the string and, if there were more than `max_lines-1`, appends a
 92/// a newline and "..." to the string, so that `max_lines` are returned.
 93/// Returns string unchanged if its length is smaller than max_lines.
 94pub fn truncate_lines_and_trailoff(s: &str, max_lines: usize) -> String {
 95    let mut lines = s.lines().take(max_lines).collect::<Vec<_>>();
 96    if lines.len() > max_lines - 1 {
 97        lines.pop();
 98        lines.join("\n") + "\n"
 99    } else {
100        lines.join("\n")
101    }
102}
103
104pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
105    let prev = *value;
106    *value += T::from(1);
107    prev
108}
109
110/// Extend a sorted vector with a sorted sequence of items, maintaining the vector's sort order and
111/// enforcing a maximum length. This also de-duplicates items. Sort the items according to the given callback. Before calling this,
112/// both `vec` and `new_items` should already be sorted according to the `cmp` comparator.
113pub fn extend_sorted<T, I, F>(vec: &mut Vec<T>, new_items: I, limit: usize, mut cmp: F)
114where
115    I: IntoIterator<Item = T>,
116    F: FnMut(&T, &T) -> Ordering,
117{
118    let mut start_index = 0;
119    for new_item in new_items {
120        if let Err(i) = vec[start_index..].binary_search_by(|m| cmp(m, &new_item)) {
121            let index = start_index + i;
122            if vec.len() < limit {
123                vec.insert(index, new_item);
124            } else if index < vec.len() {
125                vec.pop();
126                vec.insert(index, new_item);
127            }
128            start_index = index;
129        }
130    }
131}
132
133pub fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
134    use serde_json::Value;
135
136    match (source, target) {
137        (Value::Object(source), Value::Object(target)) => {
138            for (key, value) in source {
139                if let Some(target) = target.get_mut(&key) {
140                    merge_json_value_into(value, target);
141                } else {
142                    target.insert(key.clone(), value);
143                }
144            }
145        }
146
147        (source, target) => *target = source,
148    }
149}
150
151pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
152    use serde_json::Value;
153    if let Value::Object(source_object) = source {
154        let target_object = if let Value::Object(target) = target {
155            target
156        } else {
157            *target = Value::Object(Default::default());
158            target.as_object_mut().unwrap()
159        };
160        for (key, value) in source_object {
161            if let Some(target) = target_object.get_mut(&key) {
162                merge_non_null_json_value_into(value, target);
163            } else if !value.is_null() {
164                target_object.insert(key.clone(), value);
165            }
166        }
167    } else if !source.is_null() {
168        *target = source
169    }
170}
171
172pub fn measure<R>(label: &str, f: impl FnOnce() -> R) -> R {
173    lazy_static! {
174        pub static ref ZED_MEASUREMENTS: bool = env::var("ZED_MEASUREMENTS")
175            .map(|measurements| measurements == "1" || measurements == "true")
176            .unwrap_or(false);
177    }
178
179    if *ZED_MEASUREMENTS {
180        let start = Instant::now();
181        let result = f();
182        let elapsed = start.elapsed();
183        eprintln!("{}: {:?}", label, elapsed);
184        result
185    } else {
186        f()
187    }
188}
189
190pub trait ResultExt<E> {
191    type Ok;
192
193    fn log_err(self) -> Option<Self::Ok>;
194    /// Assert that this result should never be an error in development or tests.
195    fn debug_assert_ok(self, reason: &str) -> Self;
196    fn warn_on_err(self) -> Option<Self::Ok>;
197    fn inspect_error(self, func: impl FnOnce(&E)) -> Self;
198}
199
200impl<T, E> ResultExt<E> for Result<T, E>
201where
202    E: std::fmt::Debug,
203{
204    type Ok = T;
205
206    #[track_caller]
207    fn log_err(self) -> Option<T> {
208        match self {
209            Ok(value) => Some(value),
210            Err(error) => {
211                let caller = Location::caller();
212                log::error!("{}:{}: {:?}", caller.file(), caller.line(), error);
213                None
214            }
215        }
216    }
217
218    #[track_caller]
219    fn debug_assert_ok(self, reason: &str) -> Self {
220        if let Err(error) = &self {
221            debug_panic!("{reason} - {error:?}");
222        }
223        self
224    }
225
226    fn warn_on_err(self) -> Option<T> {
227        match self {
228            Ok(value) => Some(value),
229            Err(error) => {
230                log::warn!("{:?}", error);
231                None
232            }
233        }
234    }
235
236    /// https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err
237    fn inspect_error(self, func: impl FnOnce(&E)) -> Self {
238        if let Err(err) = &self {
239            func(err);
240        }
241
242        self
243    }
244}
245
246pub trait TryFutureExt {
247    fn log_err(self) -> LogErrorFuture<Self>
248    where
249        Self: Sized;
250
251    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
252    where
253        Self: Sized;
254
255    fn warn_on_err(self) -> LogErrorFuture<Self>
256    where
257        Self: Sized;
258    fn unwrap(self) -> UnwrapFuture<Self>
259    where
260        Self: Sized;
261}
262
263impl<F, T, E> TryFutureExt for F
264where
265    F: Future<Output = Result<T, E>>,
266    E: std::fmt::Debug,
267{
268    #[track_caller]
269    fn log_err(self) -> LogErrorFuture<Self>
270    where
271        Self: Sized,
272    {
273        let location = Location::caller();
274        LogErrorFuture(self, log::Level::Error, *location)
275    }
276
277    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
278    where
279        Self: Sized,
280    {
281        LogErrorFuture(self, log::Level::Error, location)
282    }
283
284    #[track_caller]
285    fn warn_on_err(self) -> LogErrorFuture<Self>
286    where
287        Self: Sized,
288    {
289        let location = Location::caller();
290        LogErrorFuture(self, log::Level::Warn, *location)
291    }
292
293    fn unwrap(self) -> UnwrapFuture<Self>
294    where
295        Self: Sized,
296    {
297        UnwrapFuture(self)
298    }
299}
300
301#[must_use]
302pub struct LogErrorFuture<F>(F, log::Level, core::panic::Location<'static>);
303
304impl<F, T, E> Future for LogErrorFuture<F>
305where
306    F: Future<Output = Result<T, E>>,
307    E: std::fmt::Debug,
308{
309    type Output = Option<T>;
310
311    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
312        let level = self.1;
313        let location = self.2;
314        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
315        match inner.poll(cx) {
316            Poll::Ready(output) => Poll::Ready(match output {
317                Ok(output) => Some(output),
318                Err(error) => {
319                    log::log!(
320                        level,
321                        "{}:{}: {:?}",
322                        location.file(),
323                        location.line(),
324                        error
325                    );
326                    None
327                }
328            }),
329            Poll::Pending => Poll::Pending,
330        }
331    }
332}
333
334pub struct UnwrapFuture<F>(F);
335
336impl<F, T, E> Future for UnwrapFuture<F>
337where
338    F: Future<Output = Result<T, E>>,
339    E: std::fmt::Debug,
340{
341    type Output = T;
342
343    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
344        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
345        match inner.poll(cx) {
346            Poll::Ready(result) => Poll::Ready(result.unwrap()),
347            Poll::Pending => Poll::Pending,
348        }
349    }
350}
351
352pub struct Deferred<F: FnOnce()>(Option<F>);
353
354impl<F: FnOnce()> Deferred<F> {
355    /// Drop without running the deferred function.
356    pub fn abort(mut self) {
357        self.0.take();
358    }
359}
360
361impl<F: FnOnce()> Drop for Deferred<F> {
362    fn drop(&mut self) {
363        if let Some(f) = self.0.take() {
364            f()
365        }
366    }
367}
368
369/// Run the given function when the returned value is dropped (unless it's cancelled).
370pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
371    Deferred(Some(f))
372}
373
374pub struct RandomCharIter<T: Rng> {
375    rng: T,
376    simple_text: bool,
377}
378
379impl<T: Rng> RandomCharIter<T> {
380    pub fn new(rng: T) -> Self {
381        Self {
382            rng,
383            simple_text: std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()),
384        }
385    }
386
387    pub fn with_simple_text(mut self) -> Self {
388        self.simple_text = true;
389        self
390    }
391}
392
393impl<T: Rng> Iterator for RandomCharIter<T> {
394    type Item = char;
395
396    fn next(&mut self) -> Option<Self::Item> {
397        if self.simple_text {
398            return if self.rng.gen_range(0..100) < 5 {
399                Some('\n')
400            } else {
401                Some(self.rng.gen_range(b'a'..b'z' + 1).into())
402            };
403        }
404
405        match self.rng.gen_range(0..100) {
406            // whitespace
407            0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.rng).copied(),
408            // two-byte greek letters
409            20..=32 => char::from_u32(self.rng.gen_range(('α' as u32)..('ω' as u32 + 1))),
410            // // three-byte characters
411            33..=45 => ['✋', '✅', '❌', '❎', '⭐']
412                .choose(&mut self.rng)
413                .copied(),
414            // // four-byte characters
415            46..=58 => ['🍐', '🏀', '🍗', '🎉'].choose(&mut self.rng).copied(),
416            // ascii letters
417            _ => Some(self.rng.gen_range(b'a'..b'z' + 1).into()),
418        }
419    }
420}
421
422/// Get an embedded file as a string.
423pub fn asset_str<A: rust_embed::RustEmbed>(path: &str) -> Cow<'static, str> {
424    match A::get(path).unwrap().data {
425        Cow::Borrowed(bytes) => Cow::Borrowed(std::str::from_utf8(bytes).unwrap()),
426        Cow::Owned(bytes) => Cow::Owned(String::from_utf8(bytes).unwrap()),
427    }
428}
429
430// copy unstable standard feature option unzip
431// https://github.com/rust-lang/rust/issues/87800
432// Remove when this ship in Rust 1.66 or 1.67
433pub fn unzip_option<T, U>(option: Option<(T, U)>) -> (Option<T>, Option<U>) {
434    match option {
435        Some((a, b)) => (Some(a), Some(b)),
436        None => (None, None),
437    }
438}
439
440/// Expands to an immediately-invoked function expression. Good for using the ? operator
441/// in functions which do not return an Option or Result.
442///
443/// Accepts a normal block, an async block, or an async move block.
444#[macro_export]
445macro_rules! maybe {
446    ($block:block) => {
447        (|| $block)()
448    };
449    (async $block:block) => {
450        (|| async $block)()
451    };
452    (async move $block:block) => {
453        (|| async move $block)()
454    };
455}
456
457pub trait RangeExt<T> {
458    fn sorted(&self) -> Self;
459    fn to_inclusive(&self) -> RangeInclusive<T>;
460    fn overlaps(&self, other: &Range<T>) -> bool;
461    fn contains_inclusive(&self, other: &Range<T>) -> bool;
462}
463
464impl<T: Ord + Clone> RangeExt<T> for Range<T> {
465    fn sorted(&self) -> Self {
466        cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
467    }
468
469    fn to_inclusive(&self) -> RangeInclusive<T> {
470        self.start.clone()..=self.end.clone()
471    }
472
473    fn overlaps(&self, other: &Range<T>) -> bool {
474        self.start < other.end && other.start < self.end
475    }
476
477    fn contains_inclusive(&self, other: &Range<T>) -> bool {
478        self.start <= other.start && other.end <= self.end
479    }
480}
481
482impl<T: Ord + Clone> RangeExt<T> for RangeInclusive<T> {
483    fn sorted(&self) -> Self {
484        cmp::min(self.start(), self.end()).clone()..=cmp::max(self.start(), self.end()).clone()
485    }
486
487    fn to_inclusive(&self) -> RangeInclusive<T> {
488        self.clone()
489    }
490
491    fn overlaps(&self, other: &Range<T>) -> bool {
492        self.start() < &other.end && &other.start <= self.end()
493    }
494
495    fn contains_inclusive(&self, other: &Range<T>) -> bool {
496        self.start() <= &other.start && &other.end <= self.end()
497    }
498}
499
500/// A way to sort strings with starting numbers numerically first, falling back to alphanumeric one,
501/// case-insensitive.
502///
503/// This is useful for turning regular alphanumerically sorted sequences as `1-abc, 10, 11-def, .., 2, 21-abc`
504/// into `1-abc, 2, 10, 11-def, .., 21-abc`
505#[derive(Debug, PartialEq, Eq)]
506pub struct NumericPrefixWithSuffix<'a>(i32, &'a str);
507
508impl<'a> NumericPrefixWithSuffix<'a> {
509    pub fn from_numeric_prefixed_str(str: &'a str) -> Option<Self> {
510        let mut chars = str.chars();
511        let prefix: String = chars.by_ref().take_while(|c| c.is_ascii_digit()).collect();
512        let remainder = chars.as_str();
513
514        match prefix.parse::<i32>() {
515            Ok(prefix) => Some(NumericPrefixWithSuffix(prefix, remainder)),
516            Err(_) => None,
517        }
518    }
519}
520
521impl Ord for NumericPrefixWithSuffix<'_> {
522    fn cmp(&self, other: &Self) -> Ordering {
523        let NumericPrefixWithSuffix(num_a, remainder_a) = self;
524        let NumericPrefixWithSuffix(num_b, remainder_b) = other;
525        num_a
526            .cmp(num_b)
527            .then_with(|| UniCase::new(remainder_a).cmp(&UniCase::new(remainder_b)))
528    }
529}
530
531impl<'a> PartialOrd for NumericPrefixWithSuffix<'a> {
532    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
533        Some(self.cmp(other))
534    }
535}
536lazy_static! {
537    static ref EMOJI_REGEX: regex::Regex = regex::Regex::new("(\\p{Emoji}|\u{200D})").unwrap();
538}
539
540/// Returns true if the given string consists of emojis only.
541/// E.g. "👨‍👩‍👧‍👧👋" will return true, but "👋!" will return false.
542pub fn word_consists_of_emojis(s: &str) -> bool {
543    let mut prev_end = 0;
544    for capture in EMOJI_REGEX.find_iter(s) {
545        if capture.start() != prev_end {
546            return false;
547        }
548        prev_end = capture.end();
549    }
550    prev_end == s.len()
551}
552
553#[cfg(test)]
554mod tests {
555    use super::*;
556
557    #[test]
558    fn test_extend_sorted() {
559        let mut vec = vec![];
560
561        extend_sorted(&mut vec, vec![21, 17, 13, 8, 1, 0], 5, |a, b| b.cmp(a));
562        assert_eq!(vec, &[21, 17, 13, 8, 1]);
563
564        extend_sorted(&mut vec, vec![101, 19, 17, 8, 2], 8, |a, b| b.cmp(a));
565        assert_eq!(vec, &[101, 21, 19, 17, 13, 8, 2, 1]);
566
567        extend_sorted(&mut vec, vec![1000, 19, 17, 9, 5], 8, |a, b| b.cmp(a));
568        assert_eq!(vec, &[1000, 101, 21, 19, 17, 13, 9, 8]);
569    }
570
571    #[test]
572    fn test_iife() {
573        fn option_returning_function() -> Option<()> {
574            None
575        }
576
577        let foo = maybe!({
578            option_returning_function()?;
579            Some(())
580        });
581
582        assert_eq!(foo, None);
583    }
584
585    #[test]
586    fn test_trancate_and_trailoff() {
587        assert_eq!(truncate_and_trailoff("", 5), "");
588        assert_eq!(truncate_and_trailoff("èèèèèè", 7), "èèèèèè");
589        assert_eq!(truncate_and_trailoff("èèèèèè", 6), "èèèèèè");
590        assert_eq!(truncate_and_trailoff("èèèèèè", 5), "èèèèè…");
591    }
592
593    #[test]
594    fn test_numeric_prefix_with_suffix() {
595        let mut sorted = vec!["1-abc", "10", "11def", "2", "21-abc"];
596        sorted.sort_by_key(|s| {
597            NumericPrefixWithSuffix::from_numeric_prefixed_str(s).unwrap_or_else(|| {
598                panic!("Cannot convert string `{s}` into NumericPrefixWithSuffix")
599            })
600        });
601        assert_eq!(sorted, ["1-abc", "2", "10", "11def", "21-abc"]);
602
603        for numeric_prefix_less in ["numeric_prefix_less", "aaa", "~™£"] {
604            assert_eq!(
605                NumericPrefixWithSuffix::from_numeric_prefixed_str(numeric_prefix_less),
606                None,
607                "String without numeric prefix `{numeric_prefix_less}` should not be converted into NumericPrefixWithSuffix"
608            )
609        }
610    }
611
612    #[test]
613    fn test_word_consists_of_emojis() {
614        let words_to_test = vec![
615            ("👨‍👩‍👧‍👧👋🥒", true),
616            ("👋", true),
617            ("!👋", false),
618            ("👋!", false),
619            ("👋 ", false),
620            (" 👋", false),
621            ("Test", false),
622        ];
623
624        for (text, expected_result) in words_to_test {
625            assert_eq!(word_consists_of_emojis(text), expected_result);
626        }
627    }
628
629    #[test]
630    fn test_truncate_lines_and_trailoff() {
631        let text = r#"Line 1
632Line 2
633Line 3"#;
634
635        assert_eq!(
636            truncate_lines_and_trailoff(text, 2),
637            r#"Line 1
638…"#
639        );
640
641        assert_eq!(
642            truncate_lines_and_trailoff(text, 3),
643            r#"Line 1
644Line 2
645…"#
646        );
647
648        assert_eq!(
649            truncate_lines_and_trailoff(text, 4),
650            r#"Line 1
651Line 2
652Line 3"#
653        );
654    }
655}