1pub mod arc_cow;
2pub mod archive;
3pub mod command;
4pub mod fs;
5pub mod markdown;
6pub mod paths;
7pub mod serde;
8pub mod shell_env;
9pub mod size;
10#[cfg(any(test, feature = "test-support"))]
11pub mod test;
12pub mod time;
13
14use anyhow::Result;
15use futures::Future;
16use itertools::Either;
17use regex::Regex;
18use std::sync::{LazyLock, OnceLock};
19use std::{
20 borrow::Cow,
21 cmp::{self, Ordering},
22 env,
23 ops::{AddAssign, Range, RangeInclusive},
24 panic::Location,
25 pin::Pin,
26 task::{Context, Poll},
27 time::Instant,
28};
29use unicase::UniCase;
30
31pub use take_until::*;
32#[cfg(any(test, feature = "test-support"))]
33pub use util_macros::{line_endings, path, uri};
34
35#[macro_export]
36macro_rules! debug_panic {
37 ( $($fmt_arg:tt)* ) => {
38 if cfg!(debug_assertions) {
39 panic!( $($fmt_arg)* );
40 } else {
41 let backtrace = std::backtrace::Backtrace::capture();
42 log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
43 }
44 };
45}
46
47pub fn truncate(s: &str, max_chars: usize) -> &str {
48 match s.char_indices().nth(max_chars) {
49 None => s,
50 Some((idx, _)) => &s[..idx],
51 }
52}
53
54/// Removes characters from the end of the string if its length is greater than `max_chars` and
55/// appends "..." to the string. Returns string unchanged if its length is smaller than max_chars.
56pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {
57 debug_assert!(max_chars >= 5);
58
59 // If the string's byte length is <= max_chars, walking the string can be skipped since the
60 // number of chars is <= the number of bytes.
61 if s.len() <= max_chars {
62 return s.to_string();
63 }
64 let truncation_ix = s.char_indices().map(|(i, _)| i).nth(max_chars);
65 match truncation_ix {
66 Some(index) => s[..index].to_string() + "β¦",
67 _ => s.to_string(),
68 }
69}
70
71/// Removes characters from the front of the string if its length is greater than `max_chars` and
72/// prepends the string with "...". Returns string unchanged if its length is smaller than max_chars.
73pub fn truncate_and_remove_front(s: &str, max_chars: usize) -> String {
74 debug_assert!(max_chars >= 5);
75
76 // If the string's byte length is <= max_chars, walking the string can be skipped since the
77 // number of chars is <= the number of bytes.
78 if s.len() <= max_chars {
79 return s.to_string();
80 }
81 let suffix_char_length = max_chars.saturating_sub(1);
82 let truncation_ix = s
83 .char_indices()
84 .map(|(i, _)| i)
85 .nth_back(suffix_char_length);
86 match truncation_ix {
87 Some(index) if index > 0 => "β¦".to_string() + &s[index..],
88 _ => s.to_string(),
89 }
90}
91
92/// Takes only `max_lines` from the string and, if there were more than `max_lines-1`, appends a
93/// a newline and "..." to the string, so that `max_lines` are returned.
94/// Returns string unchanged if its length is smaller than max_lines.
95pub fn truncate_lines_and_trailoff(s: &str, max_lines: usize) -> String {
96 let mut lines = s.lines().take(max_lines).collect::<Vec<_>>();
97 if lines.len() > max_lines - 1 {
98 lines.pop();
99 lines.join("\n") + "\nβ¦"
100 } else {
101 lines.join("\n")
102 }
103}
104
105/// Truncates the string at a character boundary, such that the result is less than `max_bytes` in
106/// length.
107pub fn truncate_to_byte_limit(s: &str, max_bytes: usize) -> &str {
108 if s.len() < max_bytes {
109 return s;
110 }
111
112 for i in (0..max_bytes).rev() {
113 if s.is_char_boundary(i) {
114 return &s[..i];
115 }
116 }
117
118 ""
119}
120
121/// Takes a prefix of complete lines which fit within the byte limit. If the first line is longer
122/// than the limit, truncates at a character boundary.
123pub fn truncate_lines_to_byte_limit(s: &str, max_bytes: usize) -> &str {
124 if s.len() < max_bytes {
125 return s;
126 }
127
128 for i in (0..max_bytes).rev() {
129 if s.is_char_boundary(i) {
130 if s.as_bytes()[i] == b'\n' {
131 // Since the i-th character is \n, valid to slice at i + 1.
132 return &s[..i + 1];
133 }
134 }
135 }
136
137 truncate_to_byte_limit(s, max_bytes)
138}
139
140#[test]
141fn test_truncate_lines_to_byte_limit() {
142 let text = "Line 1\nLine 2\nLine 3\nLine 4";
143
144 // Limit that includes all lines
145 assert_eq!(truncate_lines_to_byte_limit(text, 100), text);
146
147 // Exactly the first line
148 assert_eq!(truncate_lines_to_byte_limit(text, 7), "Line 1\n");
149
150 // Limit between lines
151 assert_eq!(truncate_lines_to_byte_limit(text, 13), "Line 1\n");
152 assert_eq!(truncate_lines_to_byte_limit(text, 20), "Line 1\nLine 2\n");
153
154 // Limit before first newline
155 assert_eq!(truncate_lines_to_byte_limit(text, 6), "Line ");
156
157 // Test with non-ASCII characters
158 let text_utf8 = "Line 1\nLΓne 2\nLine 3";
159 assert_eq!(
160 truncate_lines_to_byte_limit(text_utf8, 15),
161 "Line 1\nLΓne 2\n"
162 );
163}
164
165pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
166 let prev = *value;
167 *value += T::from(1);
168 prev
169}
170
171/// Extend a sorted vector with a sorted sequence of items, maintaining the vector's sort order and
172/// enforcing a maximum length. This also de-duplicates items. Sort the items according to the given callback. Before calling this,
173/// both `vec` and `new_items` should already be sorted according to the `cmp` comparator.
174pub fn extend_sorted<T, I, F>(vec: &mut Vec<T>, new_items: I, limit: usize, mut cmp: F)
175where
176 I: IntoIterator<Item = T>,
177 F: FnMut(&T, &T) -> Ordering,
178{
179 let mut start_index = 0;
180 for new_item in new_items {
181 if let Err(i) = vec[start_index..].binary_search_by(|m| cmp(m, &new_item)) {
182 let index = start_index + i;
183 if vec.len() < limit {
184 vec.insert(index, new_item);
185 } else if index < vec.len() {
186 vec.pop();
187 vec.insert(index, new_item);
188 }
189 start_index = index;
190 }
191 }
192}
193
194pub fn truncate_to_bottom_n_sorted_by<T, F>(items: &mut Vec<T>, limit: usize, compare: &F)
195where
196 F: Fn(&T, &T) -> Ordering,
197{
198 if limit == 0 {
199 items.truncate(0);
200 }
201 if items.len() <= limit {
202 items.sort_by(compare);
203 return;
204 }
205 // When limit is near to items.len() it may be more efficient to sort the whole list and
206 // truncate, rather than always doing selection first as is done below. It's hard to analyze
207 // where the threshold for this should be since the quickselect style algorithm used by
208 // `select_nth_unstable_by` makes the prefix partially sorted, and so its work is not wasted -
209 // the expected number of comparisons needed by `sort_by` is less than it is for some arbitrary
210 // unsorted input.
211 items.select_nth_unstable_by(limit, compare);
212 items.truncate(limit);
213 items.sort_by(compare);
214}
215
216#[cfg(unix)]
217fn load_shell_from_passwd() -> Result<()> {
218 let buflen = match unsafe { libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) } {
219 n if n < 0 => 1024,
220 n => n as usize,
221 };
222 let mut buffer = Vec::with_capacity(buflen);
223
224 let mut pwd: std::mem::MaybeUninit<libc::passwd> = std::mem::MaybeUninit::uninit();
225 let mut result: *mut libc::passwd = std::ptr::null_mut();
226
227 let uid = unsafe { libc::getuid() };
228 let status = unsafe {
229 libc::getpwuid_r(
230 uid,
231 pwd.as_mut_ptr(),
232 buffer.as_mut_ptr() as *mut libc::c_char,
233 buflen,
234 &mut result,
235 )
236 };
237 let entry = unsafe { pwd.assume_init() };
238
239 anyhow::ensure!(
240 status == 0,
241 "call to getpwuid_r failed. uid: {}, status: {}",
242 uid,
243 status
244 );
245 anyhow::ensure!(!result.is_null(), "passwd entry for uid {} not found", uid);
246 anyhow::ensure!(
247 entry.pw_uid == uid,
248 "passwd entry has different uid ({}) than getuid ({}) returned",
249 entry.pw_uid,
250 uid,
251 );
252
253 let shell = unsafe { std::ffi::CStr::from_ptr(entry.pw_shell).to_str().unwrap() };
254 if env::var("SHELL").map_or(true, |shell_env| shell_env != shell) {
255 log::info!(
256 "updating SHELL environment variable to value from passwd entry: {:?}",
257 shell,
258 );
259 unsafe { env::set_var("SHELL", shell) };
260 }
261
262 Ok(())
263}
264
265#[cfg(unix)]
266/// Returns a shell escaped path for the current zed executable
267pub fn get_shell_safe_zed_path() -> anyhow::Result<String> {
268 use anyhow::Context;
269
270 let zed_path = std::env::current_exe()
271 .context("Failed to determine current zed executable path.")?
272 .to_string_lossy()
273 .trim_end_matches(" (deleted)") // see https://github.com/rust-lang/rust/issues/69343
274 .to_string();
275
276 // As of writing, this can only be fail if the path contains a null byte, which shouldn't be possible
277 // but shlex has annotated the error as #[non_exhaustive] so we can't make it a compile error if other
278 // errors are introduced in the future :(
279 let zed_path_escaped =
280 shlex::try_quote(&zed_path).context("Failed to shell-escape Zed executable path.")?;
281
282 return Ok(zed_path_escaped.to_string());
283}
284
285#[cfg(unix)]
286pub fn load_login_shell_environment() -> Result<()> {
287 load_shell_from_passwd().log_err();
288
289 // If possible, we want to `cd` in the user's `$HOME` to trigger programs
290 // such as direnv, asdf, mise, ... to adjust the PATH. These tools often hook
291 // into shell's `cd` command (and hooks) to manipulate env.
292 // We do this so that we get the env a user would have when spawning a shell
293 // in home directory.
294 for (name, value) in shell_env::capture(paths::home_dir())? {
295 unsafe { env::set_var(&name, &value) };
296 }
297
298 log::info!(
299 "set environment variables from shell:{}, path:{}",
300 std::env::var("SHELL").unwrap_or_default(),
301 std::env::var("PATH").unwrap_or_default(),
302 );
303
304 Ok(())
305}
306
307/// Configures the process to start a new session, to prevent interactive shells from taking control
308/// of the terminal.
309///
310/// For more details: https://registerspill.thorstenball.com/p/how-to-lose-control-of-your-shell
311pub fn set_pre_exec_to_start_new_session(
312 command: &mut std::process::Command,
313) -> &mut std::process::Command {
314 // safety: code in pre_exec should be signal safe.
315 // https://man7.org/linux/man-pages/man7/signal-safety.7.html
316 #[cfg(not(target_os = "windows"))]
317 unsafe {
318 use std::os::unix::process::CommandExt;
319 command.pre_exec(|| {
320 libc::setsid();
321 Ok(())
322 });
323 };
324 command
325}
326
327pub fn merge_json_lenient_value_into(
328 source: serde_json_lenient::Value,
329 target: &mut serde_json_lenient::Value,
330) {
331 match (source, target) {
332 (serde_json_lenient::Value::Object(source), serde_json_lenient::Value::Object(target)) => {
333 for (key, value) in source {
334 if let Some(target) = target.get_mut(&key) {
335 merge_json_lenient_value_into(value, target);
336 } else {
337 target.insert(key, value);
338 }
339 }
340 }
341
342 (serde_json_lenient::Value::Array(source), serde_json_lenient::Value::Array(target)) => {
343 for value in source {
344 target.push(value);
345 }
346 }
347
348 (source, target) => *target = source,
349 }
350}
351
352pub fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
353 use serde_json::Value;
354
355 match (source, target) {
356 (Value::Object(source), Value::Object(target)) => {
357 for (key, value) in source {
358 if let Some(target) = target.get_mut(&key) {
359 merge_json_value_into(value, target);
360 } else {
361 target.insert(key, value);
362 }
363 }
364 }
365
366 (Value::Array(source), Value::Array(target)) => {
367 for value in source {
368 target.push(value);
369 }
370 }
371
372 (source, target) => *target = source,
373 }
374}
375
376pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
377 use serde_json::Value;
378 if let Value::Object(source_object) = source {
379 let target_object = if let Value::Object(target) = target {
380 target
381 } else {
382 *target = Value::Object(Default::default());
383 target.as_object_mut().unwrap()
384 };
385 for (key, value) in source_object {
386 if let Some(target) = target_object.get_mut(&key) {
387 merge_non_null_json_value_into(value, target);
388 } else if !value.is_null() {
389 target_object.insert(key, value);
390 }
391 }
392 } else if !source.is_null() {
393 *target = source
394 }
395}
396
397pub fn measure<R>(label: &str, f: impl FnOnce() -> R) -> R {
398 static ZED_MEASUREMENTS: OnceLock<bool> = OnceLock::new();
399 let zed_measurements = ZED_MEASUREMENTS.get_or_init(|| {
400 env::var("ZED_MEASUREMENTS")
401 .map(|measurements| measurements == "1" || measurements == "true")
402 .unwrap_or(false)
403 });
404
405 if *zed_measurements {
406 let start = Instant::now();
407 let result = f();
408 let elapsed = start.elapsed();
409 eprintln!("{}: {:?}", label, elapsed);
410 result
411 } else {
412 f()
413 }
414}
415
416pub fn expanded_and_wrapped_usize_range(
417 range: Range<usize>,
418 additional_before: usize,
419 additional_after: usize,
420 wrap_length: usize,
421) -> impl Iterator<Item = usize> {
422 let start_wraps = range.start < additional_before;
423 let end_wraps = wrap_length < range.end + additional_after;
424 if start_wraps && end_wraps {
425 Either::Left(0..wrap_length)
426 } else if start_wraps {
427 let wrapped_start = (range.start + wrap_length).saturating_sub(additional_before);
428 if wrapped_start <= range.end {
429 Either::Left(0..wrap_length)
430 } else {
431 Either::Right((0..range.end + additional_after).chain(wrapped_start..wrap_length))
432 }
433 } else if end_wraps {
434 let wrapped_end = range.end + additional_after - wrap_length;
435 if range.start <= wrapped_end {
436 Either::Left(0..wrap_length)
437 } else {
438 Either::Right((0..wrapped_end).chain(range.start - additional_before..wrap_length))
439 }
440 } else {
441 Either::Left((range.start - additional_before)..(range.end + additional_after))
442 }
443}
444
445/// Yields `[i, i + 1, i - 1, i + 2, ..]`, each modulo `wrap_length` and bounded by
446/// `additional_before` and `additional_after`. If the wrapping causes overlap, duplicates are not
447/// emitted. If wrap_length is 0, nothing is yielded.
448pub fn wrapped_usize_outward_from(
449 start: usize,
450 additional_before: usize,
451 additional_after: usize,
452 wrap_length: usize,
453) -> impl Iterator<Item = usize> {
454 let mut count = 0;
455 let mut after_offset = 1;
456 let mut before_offset = 1;
457
458 std::iter::from_fn(move || {
459 count += 1;
460 if count > wrap_length {
461 None
462 } else if count == 1 {
463 Some(start % wrap_length)
464 } else if after_offset <= additional_after && after_offset <= before_offset {
465 let value = (start + after_offset) % wrap_length;
466 after_offset += 1;
467 Some(value)
468 } else if before_offset <= additional_before {
469 let value = (start + wrap_length - before_offset) % wrap_length;
470 before_offset += 1;
471 Some(value)
472 } else if after_offset <= additional_after {
473 let value = (start + after_offset) % wrap_length;
474 after_offset += 1;
475 Some(value)
476 } else {
477 None
478 }
479 })
480}
481
482#[cfg(target_os = "windows")]
483pub fn get_windows_system_shell() -> String {
484 use std::path::PathBuf;
485
486 fn find_pwsh_in_programfiles(find_alternate: bool, find_preview: bool) -> Option<PathBuf> {
487 #[cfg(target_pointer_width = "64")]
488 let env_var = if find_alternate {
489 "ProgramFiles(x86)"
490 } else {
491 "ProgramFiles"
492 };
493
494 #[cfg(target_pointer_width = "32")]
495 let env_var = if find_alternate {
496 "ProgramW6432"
497 } else {
498 "ProgramFiles"
499 };
500
501 let install_base_dir = PathBuf::from(std::env::var_os(env_var)?).join("PowerShell");
502 install_base_dir
503 .read_dir()
504 .ok()?
505 .filter_map(Result::ok)
506 .filter(|entry| matches!(entry.file_type(), Ok(ft) if ft.is_dir()))
507 .filter_map(|entry| {
508 let dir_name = entry.file_name();
509 let dir_name = dir_name.to_string_lossy();
510
511 let version = if find_preview {
512 let dash_index = dir_name.find('-')?;
513 if &dir_name[dash_index + 1..] != "preview" {
514 return None;
515 };
516 dir_name[..dash_index].parse::<u32>().ok()?
517 } else {
518 dir_name.parse::<u32>().ok()?
519 };
520
521 let exe_path = entry.path().join("pwsh.exe");
522 if exe_path.exists() {
523 Some((version, exe_path))
524 } else {
525 None
526 }
527 })
528 .max_by_key(|(version, _)| *version)
529 .map(|(_, path)| path)
530 }
531
532 fn find_pwsh_in_msix(find_preview: bool) -> Option<PathBuf> {
533 let msix_app_dir =
534 PathBuf::from(std::env::var_os("LOCALAPPDATA")?).join("Microsoft\\WindowsApps");
535 if !msix_app_dir.exists() {
536 return None;
537 }
538
539 let prefix = if find_preview {
540 "Microsoft.PowerShellPreview_"
541 } else {
542 "Microsoft.PowerShell_"
543 };
544 msix_app_dir
545 .read_dir()
546 .ok()?
547 .filter_map(|entry| {
548 let entry = entry.ok()?;
549 if !matches!(entry.file_type(), Ok(ft) if ft.is_dir()) {
550 return None;
551 }
552
553 if !entry.file_name().to_string_lossy().starts_with(prefix) {
554 return None;
555 }
556
557 let exe_path = entry.path().join("pwsh.exe");
558 exe_path.exists().then_some(exe_path)
559 })
560 .next()
561 }
562
563 fn find_pwsh_in_scoop() -> Option<PathBuf> {
564 let pwsh_exe =
565 PathBuf::from(std::env::var_os("USERPROFILE")?).join("scoop\\shims\\pwsh.exe");
566 pwsh_exe.exists().then_some(pwsh_exe)
567 }
568
569 static SYSTEM_SHELL: LazyLock<String> = LazyLock::new(|| {
570 find_pwsh_in_programfiles(false, false)
571 .or_else(|| find_pwsh_in_programfiles(true, false))
572 .or_else(|| find_pwsh_in_msix(false))
573 .or_else(|| find_pwsh_in_programfiles(false, true))
574 .or_else(|| find_pwsh_in_msix(true))
575 .or_else(|| find_pwsh_in_programfiles(true, true))
576 .or_else(find_pwsh_in_scoop)
577 .map(|p| p.to_string_lossy().to_string())
578 .unwrap_or("powershell.exe".to_string())
579 });
580
581 (*SYSTEM_SHELL).clone()
582}
583
584pub trait ResultExt<E> {
585 type Ok;
586
587 fn log_err(self) -> Option<Self::Ok>;
588 /// Assert that this result should never be an error in development or tests.
589 fn debug_assert_ok(self, reason: &str) -> Self;
590 fn warn_on_err(self) -> Option<Self::Ok>;
591 fn log_with_level(self, level: log::Level) -> Option<Self::Ok>;
592 fn anyhow(self) -> anyhow::Result<Self::Ok>
593 where
594 E: Into<anyhow::Error>;
595}
596
597impl<T, E> ResultExt<E> for Result<T, E>
598where
599 E: std::fmt::Debug,
600{
601 type Ok = T;
602
603 #[track_caller]
604 fn log_err(self) -> Option<T> {
605 self.log_with_level(log::Level::Error)
606 }
607
608 #[track_caller]
609 fn debug_assert_ok(self, reason: &str) -> Self {
610 if let Err(error) = &self {
611 debug_panic!("{reason} - {error:?}");
612 }
613 self
614 }
615
616 #[track_caller]
617 fn warn_on_err(self) -> Option<T> {
618 self.log_with_level(log::Level::Warn)
619 }
620
621 #[track_caller]
622 fn log_with_level(self, level: log::Level) -> Option<T> {
623 match self {
624 Ok(value) => Some(value),
625 Err(error) => {
626 log_error_with_caller(*Location::caller(), error, level);
627 None
628 }
629 }
630 }
631
632 fn anyhow(self) -> anyhow::Result<T>
633 where
634 E: Into<anyhow::Error>,
635 {
636 self.map_err(Into::into)
637 }
638}
639
640fn log_error_with_caller<E>(caller: core::panic::Location<'_>, error: E, level: log::Level)
641where
642 E: std::fmt::Debug,
643{
644 #[cfg(not(target_os = "windows"))]
645 let file = caller.file();
646 #[cfg(target_os = "windows")]
647 let file = caller.file().replace('\\', "/");
648 // In this codebase, the first segment of the file path is
649 // the 'crates' folder, followed by the crate name.
650 let target = file.split('/').nth(1);
651
652 log::logger().log(
653 &log::Record::builder()
654 .target(target.unwrap_or(""))
655 .module_path(target)
656 .args(format_args!("{:?}", error))
657 .file(Some(caller.file()))
658 .line(Some(caller.line()))
659 .level(level)
660 .build(),
661 );
662}
663
664pub fn log_err<E: std::fmt::Debug>(error: &E) {
665 log_error_with_caller(*Location::caller(), error, log::Level::Warn);
666}
667
668pub trait TryFutureExt {
669 fn log_err(self) -> LogErrorFuture<Self>
670 where
671 Self: Sized;
672
673 fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
674 where
675 Self: Sized;
676
677 fn warn_on_err(self) -> LogErrorFuture<Self>
678 where
679 Self: Sized;
680 fn unwrap(self) -> UnwrapFuture<Self>
681 where
682 Self: Sized;
683}
684
685impl<F, T, E> TryFutureExt for F
686where
687 F: Future<Output = Result<T, E>>,
688 E: std::fmt::Debug,
689{
690 #[track_caller]
691 fn log_err(self) -> LogErrorFuture<Self>
692 where
693 Self: Sized,
694 {
695 let location = Location::caller();
696 LogErrorFuture(self, log::Level::Error, *location)
697 }
698
699 fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
700 where
701 Self: Sized,
702 {
703 LogErrorFuture(self, log::Level::Error, location)
704 }
705
706 #[track_caller]
707 fn warn_on_err(self) -> LogErrorFuture<Self>
708 where
709 Self: Sized,
710 {
711 let location = Location::caller();
712 LogErrorFuture(self, log::Level::Warn, *location)
713 }
714
715 fn unwrap(self) -> UnwrapFuture<Self>
716 where
717 Self: Sized,
718 {
719 UnwrapFuture(self)
720 }
721}
722
723#[must_use]
724pub struct LogErrorFuture<F>(F, log::Level, core::panic::Location<'static>);
725
726impl<F, T, E> Future for LogErrorFuture<F>
727where
728 F: Future<Output = Result<T, E>>,
729 E: std::fmt::Debug,
730{
731 type Output = Option<T>;
732
733 fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
734 let level = self.1;
735 let location = self.2;
736 let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
737 match inner.poll(cx) {
738 Poll::Ready(output) => Poll::Ready(match output {
739 Ok(output) => Some(output),
740 Err(error) => {
741 log_error_with_caller(location, error, level);
742 None
743 }
744 }),
745 Poll::Pending => Poll::Pending,
746 }
747 }
748}
749
750pub struct UnwrapFuture<F>(F);
751
752impl<F, T, E> Future for UnwrapFuture<F>
753where
754 F: Future<Output = Result<T, E>>,
755 E: std::fmt::Debug,
756{
757 type Output = T;
758
759 fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
760 let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
761 match inner.poll(cx) {
762 Poll::Ready(result) => Poll::Ready(result.unwrap()),
763 Poll::Pending => Poll::Pending,
764 }
765 }
766}
767
768pub struct Deferred<F: FnOnce()>(Option<F>);
769
770impl<F: FnOnce()> Deferred<F> {
771 /// Drop without running the deferred function.
772 pub fn abort(mut self) {
773 self.0.take();
774 }
775}
776
777impl<F: FnOnce()> Drop for Deferred<F> {
778 fn drop(&mut self) {
779 if let Some(f) = self.0.take() {
780 f()
781 }
782 }
783}
784
785/// Run the given function when the returned value is dropped (unless it's cancelled).
786#[must_use]
787pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
788 Deferred(Some(f))
789}
790
791#[cfg(any(test, feature = "test-support"))]
792mod rng {
793 use rand::{Rng, seq::SliceRandom};
794 pub struct RandomCharIter<T: Rng> {
795 rng: T,
796 simple_text: bool,
797 }
798
799 impl<T: Rng> RandomCharIter<T> {
800 pub fn new(rng: T) -> Self {
801 Self {
802 rng,
803 simple_text: std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()),
804 }
805 }
806
807 pub fn with_simple_text(mut self) -> Self {
808 self.simple_text = true;
809 self
810 }
811 }
812
813 impl<T: Rng> Iterator for RandomCharIter<T> {
814 type Item = char;
815
816 fn next(&mut self) -> Option<Self::Item> {
817 if self.simple_text {
818 return if self.rng.gen_range(0..100) < 5 {
819 Some('\n')
820 } else {
821 Some(self.rng.gen_range(b'a'..b'z' + 1).into())
822 };
823 }
824
825 match self.rng.gen_range(0..100) {
826 // whitespace
827 0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.rng).copied(),
828 // two-byte greek letters
829 20..=32 => char::from_u32(self.rng.gen_range(('Ξ±' as u32)..('Ο' as u32 + 1))),
830 // // three-byte characters
831 33..=45 => ['β', 'β
', 'β', 'β', 'β']
832 .choose(&mut self.rng)
833 .copied(),
834 // // four-byte characters
835 46..=58 => ['π', 'π', 'π', 'π'].choose(&mut self.rng).copied(),
836 // ascii letters
837 _ => Some(self.rng.gen_range(b'a'..b'z' + 1).into()),
838 }
839 }
840 }
841}
842#[cfg(any(test, feature = "test-support"))]
843pub use rng::RandomCharIter;
844/// Get an embedded file as a string.
845pub fn asset_str<A: rust_embed::RustEmbed>(path: &str) -> Cow<'static, str> {
846 match A::get(path).expect(path).data {
847 Cow::Borrowed(bytes) => Cow::Borrowed(std::str::from_utf8(bytes).unwrap()),
848 Cow::Owned(bytes) => Cow::Owned(String::from_utf8(bytes).unwrap()),
849 }
850}
851
852/// Expands to an immediately-invoked function expression. Good for using the ? operator
853/// in functions which do not return an Option or Result.
854///
855/// Accepts a normal block, an async block, or an async move block.
856#[macro_export]
857macro_rules! maybe {
858 ($block:block) => {
859 (|| $block)()
860 };
861 (async $block:block) => {
862 (|| async $block)()
863 };
864 (async move $block:block) => {
865 (|| async move $block)()
866 };
867}
868
869pub trait RangeExt<T> {
870 fn sorted(&self) -> Self;
871 fn to_inclusive(&self) -> RangeInclusive<T>;
872 fn overlaps(&self, other: &Range<T>) -> bool;
873 fn contains_inclusive(&self, other: &Range<T>) -> bool;
874}
875
876impl<T: Ord + Clone> RangeExt<T> for Range<T> {
877 fn sorted(&self) -> Self {
878 cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
879 }
880
881 fn to_inclusive(&self) -> RangeInclusive<T> {
882 self.start.clone()..=self.end.clone()
883 }
884
885 fn overlaps(&self, other: &Range<T>) -> bool {
886 self.start < other.end && other.start < self.end
887 }
888
889 fn contains_inclusive(&self, other: &Range<T>) -> bool {
890 self.start <= other.start && other.end <= self.end
891 }
892}
893
894impl<T: Ord + Clone> RangeExt<T> for RangeInclusive<T> {
895 fn sorted(&self) -> Self {
896 cmp::min(self.start(), self.end()).clone()..=cmp::max(self.start(), self.end()).clone()
897 }
898
899 fn to_inclusive(&self) -> RangeInclusive<T> {
900 self.clone()
901 }
902
903 fn overlaps(&self, other: &Range<T>) -> bool {
904 self.start() < &other.end && &other.start <= self.end()
905 }
906
907 fn contains_inclusive(&self, other: &Range<T>) -> bool {
908 self.start() <= &other.start && &other.end <= self.end()
909 }
910}
911
912/// A way to sort strings with starting numbers numerically first, falling back to alphanumeric one,
913/// case-insensitive.
914///
915/// This is useful for turning regular alphanumerically sorted sequences as `1-abc, 10, 11-def, .., 2, 21-abc`
916/// into `1-abc, 2, 10, 11-def, .., 21-abc`
917#[derive(Debug, PartialEq, Eq)]
918pub struct NumericPrefixWithSuffix<'a>(Option<u64>, &'a str);
919
920impl<'a> NumericPrefixWithSuffix<'a> {
921 pub fn from_numeric_prefixed_str(str: &'a str) -> Self {
922 let i = str.chars().take_while(|c| c.is_ascii_digit()).count();
923 let (prefix, remainder) = str.split_at(i);
924
925 let prefix = prefix.parse().ok();
926 Self(prefix, remainder)
927 }
928}
929
930/// When dealing with equality, we need to consider the case of the strings to achieve strict equality
931/// to handle cases like "a" < "A" instead of "a" == "A".
932impl Ord for NumericPrefixWithSuffix<'_> {
933 fn cmp(&self, other: &Self) -> Ordering {
934 match (self.0, other.0) {
935 (None, None) => UniCase::new(self.1)
936 .cmp(&UniCase::new(other.1))
937 .then_with(|| self.1.cmp(other.1).reverse()),
938 (None, Some(_)) => Ordering::Greater,
939 (Some(_), None) => Ordering::Less,
940 (Some(a), Some(b)) => a.cmp(&b).then_with(|| {
941 UniCase::new(self.1)
942 .cmp(&UniCase::new(other.1))
943 .then_with(|| self.1.cmp(other.1).reverse())
944 }),
945 }
946 }
947}
948
949impl PartialOrd for NumericPrefixWithSuffix<'_> {
950 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
951 Some(self.cmp(other))
952 }
953}
954
955/// Capitalizes the first character of a string.
956///
957/// This function takes a string slice as input and returns a new `String` with the first character
958/// capitalized.
959///
960/// # Examples
961///
962/// ```
963/// use util::capitalize;
964///
965/// assert_eq!(capitalize("hello"), "Hello");
966/// assert_eq!(capitalize("WORLD"), "WORLD");
967/// assert_eq!(capitalize(""), "");
968/// ```
969pub fn capitalize(str: &str) -> String {
970 let mut chars = str.chars();
971 match chars.next() {
972 None => String::new(),
973 Some(first_char) => first_char.to_uppercase().collect::<String>() + chars.as_str(),
974 }
975}
976
977fn emoji_regex() -> &'static Regex {
978 static EMOJI_REGEX: LazyLock<Regex> =
979 LazyLock::new(|| Regex::new("(\\p{Emoji}|\u{200D})").unwrap());
980 &EMOJI_REGEX
981}
982
983/// Returns true if the given string consists of emojis only.
984/// E.g. "π¨βπ©βπ§βπ§π" will return true, but "π!" will return false.
985pub fn word_consists_of_emojis(s: &str) -> bool {
986 let mut prev_end = 0;
987 for capture in emoji_regex().find_iter(s) {
988 if capture.start() != prev_end {
989 return false;
990 }
991 prev_end = capture.end();
992 }
993 prev_end == s.len()
994}
995
996/// Similar to `str::split`, but also provides byte-offset ranges of the results. Unlike
997/// `str::split`, this is not generic on pattern types and does not return an `Iterator`.
998pub fn split_str_with_ranges(s: &str, pat: impl Fn(char) -> bool) -> Vec<(Range<usize>, &str)> {
999 let mut result = Vec::new();
1000 let mut start = 0;
1001
1002 for (i, ch) in s.char_indices() {
1003 if pat(ch) {
1004 if i > start {
1005 result.push((start..i, &s[start..i]));
1006 }
1007 start = i + ch.len_utf8();
1008 }
1009 }
1010
1011 if s.len() > start {
1012 result.push((start..s.len(), &s[start..s.len()]));
1013 }
1014
1015 result
1016}
1017
1018pub fn default<D: Default>() -> D {
1019 Default::default()
1020}
1021
1022pub fn get_system_shell() -> String {
1023 #[cfg(target_os = "windows")]
1024 {
1025 get_windows_system_shell()
1026 }
1027
1028 #[cfg(not(target_os = "windows"))]
1029 {
1030 std::env::var("SHELL").unwrap_or("/bin/sh".to_string())
1031 }
1032}
1033
1034#[derive(Debug)]
1035pub enum ConnectionResult<O> {
1036 Timeout,
1037 ConnectionReset,
1038 Result(anyhow::Result<O>),
1039}
1040
1041impl<O> ConnectionResult<O> {
1042 pub fn into_response(self) -> anyhow::Result<O> {
1043 match self {
1044 ConnectionResult::Timeout => anyhow::bail!("Request timed out"),
1045 ConnectionResult::ConnectionReset => anyhow::bail!("Server reset the connection"),
1046 ConnectionResult::Result(r) => r,
1047 }
1048 }
1049}
1050
1051impl<O> From<anyhow::Result<O>> for ConnectionResult<O> {
1052 fn from(result: anyhow::Result<O>) -> Self {
1053 ConnectionResult::Result(result)
1054 }
1055}
1056
1057#[cfg(test)]
1058mod tests {
1059 use super::*;
1060
1061 #[test]
1062 fn test_extend_sorted() {
1063 let mut vec = vec![];
1064
1065 extend_sorted(&mut vec, vec![21, 17, 13, 8, 1, 0], 5, |a, b| b.cmp(a));
1066 assert_eq!(vec, &[21, 17, 13, 8, 1]);
1067
1068 extend_sorted(&mut vec, vec![101, 19, 17, 8, 2], 8, |a, b| b.cmp(a));
1069 assert_eq!(vec, &[101, 21, 19, 17, 13, 8, 2, 1]);
1070
1071 extend_sorted(&mut vec, vec![1000, 19, 17, 9, 5], 8, |a, b| b.cmp(a));
1072 assert_eq!(vec, &[1000, 101, 21, 19, 17, 13, 9, 8]);
1073 }
1074
1075 #[test]
1076 fn test_truncate_to_bottom_n_sorted_by() {
1077 let mut vec: Vec<u32> = vec![5, 2, 3, 4, 1];
1078 truncate_to_bottom_n_sorted_by(&mut vec, 10, &u32::cmp);
1079 assert_eq!(vec, &[1, 2, 3, 4, 5]);
1080
1081 vec = vec![5, 2, 3, 4, 1];
1082 truncate_to_bottom_n_sorted_by(&mut vec, 5, &u32::cmp);
1083 assert_eq!(vec, &[1, 2, 3, 4, 5]);
1084
1085 vec = vec![5, 2, 3, 4, 1];
1086 truncate_to_bottom_n_sorted_by(&mut vec, 4, &u32::cmp);
1087 assert_eq!(vec, &[1, 2, 3, 4]);
1088
1089 vec = vec![5, 2, 3, 4, 1];
1090 truncate_to_bottom_n_sorted_by(&mut vec, 1, &u32::cmp);
1091 assert_eq!(vec, &[1]);
1092
1093 vec = vec![5, 2, 3, 4, 1];
1094 truncate_to_bottom_n_sorted_by(&mut vec, 0, &u32::cmp);
1095 assert!(vec.is_empty());
1096 }
1097
1098 #[test]
1099 fn test_iife() {
1100 fn option_returning_function() -> Option<()> {
1101 None
1102 }
1103
1104 let foo = maybe!({
1105 option_returning_function()?;
1106 Some(())
1107 });
1108
1109 assert_eq!(foo, None);
1110 }
1111
1112 #[test]
1113 fn test_truncate_and_trailoff() {
1114 assert_eq!(truncate_and_trailoff("", 5), "");
1115 assert_eq!(truncate_and_trailoff("aaaaaa", 7), "aaaaaa");
1116 assert_eq!(truncate_and_trailoff("aaaaaa", 6), "aaaaaa");
1117 assert_eq!(truncate_and_trailoff("aaaaaa", 5), "aaaaaβ¦");
1118 assert_eq!(truncate_and_trailoff("èèèèèè", 7), "èèèèèè");
1119 assert_eq!(truncate_and_trailoff("èèèèèè", 6), "èèèèèè");
1120 assert_eq!(truncate_and_trailoff("èèèèèè", 5), "èèèèèβ¦");
1121 }
1122
1123 #[test]
1124 fn test_truncate_and_remove_front() {
1125 assert_eq!(truncate_and_remove_front("", 5), "");
1126 assert_eq!(truncate_and_remove_front("aaaaaa", 7), "aaaaaa");
1127 assert_eq!(truncate_and_remove_front("aaaaaa", 6), "aaaaaa");
1128 assert_eq!(truncate_and_remove_front("aaaaaa", 5), "β¦aaaaa");
1129 assert_eq!(truncate_and_remove_front("èèèèèè", 7), "èèèèèè");
1130 assert_eq!(truncate_and_remove_front("èèèèèè", 6), "èèèèèè");
1131 assert_eq!(truncate_and_remove_front("èèèèèè", 5), "β¦Γ¨Γ¨Γ¨Γ¨Γ¨");
1132 }
1133
1134 #[test]
1135 fn test_numeric_prefix_str_method() {
1136 let target = "1a";
1137 assert_eq!(
1138 NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
1139 NumericPrefixWithSuffix(Some(1), "a")
1140 );
1141
1142 let target = "12ab";
1143 assert_eq!(
1144 NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
1145 NumericPrefixWithSuffix(Some(12), "ab")
1146 );
1147
1148 let target = "12_ab";
1149 assert_eq!(
1150 NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
1151 NumericPrefixWithSuffix(Some(12), "_ab")
1152 );
1153
1154 let target = "1_2ab";
1155 assert_eq!(
1156 NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
1157 NumericPrefixWithSuffix(Some(1), "_2ab")
1158 );
1159
1160 let target = "1.2";
1161 assert_eq!(
1162 NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
1163 NumericPrefixWithSuffix(Some(1), ".2")
1164 );
1165
1166 let target = "1.2_a";
1167 assert_eq!(
1168 NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
1169 NumericPrefixWithSuffix(Some(1), ".2_a")
1170 );
1171
1172 let target = "12.2_a";
1173 assert_eq!(
1174 NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
1175 NumericPrefixWithSuffix(Some(12), ".2_a")
1176 );
1177
1178 let target = "12a.2_a";
1179 assert_eq!(
1180 NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
1181 NumericPrefixWithSuffix(Some(12), "a.2_a")
1182 );
1183 }
1184
1185 #[test]
1186 fn test_numeric_prefix_with_suffix() {
1187 let mut sorted = vec!["1-abc", "10", "11def", "2", "21-abc"];
1188 sorted.sort_by_key(|s| NumericPrefixWithSuffix::from_numeric_prefixed_str(s));
1189 assert_eq!(sorted, ["1-abc", "2", "10", "11def", "21-abc"]);
1190
1191 for numeric_prefix_less in ["numeric_prefix_less", "aaa", "~β’Β£"] {
1192 assert_eq!(
1193 NumericPrefixWithSuffix::from_numeric_prefixed_str(numeric_prefix_less),
1194 NumericPrefixWithSuffix(None, numeric_prefix_less),
1195 "String without numeric prefix `{numeric_prefix_less}` should not be converted into NumericPrefixWithSuffix"
1196 )
1197 }
1198 }
1199
1200 #[test]
1201 fn test_word_consists_of_emojis() {
1202 let words_to_test = vec![
1203 ("π¨βπ©βπ§βπ§ππ₯", true),
1204 ("π", true),
1205 ("!π", false),
1206 ("π!", false),
1207 ("π ", false),
1208 (" π", false),
1209 ("Test", false),
1210 ];
1211
1212 for (text, expected_result) in words_to_test {
1213 assert_eq!(word_consists_of_emojis(text), expected_result);
1214 }
1215 }
1216
1217 #[test]
1218 fn test_truncate_lines_and_trailoff() {
1219 let text = r#"Line 1
1220Line 2
1221Line 3"#;
1222
1223 assert_eq!(
1224 truncate_lines_and_trailoff(text, 2),
1225 r#"Line 1
1226β¦"#
1227 );
1228
1229 assert_eq!(
1230 truncate_lines_and_trailoff(text, 3),
1231 r#"Line 1
1232Line 2
1233β¦"#
1234 );
1235
1236 assert_eq!(
1237 truncate_lines_and_trailoff(text, 4),
1238 r#"Line 1
1239Line 2
1240Line 3"#
1241 );
1242 }
1243
1244 #[test]
1245 fn test_expanded_and_wrapped_usize_range() {
1246 // Neither wrap
1247 assert_eq!(
1248 expanded_and_wrapped_usize_range(2..4, 1, 1, 8).collect::<Vec<usize>>(),
1249 (1..5).collect::<Vec<usize>>()
1250 );
1251 // Start wraps
1252 assert_eq!(
1253 expanded_and_wrapped_usize_range(2..4, 3, 1, 8).collect::<Vec<usize>>(),
1254 ((0..5).chain(7..8)).collect::<Vec<usize>>()
1255 );
1256 // Start wraps all the way around
1257 assert_eq!(
1258 expanded_and_wrapped_usize_range(2..4, 5, 1, 8).collect::<Vec<usize>>(),
1259 (0..8).collect::<Vec<usize>>()
1260 );
1261 // Start wraps all the way around and past 0
1262 assert_eq!(
1263 expanded_and_wrapped_usize_range(2..4, 10, 1, 8).collect::<Vec<usize>>(),
1264 (0..8).collect::<Vec<usize>>()
1265 );
1266 // End wraps
1267 assert_eq!(
1268 expanded_and_wrapped_usize_range(3..5, 1, 4, 8).collect::<Vec<usize>>(),
1269 (0..1).chain(2..8).collect::<Vec<usize>>()
1270 );
1271 // End wraps all the way around
1272 assert_eq!(
1273 expanded_and_wrapped_usize_range(3..5, 1, 5, 8).collect::<Vec<usize>>(),
1274 (0..8).collect::<Vec<usize>>()
1275 );
1276 // End wraps all the way around and past the end
1277 assert_eq!(
1278 expanded_and_wrapped_usize_range(3..5, 1, 10, 8).collect::<Vec<usize>>(),
1279 (0..8).collect::<Vec<usize>>()
1280 );
1281 // Both start and end wrap
1282 assert_eq!(
1283 expanded_and_wrapped_usize_range(3..5, 4, 4, 8).collect::<Vec<usize>>(),
1284 (0..8).collect::<Vec<usize>>()
1285 );
1286 }
1287
1288 #[test]
1289 fn test_wrapped_usize_outward_from() {
1290 // No wrapping
1291 assert_eq!(
1292 wrapped_usize_outward_from(4, 2, 2, 10).collect::<Vec<usize>>(),
1293 vec![4, 5, 3, 6, 2]
1294 );
1295 // Wrapping at end
1296 assert_eq!(
1297 wrapped_usize_outward_from(8, 2, 3, 10).collect::<Vec<usize>>(),
1298 vec![8, 9, 7, 0, 6, 1]
1299 );
1300 // Wrapping at start
1301 assert_eq!(
1302 wrapped_usize_outward_from(1, 3, 2, 10).collect::<Vec<usize>>(),
1303 vec![1, 2, 0, 3, 9, 8]
1304 );
1305 // All values wrap around
1306 assert_eq!(
1307 wrapped_usize_outward_from(5, 10, 10, 8).collect::<Vec<usize>>(),
1308 vec![5, 6, 4, 7, 3, 0, 2, 1]
1309 );
1310 // None before / after
1311 assert_eq!(
1312 wrapped_usize_outward_from(3, 0, 0, 8).collect::<Vec<usize>>(),
1313 vec![3]
1314 );
1315 // Starting point already wrapped
1316 assert_eq!(
1317 wrapped_usize_outward_from(15, 2, 2, 10).collect::<Vec<usize>>(),
1318 vec![5, 6, 4, 7, 3]
1319 );
1320 // wrap_length of 0
1321 assert_eq!(
1322 wrapped_usize_outward_from(4, 2, 2, 0).collect::<Vec<usize>>(),
1323 Vec::<usize>::new()
1324 );
1325 }
1326
1327 #[test]
1328 fn test_split_with_ranges() {
1329 let input = "hi";
1330 let result = split_str_with_ranges(input, |c| c == ' ');
1331
1332 assert_eq!(result.len(), 1);
1333 assert_eq!(result[0], (0..2, "hi"));
1334
1335 let input = "hΓ©lloπ¦world";
1336 let result = split_str_with_ranges(input, |c| c == 'π¦');
1337
1338 assert_eq!(result.len(), 2);
1339 assert_eq!(result[0], (0..6, "hΓ©llo")); // 'Γ©' is 2 bytes
1340 assert_eq!(result[1], (10..15, "world")); // 'π¦' is 4 bytes
1341 }
1342}