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).
370#[must_use]
371pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
372    Deferred(Some(f))
373}
374
375pub struct RandomCharIter<T: Rng> {
376    rng: T,
377    simple_text: bool,
378}
379
380impl<T: Rng> RandomCharIter<T> {
381    pub fn new(rng: T) -> Self {
382        Self {
383            rng,
384            simple_text: std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()),
385        }
386    }
387
388    pub fn with_simple_text(mut self) -> Self {
389        self.simple_text = true;
390        self
391    }
392}
393
394impl<T: Rng> Iterator for RandomCharIter<T> {
395    type Item = char;
396
397    fn next(&mut self) -> Option<Self::Item> {
398        if self.simple_text {
399            return if self.rng.gen_range(0..100) < 5 {
400                Some('\n')
401            } else {
402                Some(self.rng.gen_range(b'a'..b'z' + 1).into())
403            };
404        }
405
406        match self.rng.gen_range(0..100) {
407            // whitespace
408            0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.rng).copied(),
409            // two-byte greek letters
410            20..=32 => char::from_u32(self.rng.gen_range(('α' as u32)..('ω' as u32 + 1))),
411            // // three-byte characters
412            33..=45 => ['✋', '✅', '❌', '❎', '⭐']
413                .choose(&mut self.rng)
414                .copied(),
415            // // four-byte characters
416            46..=58 => ['🍐', '🏀', '🍗', '🎉'].choose(&mut self.rng).copied(),
417            // ascii letters
418            _ => Some(self.rng.gen_range(b'a'..b'z' + 1).into()),
419        }
420    }
421}
422
423/// Get an embedded file as a string.
424pub fn asset_str<A: rust_embed::RustEmbed>(path: &str) -> Cow<'static, str> {
425    match A::get(path).unwrap().data {
426        Cow::Borrowed(bytes) => Cow::Borrowed(std::str::from_utf8(bytes).unwrap()),
427        Cow::Owned(bytes) => Cow::Owned(String::from_utf8(bytes).unwrap()),
428    }
429}
430
431// copy unstable standard feature option unzip
432// https://github.com/rust-lang/rust/issues/87800
433// Remove when this ship in Rust 1.66 or 1.67
434pub fn unzip_option<T, U>(option: Option<(T, U)>) -> (Option<T>, Option<U>) {
435    match option {
436        Some((a, b)) => (Some(a), Some(b)),
437        None => (None, None),
438    }
439}
440
441/// Expands to an immediately-invoked function expression. Good for using the ? operator
442/// in functions which do not return an Option or Result.
443///
444/// Accepts a normal block, an async block, or an async move block.
445#[macro_export]
446macro_rules! maybe {
447    ($block:block) => {
448        (|| $block)()
449    };
450    (async $block:block) => {
451        (|| async $block)()
452    };
453    (async move $block:block) => {
454        (|| async move $block)()
455    };
456}
457
458pub trait RangeExt<T> {
459    fn sorted(&self) -> Self;
460    fn to_inclusive(&self) -> RangeInclusive<T>;
461    fn overlaps(&self, other: &Range<T>) -> bool;
462    fn contains_inclusive(&self, other: &Range<T>) -> bool;
463}
464
465impl<T: Ord + Clone> RangeExt<T> for Range<T> {
466    fn sorted(&self) -> Self {
467        cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
468    }
469
470    fn to_inclusive(&self) -> RangeInclusive<T> {
471        self.start.clone()..=self.end.clone()
472    }
473
474    fn overlaps(&self, other: &Range<T>) -> bool {
475        self.start < other.end && other.start < self.end
476    }
477
478    fn contains_inclusive(&self, other: &Range<T>) -> bool {
479        self.start <= other.start && other.end <= self.end
480    }
481}
482
483impl<T: Ord + Clone> RangeExt<T> for RangeInclusive<T> {
484    fn sorted(&self) -> Self {
485        cmp::min(self.start(), self.end()).clone()..=cmp::max(self.start(), self.end()).clone()
486    }
487
488    fn to_inclusive(&self) -> RangeInclusive<T> {
489        self.clone()
490    }
491
492    fn overlaps(&self, other: &Range<T>) -> bool {
493        self.start() < &other.end && &other.start <= self.end()
494    }
495
496    fn contains_inclusive(&self, other: &Range<T>) -> bool {
497        self.start() <= &other.start && &other.end <= self.end()
498    }
499}
500
501/// A way to sort strings with starting numbers numerically first, falling back to alphanumeric one,
502/// case-insensitive.
503///
504/// This is useful for turning regular alphanumerically sorted sequences as `1-abc, 10, 11-def, .., 2, 21-abc`
505/// into `1-abc, 2, 10, 11-def, .., 21-abc`
506#[derive(Debug, PartialEq, Eq)]
507pub struct NumericPrefixWithSuffix<'a>(i32, &'a str);
508
509impl<'a> NumericPrefixWithSuffix<'a> {
510    pub fn from_numeric_prefixed_str(str: &'a str) -> Option<Self> {
511        let mut chars = str.chars();
512        let prefix: String = chars.by_ref().take_while(|c| c.is_ascii_digit()).collect();
513        let remainder = chars.as_str();
514
515        match prefix.parse::<i32>() {
516            Ok(prefix) => Some(NumericPrefixWithSuffix(prefix, remainder)),
517            Err(_) => None,
518        }
519    }
520}
521
522impl Ord for NumericPrefixWithSuffix<'_> {
523    fn cmp(&self, other: &Self) -> Ordering {
524        let NumericPrefixWithSuffix(num_a, remainder_a) = self;
525        let NumericPrefixWithSuffix(num_b, remainder_b) = other;
526        num_a
527            .cmp(num_b)
528            .then_with(|| UniCase::new(remainder_a).cmp(&UniCase::new(remainder_b)))
529    }
530}
531
532impl<'a> PartialOrd for NumericPrefixWithSuffix<'a> {
533    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
534        Some(self.cmp(other))
535    }
536}
537lazy_static! {
538    static ref EMOJI_REGEX: regex::Regex = regex::Regex::new("(\\p{Emoji}|\u{200D})").unwrap();
539}
540
541/// Returns true if the given string consists of emojis only.
542/// E.g. "👨‍👩‍👧‍👧👋" will return true, but "👋!" will return false.
543pub fn word_consists_of_emojis(s: &str) -> bool {
544    let mut prev_end = 0;
545    for capture in EMOJI_REGEX.find_iter(s) {
546        if capture.start() != prev_end {
547            return false;
548        }
549        prev_end = capture.end();
550    }
551    prev_end == s.len()
552}
553
554#[cfg(test)]
555mod tests {
556    use super::*;
557
558    #[test]
559    fn test_extend_sorted() {
560        let mut vec = vec![];
561
562        extend_sorted(&mut vec, vec![21, 17, 13, 8, 1, 0], 5, |a, b| b.cmp(a));
563        assert_eq!(vec, &[21, 17, 13, 8, 1]);
564
565        extend_sorted(&mut vec, vec![101, 19, 17, 8, 2], 8, |a, b| b.cmp(a));
566        assert_eq!(vec, &[101, 21, 19, 17, 13, 8, 2, 1]);
567
568        extend_sorted(&mut vec, vec![1000, 19, 17, 9, 5], 8, |a, b| b.cmp(a));
569        assert_eq!(vec, &[1000, 101, 21, 19, 17, 13, 9, 8]);
570    }
571
572    #[test]
573    fn test_iife() {
574        fn option_returning_function() -> Option<()> {
575            None
576        }
577
578        let foo = maybe!({
579            option_returning_function()?;
580            Some(())
581        });
582
583        assert_eq!(foo, None);
584    }
585
586    #[test]
587    fn test_trancate_and_trailoff() {
588        assert_eq!(truncate_and_trailoff("", 5), "");
589        assert_eq!(truncate_and_trailoff("èèèèèè", 7), "èèèèèè");
590        assert_eq!(truncate_and_trailoff("èèèèèè", 6), "èèèèèè");
591        assert_eq!(truncate_and_trailoff("èèèèèè", 5), "èèèèè…");
592    }
593
594    #[test]
595    fn test_numeric_prefix_with_suffix() {
596        let mut sorted = vec!["1-abc", "10", "11def", "2", "21-abc"];
597        sorted.sort_by_key(|s| {
598            NumericPrefixWithSuffix::from_numeric_prefixed_str(s).unwrap_or_else(|| {
599                panic!("Cannot convert string `{s}` into NumericPrefixWithSuffix")
600            })
601        });
602        assert_eq!(sorted, ["1-abc", "2", "10", "11def", "21-abc"]);
603
604        for numeric_prefix_less in ["numeric_prefix_less", "aaa", "~™£"] {
605            assert_eq!(
606                NumericPrefixWithSuffix::from_numeric_prefixed_str(numeric_prefix_less),
607                None,
608                "String without numeric prefix `{numeric_prefix_less}` should not be converted into NumericPrefixWithSuffix"
609            )
610        }
611    }
612
613    #[test]
614    fn test_word_consists_of_emojis() {
615        let words_to_test = vec![
616            ("👨‍👩‍👧‍👧👋🥒", true),
617            ("👋", true),
618            ("!👋", false),
619            ("👋!", false),
620            ("👋 ", false),
621            (" 👋", false),
622            ("Test", false),
623        ];
624
625        for (text, expected_result) in words_to_test {
626            assert_eq!(word_consists_of_emojis(text), expected_result);
627        }
628    }
629
630    #[test]
631    fn test_truncate_lines_and_trailoff() {
632        let text = r#"Line 1
633Line 2
634Line 3"#;
635
636        assert_eq!(
637            truncate_lines_and_trailoff(text, 2),
638            r#"Line 1
639…"#
640        );
641
642        assert_eq!(
643            truncate_lines_and_trailoff(text, 3),
644            r#"Line 1
645Line 2
646…"#
647        );
648
649        assert_eq!(
650            truncate_lines_and_trailoff(text, 4),
651            r#"Line 1
652Line 2
653Line 3"#
654        );
655    }
656}