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