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