util.rs

  1pub mod arc_cow;
  2pub mod command;
  3pub mod fs;
  4pub mod paths;
  5pub mod serde;
  6#[cfg(any(test, feature = "test-support"))]
  7pub mod test;
  8
  9use anyhow::{anyhow, Context as _, Result};
 10use futures::Future;
 11use itertools::Either;
 12use regex::Regex;
 13use std::sync::{LazyLock, OnceLock};
 14use std::{
 15    borrow::Cow,
 16    cmp::{self, Ordering},
 17    env,
 18    ops::{AddAssign, Range, RangeInclusive},
 19    panic::Location,
 20    pin::Pin,
 21    task::{Context, Poll},
 22    time::Instant,
 23};
 24use unicase::UniCase;
 25
 26pub use take_until::*;
 27
 28#[macro_export]
 29macro_rules! debug_panic {
 30    ( $($fmt_arg:tt)* ) => {
 31        if cfg!(debug_assertions) {
 32            panic!( $($fmt_arg)* );
 33        } else {
 34            let backtrace = std::backtrace::Backtrace::capture();
 35            log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
 36        }
 37    };
 38}
 39
 40pub fn truncate(s: &str, max_chars: usize) -> &str {
 41    match s.char_indices().nth(max_chars) {
 42        None => s,
 43        Some((idx, _)) => &s[..idx],
 44    }
 45}
 46
 47/// Removes characters from the end of the string if its length is greater than `max_chars` and
 48/// appends "..." to the string. Returns string unchanged if its length is smaller than max_chars.
 49pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {
 50    debug_assert!(max_chars >= 5);
 51
 52    let truncation_ix = s.char_indices().map(|(i, _)| i).nth(max_chars);
 53    match truncation_ix {
 54        Some(length) => s[..length].to_string() + "",
 55        None => s.to_string(),
 56    }
 57}
 58
 59/// Removes characters from the front of the string if its length is greater than `max_chars` and
 60/// prepends the string with "...". Returns string unchanged if its length is smaller than max_chars.
 61pub fn truncate_and_remove_front(s: &str, max_chars: usize) -> String {
 62    debug_assert!(max_chars >= 5);
 63
 64    let truncation_ix = s.char_indices().map(|(i, _)| i).nth_back(max_chars);
 65    match truncation_ix {
 66        Some(length) => "".to_string() + &s[length..],
 67        None => s.to_string(),
 68    }
 69}
 70
 71/// Takes only `max_lines` from the string and, if there were more than `max_lines-1`, appends a
 72/// a newline and "..." to the string, so that `max_lines` are returned.
 73/// Returns string unchanged if its length is smaller than max_lines.
 74pub fn truncate_lines_and_trailoff(s: &str, max_lines: usize) -> String {
 75    let mut lines = s.lines().take(max_lines).collect::<Vec<_>>();
 76    if lines.len() > max_lines - 1 {
 77        lines.pop();
 78        lines.join("\n") + "\n"
 79    } else {
 80        lines.join("\n")
 81    }
 82}
 83
 84pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
 85    let prev = *value;
 86    *value += T::from(1);
 87    prev
 88}
 89
 90/// Extend a sorted vector with a sorted sequence of items, maintaining the vector's sort order and
 91/// enforcing a maximum length. This also de-duplicates items. Sort the items according to the given callback. Before calling this,
 92/// both `vec` and `new_items` should already be sorted according to the `cmp` comparator.
 93pub fn extend_sorted<T, I, F>(vec: &mut Vec<T>, new_items: I, limit: usize, mut cmp: F)
 94where
 95    I: IntoIterator<Item = T>,
 96    F: FnMut(&T, &T) -> Ordering,
 97{
 98    let mut start_index = 0;
 99    for new_item in new_items {
100        if let Err(i) = vec[start_index..].binary_search_by(|m| cmp(m, &new_item)) {
101            let index = start_index + i;
102            if vec.len() < limit {
103                vec.insert(index, new_item);
104            } else if index < vec.len() {
105                vec.pop();
106                vec.insert(index, new_item);
107            }
108            start_index = index;
109        }
110    }
111}
112
113pub fn truncate_to_bottom_n_sorted_by<T, F>(items: &mut Vec<T>, limit: usize, compare: &F)
114where
115    F: Fn(&T, &T) -> Ordering,
116{
117    if limit == 0 {
118        items.truncate(0);
119    }
120    if items.len() < limit {
121        return;
122    }
123    // When limit is near to items.len() it may be more efficient to sort the whole list and
124    // truncate, rather than always doing selection first as is done below. It's hard to analyze
125    // where the threshold for this should be since the quickselect style algorithm used by
126    // `select_nth_unstable_by` makes the prefix partially sorted, and so its work is not wasted -
127    // the expected number of comparisons needed by `sort_by` is less than it is for some arbitrary
128    // unsorted input.
129    items.select_nth_unstable_by(limit, compare);
130    items.truncate(limit);
131    items.sort_by(compare);
132}
133
134#[cfg(unix)]
135pub fn load_shell_from_passwd() -> Result<()> {
136    let buflen = match unsafe { libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) } {
137        n if n < 0 => 1024,
138        n => n as usize,
139    };
140    let mut buffer = Vec::with_capacity(buflen);
141
142    let mut pwd: std::mem::MaybeUninit<libc::passwd> = std::mem::MaybeUninit::uninit();
143    let mut result: *mut libc::passwd = std::ptr::null_mut();
144
145    let uid = unsafe { libc::getuid() };
146    let status = unsafe {
147        libc::getpwuid_r(
148            uid,
149            pwd.as_mut_ptr(),
150            buffer.as_mut_ptr() as *mut libc::c_char,
151            buflen,
152            &mut result,
153        )
154    };
155    let entry = unsafe { pwd.assume_init() };
156
157    anyhow::ensure!(
158        status == 0,
159        "call to getpwuid_r failed. uid: {}, status: {}",
160        uid,
161        status
162    );
163    anyhow::ensure!(!result.is_null(), "passwd entry for uid {} not found", uid);
164    anyhow::ensure!(
165        entry.pw_uid == uid,
166        "passwd entry has different uid ({}) than getuid ({}) returned",
167        entry.pw_uid,
168        uid,
169    );
170
171    let shell = unsafe { std::ffi::CStr::from_ptr(entry.pw_shell).to_str().unwrap() };
172    if env::var("SHELL").map_or(true, |shell_env| shell_env != shell) {
173        log::info!(
174            "updating SHELL environment variable to value from passwd entry: {:?}",
175            shell,
176        );
177        env::set_var("SHELL", shell);
178    }
179
180    Ok(())
181}
182
183pub fn load_login_shell_environment() -> Result<()> {
184    let marker = "ZED_LOGIN_SHELL_START";
185    let shell = env::var("SHELL").context(
186        "SHELL environment variable is not assigned so we can't source login environment variables",
187    )?;
188
189    // If possible, we want to `cd` in the user's `$HOME` to trigger programs
190    // such as direnv, asdf, mise, ... to adjust the PATH. These tools often hook
191    // into shell's `cd` command (and hooks) to manipulate env.
192    // We do this so that we get the env a user would have when spawning a shell
193    // in home directory.
194    let shell_cmd_prefix = std::env::var_os("HOME")
195        .and_then(|home| home.into_string().ok())
196        .map(|home| format!("cd '{home}';"));
197
198    // The `exit 0` is the result of hours of debugging, trying to find out
199    // why running this command here, without `exit 0`, would mess
200    // up signal process for our process so that `ctrl-c` doesn't work
201    // anymore.
202    // We still don't know why `$SHELL -l -i -c '/usr/bin/env -0'`  would
203    // do that, but it does, and `exit 0` helps.
204    let shell_cmd = format!(
205        "{}printf '%s' {marker}; /usr/bin/env; exit 0;",
206        shell_cmd_prefix.as_deref().unwrap_or("")
207    );
208
209    let output = std::process::Command::new(&shell)
210        .args(["-l", "-i", "-c", &shell_cmd])
211        .output()
212        .context("failed to spawn login shell to source login environment variables")?;
213    if !output.status.success() {
214        Err(anyhow!("login shell exited with error"))?;
215    }
216
217    let stdout = String::from_utf8_lossy(&output.stdout);
218
219    if let Some(env_output_start) = stdout.find(marker) {
220        let env_output = &stdout[env_output_start + marker.len()..];
221
222        parse_env_output(env_output, |key, value| env::set_var(key, value));
223
224        log::info!(
225            "set environment variables from shell:{}, path:{}",
226            shell,
227            env::var("PATH").unwrap_or_default(),
228        );
229    }
230
231    Ok(())
232}
233
234/// Parse the result of calling `usr/bin/env` with no arguments
235pub fn parse_env_output(env: &str, mut f: impl FnMut(String, String)) {
236    let mut current_key: Option<String> = None;
237    let mut current_value: Option<String> = None;
238
239    for line in env.split_terminator('\n') {
240        if let Some(separator_index) = line.find('=') {
241            if !line[..separator_index].is_empty() {
242                if let Some((key, value)) = Option::zip(current_key.take(), current_value.take()) {
243                    f(key, value)
244                }
245                current_key = Some(line[..separator_index].to_string());
246                current_value = Some(line[separator_index + 1..].to_string());
247                continue;
248            };
249        }
250        if let Some(value) = current_value.as_mut() {
251            value.push('\n');
252            value.push_str(line);
253        }
254    }
255    if let Some((key, value)) = Option::zip(current_key.take(), current_value.take()) {
256        f(key, value)
257    }
258}
259
260pub fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
261    use serde_json::Value;
262
263    match (source, target) {
264        (Value::Object(source), Value::Object(target)) => {
265            for (key, value) in source {
266                if let Some(target) = target.get_mut(&key) {
267                    merge_json_value_into(value, target);
268                } else {
269                    target.insert(key.clone(), value);
270                }
271            }
272        }
273
274        (Value::Array(source), Value::Array(target)) => {
275            for value in source {
276                target.push(value);
277            }
278        }
279
280        (source, target) => *target = source,
281    }
282}
283
284pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
285    use serde_json::Value;
286    if let Value::Object(source_object) = source {
287        let target_object = if let Value::Object(target) = target {
288            target
289        } else {
290            *target = Value::Object(Default::default());
291            target.as_object_mut().unwrap()
292        };
293        for (key, value) in source_object {
294            if let Some(target) = target_object.get_mut(&key) {
295                merge_non_null_json_value_into(value, target);
296            } else if !value.is_null() {
297                target_object.insert(key.clone(), value);
298            }
299        }
300    } else if !source.is_null() {
301        *target = source
302    }
303}
304
305pub fn measure<R>(label: &str, f: impl FnOnce() -> R) -> R {
306    static ZED_MEASUREMENTS: OnceLock<bool> = OnceLock::new();
307    let zed_measurements = ZED_MEASUREMENTS.get_or_init(|| {
308        env::var("ZED_MEASUREMENTS")
309            .map(|measurements| measurements == "1" || measurements == "true")
310            .unwrap_or(false)
311    });
312
313    if *zed_measurements {
314        let start = Instant::now();
315        let result = f();
316        let elapsed = start.elapsed();
317        eprintln!("{}: {:?}", label, elapsed);
318        result
319    } else {
320        f()
321    }
322}
323
324pub fn iterate_expanded_and_wrapped_usize_range(
325    range: Range<usize>,
326    additional_before: usize,
327    additional_after: usize,
328    wrap_length: usize,
329) -> impl Iterator<Item = usize> {
330    let start_wraps = range.start < additional_before;
331    let end_wraps = wrap_length < range.end + additional_after;
332    if start_wraps && end_wraps {
333        Either::Left(0..wrap_length)
334    } else if start_wraps {
335        let wrapped_start = (range.start + wrap_length).saturating_sub(additional_before);
336        if wrapped_start <= range.end {
337            Either::Left(0..wrap_length)
338        } else {
339            Either::Right((0..range.end + additional_after).chain(wrapped_start..wrap_length))
340        }
341    } else if end_wraps {
342        let wrapped_end = range.end + additional_after - wrap_length;
343        if range.start <= wrapped_end {
344            Either::Left(0..wrap_length)
345        } else {
346            Either::Right((0..wrapped_end).chain(range.start - additional_before..wrap_length))
347        }
348    } else {
349        Either::Left((range.start - additional_before)..(range.end + additional_after))
350    }
351}
352
353pub trait ResultExt<E> {
354    type Ok;
355
356    fn log_err(self) -> Option<Self::Ok>;
357    /// Assert that this result should never be an error in development or tests.
358    fn debug_assert_ok(self, reason: &str) -> Self;
359    fn warn_on_err(self) -> Option<Self::Ok>;
360    fn anyhow(self) -> anyhow::Result<Self::Ok>
361    where
362        E: Into<anyhow::Error>;
363}
364
365impl<T, E> ResultExt<E> for Result<T, E>
366where
367    E: std::fmt::Debug,
368{
369    type Ok = T;
370
371    #[track_caller]
372    fn log_err(self) -> Option<T> {
373        match self {
374            Ok(value) => Some(value),
375            Err(error) => {
376                log_error_with_caller(*Location::caller(), error, log::Level::Error);
377                None
378            }
379        }
380    }
381
382    #[track_caller]
383    fn debug_assert_ok(self, reason: &str) -> Self {
384        if let Err(error) = &self {
385            debug_panic!("{reason} - {error:?}");
386        }
387        self
388    }
389
390    #[track_caller]
391    fn warn_on_err(self) -> Option<T> {
392        match self {
393            Ok(value) => Some(value),
394            Err(error) => {
395                log_error_with_caller(*Location::caller(), error, log::Level::Warn);
396                None
397            }
398        }
399    }
400
401    fn anyhow(self) -> anyhow::Result<T>
402    where
403        E: Into<anyhow::Error>,
404    {
405        self.map_err(Into::into)
406    }
407}
408
409fn log_error_with_caller<E>(caller: core::panic::Location<'_>, error: E, level: log::Level)
410where
411    E: std::fmt::Debug,
412{
413    #[cfg(not(target_os = "windows"))]
414    let file = caller.file();
415    #[cfg(target_os = "windows")]
416    let file = caller.file().replace('\\', "/");
417    // In this codebase, the first segment of the file path is
418    // the 'crates' folder, followed by the crate name.
419    let target = file.split('/').nth(1);
420
421    log::logger().log(
422        &log::Record::builder()
423            .target(target.unwrap_or(""))
424            .module_path(target)
425            .args(format_args!("{:?}", error))
426            .file(Some(caller.file()))
427            .line(Some(caller.line()))
428            .level(level)
429            .build(),
430    );
431}
432
433pub trait TryFutureExt {
434    fn log_err(self) -> LogErrorFuture<Self>
435    where
436        Self: Sized;
437
438    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
439    where
440        Self: Sized;
441
442    fn warn_on_err(self) -> LogErrorFuture<Self>
443    where
444        Self: Sized;
445    fn unwrap(self) -> UnwrapFuture<Self>
446    where
447        Self: Sized;
448}
449
450impl<F, T, E> TryFutureExt for F
451where
452    F: Future<Output = Result<T, E>>,
453    E: std::fmt::Debug,
454{
455    #[track_caller]
456    fn log_err(self) -> LogErrorFuture<Self>
457    where
458        Self: Sized,
459    {
460        let location = Location::caller();
461        LogErrorFuture(self, log::Level::Error, *location)
462    }
463
464    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
465    where
466        Self: Sized,
467    {
468        LogErrorFuture(self, log::Level::Error, location)
469    }
470
471    #[track_caller]
472    fn warn_on_err(self) -> LogErrorFuture<Self>
473    where
474        Self: Sized,
475    {
476        let location = Location::caller();
477        LogErrorFuture(self, log::Level::Warn, *location)
478    }
479
480    fn unwrap(self) -> UnwrapFuture<Self>
481    where
482        Self: Sized,
483    {
484        UnwrapFuture(self)
485    }
486}
487
488#[must_use]
489pub struct LogErrorFuture<F>(F, log::Level, core::panic::Location<'static>);
490
491impl<F, T, E> Future for LogErrorFuture<F>
492where
493    F: Future<Output = Result<T, E>>,
494    E: std::fmt::Debug,
495{
496    type Output = Option<T>;
497
498    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
499        let level = self.1;
500        let location = self.2;
501        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
502        match inner.poll(cx) {
503            Poll::Ready(output) => Poll::Ready(match output {
504                Ok(output) => Some(output),
505                Err(error) => {
506                    log_error_with_caller(location, error, level);
507                    None
508                }
509            }),
510            Poll::Pending => Poll::Pending,
511        }
512    }
513}
514
515pub struct UnwrapFuture<F>(F);
516
517impl<F, T, E> Future for UnwrapFuture<F>
518where
519    F: Future<Output = Result<T, E>>,
520    E: std::fmt::Debug,
521{
522    type Output = T;
523
524    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
525        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
526        match inner.poll(cx) {
527            Poll::Ready(result) => Poll::Ready(result.unwrap()),
528            Poll::Pending => Poll::Pending,
529        }
530    }
531}
532
533pub struct Deferred<F: FnOnce()>(Option<F>);
534
535impl<F: FnOnce()> Deferred<F> {
536    /// Drop without running the deferred function.
537    pub fn abort(mut self) {
538        self.0.take();
539    }
540}
541
542impl<F: FnOnce()> Drop for Deferred<F> {
543    fn drop(&mut self) {
544        if let Some(f) = self.0.take() {
545            f()
546        }
547    }
548}
549
550/// Run the given function when the returned value is dropped (unless it's cancelled).
551#[must_use]
552pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
553    Deferred(Some(f))
554}
555
556#[cfg(any(test, feature = "test-support"))]
557mod rng {
558    use rand::{seq::SliceRandom, Rng};
559    pub struct RandomCharIter<T: Rng> {
560        rng: T,
561        simple_text: bool,
562    }
563
564    impl<T: Rng> RandomCharIter<T> {
565        pub fn new(rng: T) -> Self {
566            Self {
567                rng,
568                simple_text: std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()),
569            }
570        }
571
572        pub fn with_simple_text(mut self) -> Self {
573            self.simple_text = true;
574            self
575        }
576    }
577
578    impl<T: Rng> Iterator for RandomCharIter<T> {
579        type Item = char;
580
581        fn next(&mut self) -> Option<Self::Item> {
582            if self.simple_text {
583                return if self.rng.gen_range(0..100) < 5 {
584                    Some('\n')
585                } else {
586                    Some(self.rng.gen_range(b'a'..b'z' + 1).into())
587                };
588            }
589
590            match self.rng.gen_range(0..100) {
591                // whitespace
592                0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.rng).copied(),
593                // two-byte greek letters
594                20..=32 => char::from_u32(self.rng.gen_range(('α' as u32)..('ω' as u32 + 1))),
595                // // three-byte characters
596                33..=45 => ['✋', '✅', '❌', '❎', '⭐']
597                    .choose(&mut self.rng)
598                    .copied(),
599                // // four-byte characters
600                46..=58 => ['🍐', '🏀', '🍗', '🎉'].choose(&mut self.rng).copied(),
601                // ascii letters
602                _ => Some(self.rng.gen_range(b'a'..b'z' + 1).into()),
603            }
604        }
605    }
606}
607#[cfg(any(test, feature = "test-support"))]
608pub use rng::RandomCharIter;
609/// Get an embedded file as a string.
610pub fn asset_str<A: rust_embed::RustEmbed>(path: &str) -> Cow<'static, str> {
611    match A::get(path).unwrap().data {
612        Cow::Borrowed(bytes) => Cow::Borrowed(std::str::from_utf8(bytes).unwrap()),
613        Cow::Owned(bytes) => Cow::Owned(String::from_utf8(bytes).unwrap()),
614    }
615}
616
617/// Expands to an immediately-invoked function expression. Good for using the ? operator
618/// in functions which do not return an Option or Result.
619///
620/// Accepts a normal block, an async block, or an async move block.
621#[macro_export]
622macro_rules! maybe {
623    ($block:block) => {
624        (|| $block)()
625    };
626    (async $block:block) => {
627        (|| async $block)()
628    };
629    (async move $block:block) => {
630        (|| async move $block)()
631    };
632}
633
634pub trait RangeExt<T> {
635    fn sorted(&self) -> Self;
636    fn to_inclusive(&self) -> RangeInclusive<T>;
637    fn overlaps(&self, other: &Range<T>) -> bool;
638    fn contains_inclusive(&self, other: &Range<T>) -> bool;
639}
640
641impl<T: Ord + Clone> RangeExt<T> for Range<T> {
642    fn sorted(&self) -> Self {
643        cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
644    }
645
646    fn to_inclusive(&self) -> RangeInclusive<T> {
647        self.start.clone()..=self.end.clone()
648    }
649
650    fn overlaps(&self, other: &Range<T>) -> bool {
651        self.start < other.end && other.start < self.end
652    }
653
654    fn contains_inclusive(&self, other: &Range<T>) -> bool {
655        self.start <= other.start && other.end <= self.end
656    }
657}
658
659impl<T: Ord + Clone> RangeExt<T> for RangeInclusive<T> {
660    fn sorted(&self) -> Self {
661        cmp::min(self.start(), self.end()).clone()..=cmp::max(self.start(), self.end()).clone()
662    }
663
664    fn to_inclusive(&self) -> RangeInclusive<T> {
665        self.clone()
666    }
667
668    fn overlaps(&self, other: &Range<T>) -> bool {
669        self.start() < &other.end && &other.start <= self.end()
670    }
671
672    fn contains_inclusive(&self, other: &Range<T>) -> bool {
673        self.start() <= &other.start && &other.end <= self.end()
674    }
675}
676
677/// A way to sort strings with starting numbers numerically first, falling back to alphanumeric one,
678/// case-insensitive.
679///
680/// This is useful for turning regular alphanumerically sorted sequences as `1-abc, 10, 11-def, .., 2, 21-abc`
681/// into `1-abc, 2, 10, 11-def, .., 21-abc`
682#[derive(Debug, PartialEq, Eq)]
683pub struct NumericPrefixWithSuffix<'a>(Option<u64>, &'a str);
684
685impl<'a> NumericPrefixWithSuffix<'a> {
686    pub fn from_numeric_prefixed_str(str: &'a str) -> Self {
687        let i = str.chars().take_while(|c| c.is_ascii_digit()).count();
688        let (prefix, remainder) = str.split_at(i);
689
690        let prefix = prefix.parse().ok();
691        Self(prefix, remainder)
692    }
693}
694
695/// When dealing with equality, we need to consider the case of the strings to achieve strict equality
696/// to handle cases like "a" < "A" instead of "a" == "A".
697impl Ord for NumericPrefixWithSuffix<'_> {
698    fn cmp(&self, other: &Self) -> Ordering {
699        match (self.0, other.0) {
700            (None, None) => UniCase::new(self.1)
701                .cmp(&UniCase::new(other.1))
702                .then_with(|| self.1.cmp(other.1).reverse()),
703            (None, Some(_)) => Ordering::Greater,
704            (Some(_), None) => Ordering::Less,
705            (Some(a), Some(b)) => a.cmp(&b).then_with(|| {
706                UniCase::new(self.1)
707                    .cmp(&UniCase::new(other.1))
708                    .then_with(|| self.1.cmp(other.1).reverse())
709            }),
710        }
711    }
712}
713
714impl<'a> PartialOrd for NumericPrefixWithSuffix<'a> {
715    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
716        Some(self.cmp(other))
717    }
718}
719
720fn emoji_regex() -> &'static Regex {
721    static EMOJI_REGEX: LazyLock<Regex> =
722        LazyLock::new(|| Regex::new("(\\p{Emoji}|\u{200D})").unwrap());
723    &EMOJI_REGEX
724}
725
726/// Returns true if the given string consists of emojis only.
727/// E.g. "👨‍👩‍👧‍👧👋" will return true, but "👋!" will return false.
728pub fn word_consists_of_emojis(s: &str) -> bool {
729    let mut prev_end = 0;
730    for capture in emoji_regex().find_iter(s) {
731        if capture.start() != prev_end {
732            return false;
733        }
734        prev_end = capture.end();
735    }
736    prev_end == s.len()
737}
738
739#[cfg(test)]
740mod tests {
741    use super::*;
742
743    #[test]
744    fn test_extend_sorted() {
745        let mut vec = vec![];
746
747        extend_sorted(&mut vec, vec![21, 17, 13, 8, 1, 0], 5, |a, b| b.cmp(a));
748        assert_eq!(vec, &[21, 17, 13, 8, 1]);
749
750        extend_sorted(&mut vec, vec![101, 19, 17, 8, 2], 8, |a, b| b.cmp(a));
751        assert_eq!(vec, &[101, 21, 19, 17, 13, 8, 2, 1]);
752
753        extend_sorted(&mut vec, vec![1000, 19, 17, 9, 5], 8, |a, b| b.cmp(a));
754        assert_eq!(vec, &[1000, 101, 21, 19, 17, 13, 9, 8]);
755    }
756
757    #[test]
758    fn test_iife() {
759        fn option_returning_function() -> Option<()> {
760            None
761        }
762
763        let foo = maybe!({
764            option_returning_function()?;
765            Some(())
766        });
767
768        assert_eq!(foo, None);
769    }
770
771    #[test]
772    fn test_truncate_and_trailoff() {
773        assert_eq!(truncate_and_trailoff("", 5), "");
774        assert_eq!(truncate_and_trailoff("èèèèèè", 7), "èèèèèè");
775        assert_eq!(truncate_and_trailoff("èèèèèè", 6), "èèèèèè");
776        assert_eq!(truncate_and_trailoff("èèèèèè", 5), "èèèèè…");
777    }
778
779    #[test]
780    fn test_numeric_prefix_str_method() {
781        let target = "1a";
782        assert_eq!(
783            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
784            NumericPrefixWithSuffix(Some(1), "a")
785        );
786
787        let target = "12ab";
788        assert_eq!(
789            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
790            NumericPrefixWithSuffix(Some(12), "ab")
791        );
792
793        let target = "12_ab";
794        assert_eq!(
795            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
796            NumericPrefixWithSuffix(Some(12), "_ab")
797        );
798
799        let target = "1_2ab";
800        assert_eq!(
801            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
802            NumericPrefixWithSuffix(Some(1), "_2ab")
803        );
804
805        let target = "1.2";
806        assert_eq!(
807            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
808            NumericPrefixWithSuffix(Some(1), ".2")
809        );
810
811        let target = "1.2_a";
812        assert_eq!(
813            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
814            NumericPrefixWithSuffix(Some(1), ".2_a")
815        );
816
817        let target = "12.2_a";
818        assert_eq!(
819            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
820            NumericPrefixWithSuffix(Some(12), ".2_a")
821        );
822
823        let target = "12a.2_a";
824        assert_eq!(
825            NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
826            NumericPrefixWithSuffix(Some(12), "a.2_a")
827        );
828    }
829
830    #[test]
831    fn test_numeric_prefix_with_suffix() {
832        let mut sorted = vec!["1-abc", "10", "11def", "2", "21-abc"];
833        sorted.sort_by_key(|s| NumericPrefixWithSuffix::from_numeric_prefixed_str(s));
834        assert_eq!(sorted, ["1-abc", "2", "10", "11def", "21-abc"]);
835
836        for numeric_prefix_less in ["numeric_prefix_less", "aaa", "~™£"] {
837            assert_eq!(
838                NumericPrefixWithSuffix::from_numeric_prefixed_str(numeric_prefix_less),
839                NumericPrefixWithSuffix(None, numeric_prefix_less),
840                "String without numeric prefix `{numeric_prefix_less}` should not be converted into NumericPrefixWithSuffix"
841            )
842        }
843    }
844
845    #[test]
846    fn test_word_consists_of_emojis() {
847        let words_to_test = vec![
848            ("👨‍👩‍👧‍👧👋🥒", true),
849            ("👋", true),
850            ("!👋", false),
851            ("👋!", false),
852            ("👋 ", false),
853            (" 👋", false),
854            ("Test", false),
855        ];
856
857        for (text, expected_result) in words_to_test {
858            assert_eq!(word_consists_of_emojis(text), expected_result);
859        }
860    }
861
862    #[test]
863    fn test_truncate_lines_and_trailoff() {
864        let text = r#"Line 1
865Line 2
866Line 3"#;
867
868        assert_eq!(
869            truncate_lines_and_trailoff(text, 2),
870            r#"Line 1
871…"#
872        );
873
874        assert_eq!(
875            truncate_lines_and_trailoff(text, 3),
876            r#"Line 1
877Line 2
878…"#
879        );
880
881        assert_eq!(
882            truncate_lines_and_trailoff(text, 4),
883            r#"Line 1
884Line 2
885Line 3"#
886        );
887    }
888
889    #[test]
890    fn test_iterate_expanded_and_wrapped_usize_range() {
891        // Neither wrap
892        assert_eq!(
893            iterate_expanded_and_wrapped_usize_range(2..4, 1, 1, 8).collect::<Vec<usize>>(),
894            (1..5).collect::<Vec<usize>>()
895        );
896        // Start wraps
897        assert_eq!(
898            iterate_expanded_and_wrapped_usize_range(2..4, 3, 1, 8).collect::<Vec<usize>>(),
899            ((0..5).chain(7..8)).collect::<Vec<usize>>()
900        );
901        // Start wraps all the way around
902        assert_eq!(
903            iterate_expanded_and_wrapped_usize_range(2..4, 5, 1, 8).collect::<Vec<usize>>(),
904            (0..8).collect::<Vec<usize>>()
905        );
906        // Start wraps all the way around and past 0
907        assert_eq!(
908            iterate_expanded_and_wrapped_usize_range(2..4, 10, 1, 8).collect::<Vec<usize>>(),
909            (0..8).collect::<Vec<usize>>()
910        );
911        // End wraps
912        assert_eq!(
913            iterate_expanded_and_wrapped_usize_range(3..5, 1, 4, 8).collect::<Vec<usize>>(),
914            (0..1).chain(2..8).collect::<Vec<usize>>()
915        );
916        // End wraps all the way around
917        assert_eq!(
918            iterate_expanded_and_wrapped_usize_range(3..5, 1, 5, 8).collect::<Vec<usize>>(),
919            (0..8).collect::<Vec<usize>>()
920        );
921        // End wraps all the way around and past the end
922        assert_eq!(
923            iterate_expanded_and_wrapped_usize_range(3..5, 1, 10, 8).collect::<Vec<usize>>(),
924            (0..8).collect::<Vec<usize>>()
925        );
926        // Both start and end wrap
927        assert_eq!(
928            iterate_expanded_and_wrapped_usize_range(3..5, 4, 4, 8).collect::<Vec<usize>>(),
929            (0..8).collect::<Vec<usize>>()
930        );
931    }
932}