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