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