2023-09-07 00:22:29 +03:00
|
|
|
|
pub mod arc_cow;
|
2023-03-23 05:22:08 +03:00
|
|
|
|
pub mod fs;
|
2022-11-14 22:25:11 +03:00
|
|
|
|
pub mod paths;
|
2024-05-02 12:30:45 +03:00
|
|
|
|
pub mod serde;
|
2022-08-04 00:20:05 +03:00
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2021-10-04 22:07:35 +03:00
|
|
|
|
pub mod test;
|
|
|
|
|
|
2024-01-22 13:37:14 +03:00
|
|
|
|
use futures::Future;
|
|
|
|
|
use rand::{seq::SliceRandom, Rng};
|
2024-06-18 19:44:58 +03:00
|
|
|
|
use regex::Regex;
|
|
|
|
|
use std::sync::OnceLock;
|
2021-08-24 17:15:24 +03:00
|
|
|
|
use std::{
|
2023-06-06 21:46:46 +03:00
|
|
|
|
borrow::Cow,
|
2023-02-18 09:00:39 +03:00
|
|
|
|
cmp::{self, Ordering},
|
2024-01-22 13:37:14 +03:00
|
|
|
|
env,
|
2023-02-18 09:00:39 +03:00
|
|
|
|
ops::{AddAssign, Range, RangeInclusive},
|
2023-06-05 22:53:37 +03:00
|
|
|
|
panic::Location,
|
2021-08-24 17:15:24 +03:00
|
|
|
|
pin::Pin,
|
|
|
|
|
task::{Context, Poll},
|
2024-01-22 13:37:14 +03:00
|
|
|
|
time::Instant,
|
2021-08-24 17:15:24 +03:00
|
|
|
|
};
|
2024-02-28 12:03:34 +03:00
|
|
|
|
use unicase::UniCase;
|
2021-05-15 03:45:16 +03:00
|
|
|
|
|
2023-05-11 22:01:42 +03:00
|
|
|
|
pub use take_until::*;
|
|
|
|
|
|
2022-11-30 21:03:15 +03:00
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! debug_panic {
|
|
|
|
|
( $($fmt_arg:tt)* ) => {
|
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
|
panic!( $($fmt_arg)* );
|
|
|
|
|
} else {
|
2024-03-13 02:10:49 +03:00
|
|
|
|
let backtrace = std::backtrace::Backtrace::capture();
|
2022-11-30 21:03:15 +03:00
|
|
|
|
log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 08:07:29 +03:00
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! with_clone {
|
|
|
|
|
($i:ident, move ||$l:expr) => {{
|
|
|
|
|
let $i = $i.clone();
|
|
|
|
|
move || {
|
|
|
|
|
$l
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
($i:ident, move |$($k:pat_param),*|$l:expr) => {{
|
|
|
|
|
let $i = $i.clone();
|
|
|
|
|
move |$( $k ),*| {
|
|
|
|
|
$l
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
(($($i:ident),+), move ||$l:expr) => {{
|
|
|
|
|
let ($($i),+) = ($($i.clone()),+);
|
|
|
|
|
move || {
|
|
|
|
|
$l
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
(($($i:ident),+), move |$($k:pat_param),*|$l:expr) => {{
|
|
|
|
|
let ($($i),+) = ($($i.clone()),+);
|
|
|
|
|
move |$( $k ),*| {
|
|
|
|
|
$l
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mod test_with_clone {
|
|
|
|
|
|
|
|
|
|
// If this test compiles, it works
|
|
|
|
|
#[test]
|
|
|
|
|
fn test() {
|
|
|
|
|
let x = "String".to_string();
|
|
|
|
|
let y = std::sync::Arc::new(5);
|
|
|
|
|
|
|
|
|
|
fn no_arg(f: impl FnOnce()) {
|
|
|
|
|
f()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
no_arg(with_clone!(x, move || {
|
|
|
|
|
drop(x);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
no_arg(with_clone!((x, y), move || {
|
|
|
|
|
drop(x);
|
|
|
|
|
drop(y);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
fn one_arg(f: impl FnOnce(usize)) {
|
|
|
|
|
f(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
one_arg(with_clone!(x, move |_| {
|
|
|
|
|
drop(x);
|
|
|
|
|
}));
|
|
|
|
|
one_arg(with_clone!((x, y), move |b| {
|
|
|
|
|
drop(x);
|
|
|
|
|
drop(y);
|
|
|
|
|
println!("{}", b);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
fn two_arg(f: impl FnOnce(usize, bool)) {
|
|
|
|
|
f(5, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
two_arg(with_clone!((x, y), move |a, b| {
|
|
|
|
|
drop(x);
|
|
|
|
|
drop(y);
|
|
|
|
|
println!("{}{}", a, b)
|
|
|
|
|
}));
|
|
|
|
|
two_arg(with_clone!((x, y), move |a, _| {
|
|
|
|
|
drop(x);
|
|
|
|
|
drop(y);
|
|
|
|
|
println!("{}", a)
|
|
|
|
|
}));
|
|
|
|
|
two_arg(with_clone!((x, y), move |_, b| {
|
|
|
|
|
drop(x);
|
|
|
|
|
drop(y);
|
|
|
|
|
println!("{}", b)
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
struct Example {
|
|
|
|
|
z: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn destructuring_example(f: impl FnOnce(Example)) {
|
|
|
|
|
f(Example { z: 10 })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destructuring_example(with_clone!(x, move |Example { z }| {
|
|
|
|
|
drop(x);
|
|
|
|
|
println!("{}", z);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
let a_long_variable_1 = "".to_string();
|
|
|
|
|
let a_long_variable_2 = "".to_string();
|
|
|
|
|
let a_long_variable_3 = "".to_string();
|
|
|
|
|
let a_long_variable_4 = "".to_string();
|
|
|
|
|
two_arg(with_clone!(
|
|
|
|
|
(
|
|
|
|
|
x,
|
|
|
|
|
y,
|
|
|
|
|
a_long_variable_1,
|
|
|
|
|
a_long_variable_2,
|
|
|
|
|
a_long_variable_3,
|
|
|
|
|
a_long_variable_4
|
|
|
|
|
),
|
|
|
|
|
move |a, b| {
|
|
|
|
|
drop(x);
|
|
|
|
|
drop(y);
|
|
|
|
|
drop(a_long_variable_1);
|
|
|
|
|
drop(a_long_variable_2);
|
|
|
|
|
drop(a_long_variable_3);
|
|
|
|
|
drop(a_long_variable_4);
|
|
|
|
|
println!("{}{}", a, b)
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
fn single_expression_body(f: impl FnOnce(usize) -> usize) -> usize {
|
|
|
|
|
f(20)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let _result = single_expression_body(with_clone!(y, move |z| *y + z));
|
|
|
|
|
|
|
|
|
|
// Explicitly move all variables
|
|
|
|
|
drop(x);
|
|
|
|
|
drop(y);
|
|
|
|
|
drop(a_long_variable_1);
|
|
|
|
|
drop(a_long_variable_2);
|
|
|
|
|
drop(a_long_variable_3);
|
|
|
|
|
drop(a_long_variable_4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 00:45:45 +03:00
|
|
|
|
pub fn truncate(s: &str, max_chars: usize) -> &str {
|
|
|
|
|
match s.char_indices().nth(max_chars) {
|
|
|
|
|
None => s,
|
|
|
|
|
Some((idx, _)) => &s[..idx],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 18:09:48 +03:00
|
|
|
|
/// Removes characters from the end of the string if its length is greater than `max_chars` and
|
|
|
|
|
/// appends "..." to the string. Returns string unchanged if its length is smaller than max_chars.
|
2022-09-04 00:45:45 +03:00
|
|
|
|
pub fn truncate_and_trailoff(s: &str, max_chars: usize) -> String {
|
|
|
|
|
debug_assert!(max_chars >= 5);
|
|
|
|
|
|
2023-02-07 21:06:20 +03:00
|
|
|
|
let truncation_ix = s.char_indices().map(|(i, _)| i).nth(max_chars);
|
|
|
|
|
match truncation_ix {
|
|
|
|
|
Some(length) => s[..length].to_string() + "…",
|
|
|
|
|
None => s.to_string(),
|
2022-09-04 00:45:45 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-10 18:09:48 +03:00
|
|
|
|
/// Removes characters from the front of the string if its length is greater than `max_chars` and
|
|
|
|
|
/// prepends the string with "...". Returns string unchanged if its length is smaller than max_chars.
|
2023-09-25 17:15:29 +03:00
|
|
|
|
pub fn truncate_and_remove_front(s: &str, max_chars: usize) -> String {
|
|
|
|
|
debug_assert!(max_chars >= 5);
|
|
|
|
|
|
|
|
|
|
let truncation_ix = s.char_indices().map(|(i, _)| i).nth_back(max_chars);
|
|
|
|
|
match truncation_ix {
|
|
|
|
|
Some(length) => "…".to_string() + &s[length..],
|
|
|
|
|
None => s.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 22:19:04 +03:00
|
|
|
|
/// Takes only `max_lines` from the string and, if there were more than `max_lines-1`, appends a
|
|
|
|
|
/// a newline and "..." to the string, so that `max_lines` are returned.
|
|
|
|
|
/// Returns string unchanged if its length is smaller than max_lines.
|
|
|
|
|
pub fn truncate_lines_and_trailoff(s: &str, max_lines: usize) -> String {
|
|
|
|
|
let mut lines = s.lines().take(max_lines).collect::<Vec<_>>();
|
|
|
|
|
if lines.len() > max_lines - 1 {
|
|
|
|
|
lines.pop();
|
|
|
|
|
lines.join("\n") + "\n…"
|
|
|
|
|
} else {
|
|
|
|
|
lines.join("\n")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 03:19:29 +03:00
|
|
|
|
pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
|
2021-03-18 22:13:31 +03:00
|
|
|
|
let prev = *value;
|
2022-02-15 03:19:29 +03:00
|
|
|
|
*value += T::from(1);
|
2021-03-18 22:13:31 +03:00
|
|
|
|
prev
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 00:02:55 +03:00
|
|
|
|
/// Extend a sorted vector with a sorted sequence of items, maintaining the vector's sort order and
|
2023-06-05 21:50:23 +03:00
|
|
|
|
/// enforcing a maximum length. This also de-duplicates items. Sort the items according to the given callback. Before calling this,
|
2021-04-28 00:02:55 +03:00
|
|
|
|
/// both `vec` and `new_items` should already be sorted according to the `cmp` comparator.
|
|
|
|
|
pub fn extend_sorted<T, I, F>(vec: &mut Vec<T>, new_items: I, limit: usize, mut cmp: F)
|
|
|
|
|
where
|
|
|
|
|
I: IntoIterator<Item = T>,
|
|
|
|
|
F: FnMut(&T, &T) -> Ordering,
|
|
|
|
|
{
|
|
|
|
|
let mut start_index = 0;
|
|
|
|
|
for new_item in new_items {
|
|
|
|
|
if let Err(i) = vec[start_index..].binary_search_by(|m| cmp(m, &new_item)) {
|
|
|
|
|
let index = start_index + i;
|
|
|
|
|
if vec.len() < limit {
|
|
|
|
|
vec.insert(index, new_item);
|
|
|
|
|
} else if index < vec.len() {
|
|
|
|
|
vec.pop();
|
|
|
|
|
vec.insert(index, new_item);
|
|
|
|
|
}
|
|
|
|
|
start_index = index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-28 18:45:59 +03:00
|
|
|
|
|
2024-04-06 05:24:46 +03:00
|
|
|
|
/// Parse the result of calling `usr/bin/env` with no arguments
|
|
|
|
|
pub fn parse_env_output(env: &str, mut f: impl FnMut(String, String)) {
|
|
|
|
|
let mut current_key: Option<String> = None;
|
|
|
|
|
let mut current_value: Option<String> = None;
|
|
|
|
|
|
|
|
|
|
for line in env.split_terminator('\n') {
|
|
|
|
|
if let Some(separator_index) = line.find('=') {
|
|
|
|
|
if &line[..separator_index] != "" {
|
|
|
|
|
if let Some((key, value)) = Option::zip(current_key.take(), current_value.take()) {
|
|
|
|
|
f(key, value)
|
|
|
|
|
}
|
|
|
|
|
current_key = Some(line[..separator_index].to_string());
|
|
|
|
|
current_value = Some(line[separator_index + 1..].to_string());
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if let Some(value) = current_value.as_mut() {
|
|
|
|
|
value.push('\n');
|
|
|
|
|
value.push_str(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Some((key, value)) = Option::zip(current_key.take(), current_value.take()) {
|
|
|
|
|
f(key, value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 16:34:48 +03:00
|
|
|
|
pub fn merge_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
|
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
|
|
match (source, target) {
|
|
|
|
|
(Value::Object(source), Value::Object(target)) => {
|
|
|
|
|
for (key, value) in source {
|
|
|
|
|
if let Some(target) = target.get_mut(&key) {
|
|
|
|
|
merge_json_value_into(value, target);
|
|
|
|
|
} else {
|
|
|
|
|
target.insert(key.clone(), value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(source, target) => *target = source,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 02:59:44 +03:00
|
|
|
|
pub fn merge_non_null_json_value_into(source: serde_json::Value, target: &mut serde_json::Value) {
|
|
|
|
|
use serde_json::Value;
|
|
|
|
|
if let Value::Object(source_object) = source {
|
|
|
|
|
let target_object = if let Value::Object(target) = target {
|
|
|
|
|
target
|
|
|
|
|
} else {
|
|
|
|
|
*target = Value::Object(Default::default());
|
|
|
|
|
target.as_object_mut().unwrap()
|
|
|
|
|
};
|
|
|
|
|
for (key, value) in source_object {
|
|
|
|
|
if let Some(target) = target_object.get_mut(&key) {
|
|
|
|
|
merge_non_null_json_value_into(value, target);
|
|
|
|
|
} else if !value.is_null() {
|
|
|
|
|
target_object.insert(key.clone(), value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if !source.is_null() {
|
|
|
|
|
*target = source
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 13:37:14 +03:00
|
|
|
|
pub fn measure<R>(label: &str, f: impl FnOnce() -> R) -> R {
|
2024-06-18 19:44:58 +03:00
|
|
|
|
static ZED_MEASUREMENTS: OnceLock<bool> = OnceLock::new();
|
|
|
|
|
let zed_measurements = ZED_MEASUREMENTS.get_or_init(|| {
|
|
|
|
|
env::var("ZED_MEASUREMENTS")
|
2024-01-22 13:37:14 +03:00
|
|
|
|
.map(|measurements| measurements == "1" || measurements == "true")
|
2024-06-18 19:44:58 +03:00
|
|
|
|
.unwrap_or(false)
|
|
|
|
|
});
|
2024-01-22 13:37:14 +03:00
|
|
|
|
|
2024-06-18 19:44:58 +03:00
|
|
|
|
if *zed_measurements {
|
2024-01-22 13:37:14 +03:00
|
|
|
|
let start = Instant::now();
|
|
|
|
|
let result = f();
|
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
|
eprintln!("{}: {:?}", label, elapsed);
|
|
|
|
|
result
|
|
|
|
|
} else {
|
|
|
|
|
f()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-12 21:25:45 +03:00
|
|
|
|
pub trait ResultExt<E> {
|
2021-08-24 17:15:24 +03:00
|
|
|
|
type Ok;
|
|
|
|
|
|
|
|
|
|
fn log_err(self) -> Option<Self::Ok>;
|
2024-01-12 00:41:28 +03:00
|
|
|
|
/// Assert that this result should never be an error in development or tests.
|
|
|
|
|
fn debug_assert_ok(self, reason: &str) -> Self;
|
2021-10-05 01:47:18 +03:00
|
|
|
|
fn warn_on_err(self) -> Option<Self::Ok>;
|
2021-08-24 17:15:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-12 21:25:45 +03:00
|
|
|
|
impl<T, E> ResultExt<E> for Result<T, E>
|
2021-12-18 18:12:59 +03:00
|
|
|
|
where
|
|
|
|
|
E: std::fmt::Debug,
|
|
|
|
|
{
|
2021-08-24 17:15:24 +03:00
|
|
|
|
type Ok = T;
|
|
|
|
|
|
2023-06-05 22:53:37 +03:00
|
|
|
|
#[track_caller]
|
2021-08-24 17:15:24 +03:00
|
|
|
|
fn log_err(self) -> Option<T> {
|
|
|
|
|
match self {
|
|
|
|
|
Ok(value) => Some(value),
|
|
|
|
|
Err(error) => {
|
2024-06-28 03:03:47 +03:00
|
|
|
|
log_error_with_caller(*Location::caller(), error, log::Level::Error);
|
2021-08-24 17:15:24 +03:00
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-05 01:47:18 +03:00
|
|
|
|
|
2024-01-12 00:41:28 +03:00
|
|
|
|
#[track_caller]
|
|
|
|
|
fn debug_assert_ok(self, reason: &str) -> Self {
|
|
|
|
|
if let Err(error) = &self {
|
|
|
|
|
debug_panic!("{reason} - {error:?}");
|
|
|
|
|
}
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-28 03:03:47 +03:00
|
|
|
|
#[track_caller]
|
2021-10-05 01:47:18 +03:00
|
|
|
|
fn warn_on_err(self) -> Option<T> {
|
|
|
|
|
match self {
|
|
|
|
|
Ok(value) => Some(value),
|
|
|
|
|
Err(error) => {
|
2024-06-28 03:03:47 +03:00
|
|
|
|
log_error_with_caller(*Location::caller(), error, log::Level::Warn);
|
2021-10-05 01:47:18 +03:00
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-28 03:03:47 +03:00
|
|
|
|
}
|
2023-06-12 21:25:45 +03:00
|
|
|
|
|
2024-06-28 03:03:47 +03:00
|
|
|
|
fn log_error_with_caller<E>(caller: core::panic::Location<'_>, error: E, level: log::Level)
|
|
|
|
|
where
|
|
|
|
|
E: std::fmt::Debug,
|
|
|
|
|
{
|
|
|
|
|
// In this codebase, the first segment of the file path is
|
|
|
|
|
// the 'crates' folder, followed by the crate name.
|
|
|
|
|
let target = caller.file().split('/').nth(1);
|
|
|
|
|
|
|
|
|
|
log::logger().log(
|
|
|
|
|
&log::Record::builder()
|
|
|
|
|
.target(target.unwrap_or(""))
|
|
|
|
|
.module_path(target)
|
|
|
|
|
.args(format_args!("{:?}", error))
|
|
|
|
|
.file(Some(caller.file()))
|
|
|
|
|
.line(Some(caller.line()))
|
|
|
|
|
.level(level)
|
|
|
|
|
.build(),
|
|
|
|
|
);
|
2021-08-24 17:15:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait TryFutureExt {
|
|
|
|
|
fn log_err(self) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
2023-12-08 04:16:19 +03:00
|
|
|
|
|
|
|
|
|
fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
|
|
|
|
|
2021-10-05 01:47:18 +03:00
|
|
|
|
fn warn_on_err(self) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
2023-03-10 13:41:13 +03:00
|
|
|
|
fn unwrap(self) -> UnwrapFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
2021-08-24 17:15:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 13:41:13 +03:00
|
|
|
|
impl<F, T, E> TryFutureExt for F
|
2021-08-19 04:06:46 +03:00
|
|
|
|
where
|
2023-03-10 13:41:13 +03:00
|
|
|
|
F: Future<Output = Result<T, E>>,
|
|
|
|
|
E: std::fmt::Debug,
|
2021-08-19 04:06:46 +03:00
|
|
|
|
{
|
2023-12-08 04:16:19 +03:00
|
|
|
|
#[track_caller]
|
2021-08-24 17:15:24 +03:00
|
|
|
|
fn log_err(self) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
2023-12-08 04:16:19 +03:00
|
|
|
|
let location = Location::caller();
|
|
|
|
|
LogErrorFuture(self, log::Level::Error, *location)
|
2021-10-05 01:47:18 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 04:16:19 +03:00
|
|
|
|
fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
LogErrorFuture(self, log::Level::Error, location)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[track_caller]
|
2021-10-05 01:47:18 +03:00
|
|
|
|
fn warn_on_err(self) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
2023-12-08 04:16:19 +03:00
|
|
|
|
let location = Location::caller();
|
|
|
|
|
LogErrorFuture(self, log::Level::Warn, *location)
|
2021-08-24 17:15:24 +03:00
|
|
|
|
}
|
2023-03-10 13:41:13 +03:00
|
|
|
|
|
|
|
|
|
fn unwrap(self) -> UnwrapFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
UnwrapFuture(self)
|
|
|
|
|
}
|
2021-08-24 17:15:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-12 03:22:34 +03:00
|
|
|
|
#[must_use]
|
2023-12-08 04:16:19 +03:00
|
|
|
|
pub struct LogErrorFuture<F>(F, log::Level, core::panic::Location<'static>);
|
2021-08-24 17:15:24 +03:00
|
|
|
|
|
2023-03-10 13:41:13 +03:00
|
|
|
|
impl<F, T, E> Future for LogErrorFuture<F>
|
2021-08-24 17:15:24 +03:00
|
|
|
|
where
|
2023-03-10 13:41:13 +03:00
|
|
|
|
F: Future<Output = Result<T, E>>,
|
|
|
|
|
E: std::fmt::Debug,
|
2021-08-24 17:15:24 +03:00
|
|
|
|
{
|
|
|
|
|
type Output = Option<T>;
|
|
|
|
|
|
2023-03-10 13:41:13 +03:00
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
2021-10-05 01:47:18 +03:00
|
|
|
|
let level = self.1;
|
2023-12-08 04:16:19 +03:00
|
|
|
|
let location = self.2;
|
2021-08-24 17:15:24 +03:00
|
|
|
|
let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
|
|
|
|
|
match inner.poll(cx) {
|
2021-10-05 01:47:18 +03:00
|
|
|
|
Poll::Ready(output) => Poll::Ready(match output {
|
|
|
|
|
Ok(output) => Some(output),
|
|
|
|
|
Err(error) => {
|
2024-06-28 03:03:47 +03:00
|
|
|
|
log_error_with_caller(location, error, level);
|
2021-10-05 01:47:18 +03:00
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}),
|
2021-08-24 17:15:24 +03:00
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
|
}
|
2021-08-19 04:06:46 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 13:41:13 +03:00
|
|
|
|
pub struct UnwrapFuture<F>(F);
|
|
|
|
|
|
|
|
|
|
impl<F, T, E> Future for UnwrapFuture<F>
|
|
|
|
|
where
|
|
|
|
|
F: Future<Output = Result<T, E>>,
|
|
|
|
|
E: std::fmt::Debug,
|
|
|
|
|
{
|
|
|
|
|
type Output = T;
|
|
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
|
let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
|
|
|
|
|
match inner.poll(cx) {
|
|
|
|
|
Poll::Ready(result) => Poll::Ready(result.unwrap()),
|
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 00:22:29 +03:00
|
|
|
|
pub struct Deferred<F: FnOnce()>(Option<F>);
|
2022-03-05 00:47:06 +03:00
|
|
|
|
|
2023-09-07 00:22:29 +03:00
|
|
|
|
impl<F: FnOnce()> Deferred<F> {
|
|
|
|
|
/// Drop without running the deferred function.
|
2024-01-19 18:28:27 +03:00
|
|
|
|
pub fn abort(mut self) {
|
2023-09-07 00:22:29 +03:00
|
|
|
|
self.0.take();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<F: FnOnce()> Drop for Deferred<F> {
|
2022-03-05 00:47:06 +03:00
|
|
|
|
fn drop(&mut self) {
|
2022-08-11 00:39:24 +03:00
|
|
|
|
if let Some(f) = self.0.take() {
|
|
|
|
|
f()
|
|
|
|
|
}
|
2022-03-05 00:47:06 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 00:22:29 +03:00
|
|
|
|
/// Run the given function when the returned value is dropped (unless it's cancelled).
|
2024-04-03 00:31:58 +03:00
|
|
|
|
#[must_use]
|
2023-09-07 00:22:29 +03:00
|
|
|
|
pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
|
|
|
|
|
Deferred(Some(f))
|
2022-03-05 00:47:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 23:01:28 +03:00
|
|
|
|
pub struct RandomCharIter<T: Rng> {
|
|
|
|
|
rng: T,
|
|
|
|
|
simple_text: bool,
|
|
|
|
|
}
|
2022-10-12 01:25:54 +03:00
|
|
|
|
|
|
|
|
|
impl<T: Rng> RandomCharIter<T> {
|
|
|
|
|
pub fn new(rng: T) -> Self {
|
2023-08-30 23:01:28 +03:00
|
|
|
|
Self {
|
|
|
|
|
rng,
|
|
|
|
|
simple_text: std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn with_simple_text(mut self) -> Self {
|
|
|
|
|
self.simple_text = true;
|
|
|
|
|
self
|
2022-10-12 01:25:54 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Rng> Iterator for RandomCharIter<T> {
|
|
|
|
|
type Item = char;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2023-08-30 23:01:28 +03:00
|
|
|
|
if self.simple_text {
|
|
|
|
|
return if self.rng.gen_range(0..100) < 5 {
|
2022-10-12 01:25:54 +03:00
|
|
|
|
Some('\n')
|
|
|
|
|
} else {
|
2023-08-30 23:01:28 +03:00
|
|
|
|
Some(self.rng.gen_range(b'a'..b'z' + 1).into())
|
2022-10-12 01:25:54 +03:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 23:01:28 +03:00
|
|
|
|
match self.rng.gen_range(0..100) {
|
2022-10-12 01:25:54 +03:00
|
|
|
|
// whitespace
|
2023-08-30 23:01:28 +03:00
|
|
|
|
0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.rng).copied(),
|
2022-10-12 01:25:54 +03:00
|
|
|
|
// two-byte greek letters
|
2023-08-30 23:01:28 +03:00
|
|
|
|
20..=32 => char::from_u32(self.rng.gen_range(('α' as u32)..('ω' as u32 + 1))),
|
2022-10-12 01:25:54 +03:00
|
|
|
|
// // three-byte characters
|
2023-08-30 23:01:28 +03:00
|
|
|
|
33..=45 => ['✋', '✅', '❌', '❎', '⭐']
|
|
|
|
|
.choose(&mut self.rng)
|
|
|
|
|
.copied(),
|
2022-10-12 01:25:54 +03:00
|
|
|
|
// // four-byte characters
|
2023-08-30 23:01:28 +03:00
|
|
|
|
46..=58 => ['🍐', '🏀', '🍗', '🎉'].choose(&mut self.rng).copied(),
|
2022-10-12 01:25:54 +03:00
|
|
|
|
// ascii letters
|
2023-08-30 23:01:28 +03:00
|
|
|
|
_ => Some(self.rng.gen_range(b'a'..b'z' + 1).into()),
|
2022-10-12 01:25:54 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 21:46:46 +03:00
|
|
|
|
/// Get an embedded file as a string.
|
|
|
|
|
pub fn asset_str<A: rust_embed::RustEmbed>(path: &str) -> Cow<'static, str> {
|
|
|
|
|
match A::get(path).unwrap().data {
|
|
|
|
|
Cow::Borrowed(bytes) => Cow::Borrowed(std::str::from_utf8(bytes).unwrap()),
|
|
|
|
|
Cow::Owned(bytes) => Cow::Owned(String::from_utf8(bytes).unwrap()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 03:29:06 +03:00
|
|
|
|
/// Expands to an immediately-invoked function expression. Good for using the ? operator
|
|
|
|
|
/// in functions which do not return an Option or Result.
|
|
|
|
|
///
|
|
|
|
|
/// Accepts a normal block, an async block, or an async move block.
|
2022-11-02 23:26:23 +03:00
|
|
|
|
#[macro_export]
|
2023-10-25 17:10:21 +03:00
|
|
|
|
macro_rules! maybe {
|
2022-11-02 23:26:23 +03:00
|
|
|
|
($block:block) => {
|
|
|
|
|
(|| $block)()
|
|
|
|
|
};
|
2024-03-23 03:29:06 +03:00
|
|
|
|
(async $block:block) => {
|
|
|
|
|
(|| async $block)()
|
|
|
|
|
};
|
|
|
|
|
(async move $block:block) => {
|
|
|
|
|
(|| async move $block)()
|
2022-12-02 01:16:38 +03:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 09:00:39 +03:00
|
|
|
|
pub trait RangeExt<T> {
|
|
|
|
|
fn sorted(&self) -> Self;
|
|
|
|
|
fn to_inclusive(&self) -> RangeInclusive<T>;
|
|
|
|
|
fn overlaps(&self, other: &Range<T>) -> bool;
|
2023-03-30 20:40:53 +03:00
|
|
|
|
fn contains_inclusive(&self, other: &Range<T>) -> bool;
|
2023-02-18 09:00:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Ord + Clone> RangeExt<T> for Range<T> {
|
|
|
|
|
fn sorted(&self) -> Self {
|
|
|
|
|
cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_inclusive(&self) -> RangeInclusive<T> {
|
|
|
|
|
self.start.clone()..=self.end.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlaps(&self, other: &Range<T>) -> bool {
|
2023-02-27 22:38:09 +03:00
|
|
|
|
self.start < other.end && other.start < self.end
|
2023-02-18 09:00:39 +03:00
|
|
|
|
}
|
2023-03-30 20:40:53 +03:00
|
|
|
|
|
|
|
|
|
fn contains_inclusive(&self, other: &Range<T>) -> bool {
|
|
|
|
|
self.start <= other.start && other.end <= self.end
|
|
|
|
|
}
|
2023-02-18 09:00:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Ord + Clone> RangeExt<T> for RangeInclusive<T> {
|
|
|
|
|
fn sorted(&self) -> Self {
|
|
|
|
|
cmp::min(self.start(), self.end()).clone()..=cmp::max(self.start(), self.end()).clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_inclusive(&self) -> RangeInclusive<T> {
|
|
|
|
|
self.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn overlaps(&self, other: &Range<T>) -> bool {
|
2023-02-27 22:38:09 +03:00
|
|
|
|
self.start() < &other.end && &other.start <= self.end()
|
2023-02-18 09:00:39 +03:00
|
|
|
|
}
|
2023-03-30 20:40:53 +03:00
|
|
|
|
|
|
|
|
|
fn contains_inclusive(&self, other: &Range<T>) -> bool {
|
|
|
|
|
self.start() <= &other.start && &other.end <= self.end()
|
|
|
|
|
}
|
2023-02-18 09:00:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 12:03:34 +03:00
|
|
|
|
/// A way to sort strings with starting numbers numerically first, falling back to alphanumeric one,
|
|
|
|
|
/// case-insensitive.
|
|
|
|
|
///
|
|
|
|
|
/// This is useful for turning regular alphanumerically sorted sequences as `1-abc, 10, 11-def, .., 2, 21-abc`
|
|
|
|
|
/// into `1-abc, 2, 10, 11-def, .., 21-abc`
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
|
pub struct NumericPrefixWithSuffix<'a>(i32, &'a str);
|
|
|
|
|
|
|
|
|
|
impl<'a> NumericPrefixWithSuffix<'a> {
|
2024-02-28 12:04:01 +03:00
|
|
|
|
pub fn from_numeric_prefixed_str(str: &'a str) -> Option<Self> {
|
2024-04-08 21:05:03 +03:00
|
|
|
|
let i = str.chars().take_while(|c| c.is_ascii_digit()).count();
|
|
|
|
|
let (prefix, remainder) = str.split_at(i);
|
2024-02-28 12:03:34 +03:00
|
|
|
|
|
|
|
|
|
match prefix.parse::<i32>() {
|
|
|
|
|
Ok(prefix) => Some(NumericPrefixWithSuffix(prefix, remainder)),
|
|
|
|
|
Err(_) => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Ord for NumericPrefixWithSuffix<'_> {
|
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
|
let NumericPrefixWithSuffix(num_a, remainder_a) = self;
|
|
|
|
|
let NumericPrefixWithSuffix(num_b, remainder_b) = other;
|
|
|
|
|
num_a
|
2024-02-28 12:04:01 +03:00
|
|
|
|
.cmp(num_b)
|
2024-02-28 12:03:34 +03:00
|
|
|
|
.then_with(|| UniCase::new(remainder_a).cmp(&UniCase::new(remainder_b)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> PartialOrd for NumericPrefixWithSuffix<'a> {
|
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
|
Some(self.cmp(other))
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-18 19:44:58 +03:00
|
|
|
|
|
|
|
|
|
fn emoji_regex() -> &'static Regex {
|
|
|
|
|
static EMOJI_REGEX: OnceLock<Regex> = OnceLock::new();
|
|
|
|
|
EMOJI_REGEX.get_or_init(|| Regex::new("(\\p{Emoji}|\u{200D})").unwrap())
|
2024-03-11 18:08:18 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns true if the given string consists of emojis only.
|
|
|
|
|
/// E.g. "👨👩👧👧👋" will return true, but "👋!" will return false.
|
|
|
|
|
pub fn word_consists_of_emojis(s: &str) -> bool {
|
|
|
|
|
let mut prev_end = 0;
|
2024-06-18 19:44:58 +03:00
|
|
|
|
for capture in emoji_regex().find_iter(s) {
|
2024-03-11 18:08:18 +03:00
|
|
|
|
if capture.start() != prev_end {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
prev_end = capture.end();
|
|
|
|
|
}
|
|
|
|
|
prev_end == s.len()
|
|
|
|
|
}
|
2024-02-28 12:03:34 +03:00
|
|
|
|
|
2021-03-18 22:13:31 +03:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2021-04-28 00:02:55 +03:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_extend_sorted() {
|
|
|
|
|
let mut vec = vec![];
|
|
|
|
|
|
|
|
|
|
extend_sorted(&mut vec, vec![21, 17, 13, 8, 1, 0], 5, |a, b| b.cmp(a));
|
|
|
|
|
assert_eq!(vec, &[21, 17, 13, 8, 1]);
|
|
|
|
|
|
|
|
|
|
extend_sorted(&mut vec, vec![101, 19, 17, 8, 2], 8, |a, b| b.cmp(a));
|
|
|
|
|
assert_eq!(vec, &[101, 21, 19, 17, 13, 8, 2, 1]);
|
|
|
|
|
|
|
|
|
|
extend_sorted(&mut vec, vec![1000, 19, 17, 9, 5], 8, |a, b| b.cmp(a));
|
|
|
|
|
assert_eq!(vec, &[1000, 101, 21, 19, 17, 13, 9, 8]);
|
|
|
|
|
}
|
2022-11-02 23:26:23 +03:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_iife() {
|
|
|
|
|
fn option_returning_function() -> Option<()> {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 17:10:21 +03:00
|
|
|
|
let foo = maybe!({
|
2022-11-02 23:26:23 +03:00
|
|
|
|
option_returning_function()?;
|
|
|
|
|
Some(())
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
assert_eq!(foo, None);
|
|
|
|
|
}
|
2023-02-07 20:25:07 +03:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_trancate_and_trailoff() {
|
2023-02-07 21:06:20 +03:00
|
|
|
|
assert_eq!(truncate_and_trailoff("", 5), "");
|
|
|
|
|
assert_eq!(truncate_and_trailoff("èèèèèè", 7), "èèèèèè");
|
|
|
|
|
assert_eq!(truncate_and_trailoff("èèèèèè", 6), "èèèèèè");
|
|
|
|
|
assert_eq!(truncate_and_trailoff("èèèèèè", 5), "èèèèè…");
|
2023-02-07 20:25:07 +03:00
|
|
|
|
}
|
2024-02-28 12:03:34 +03:00
|
|
|
|
|
2024-04-08 21:05:03 +03:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_numeric_prefix_str_method() {
|
|
|
|
|
let target = "1a";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
|
|
|
|
|
Some(NumericPrefixWithSuffix(1, "a"))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let target = "12ab";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
|
|
|
|
|
Some(NumericPrefixWithSuffix(12, "ab"))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let target = "12_ab";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
|
|
|
|
|
Some(NumericPrefixWithSuffix(12, "_ab"))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let target = "1_2ab";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
|
|
|
|
|
Some(NumericPrefixWithSuffix(1, "_2ab"))
|
|
|
|
|
);
|
2024-04-11 01:47:53 +03:00
|
|
|
|
|
|
|
|
|
let target = "1.2";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
|
|
|
|
|
Some(NumericPrefixWithSuffix(1, ".2"))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let target = "1.2_a";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
|
|
|
|
|
Some(NumericPrefixWithSuffix(1, ".2_a"))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let target = "12.2_a";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
|
|
|
|
|
Some(NumericPrefixWithSuffix(12, ".2_a"))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let target = "12a.2_a";
|
|
|
|
|
assert_eq!(
|
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(target),
|
|
|
|
|
Some(NumericPrefixWithSuffix(12, "a.2_a"))
|
|
|
|
|
);
|
2024-04-08 21:05:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 12:03:34 +03:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_numeric_prefix_with_suffix() {
|
|
|
|
|
let mut sorted = vec!["1-abc", "10", "11def", "2", "21-abc"];
|
|
|
|
|
sorted.sort_by_key(|s| {
|
2024-02-28 12:04:01 +03:00
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(s).unwrap_or_else(|| {
|
2024-02-28 12:03:34 +03:00
|
|
|
|
panic!("Cannot convert string `{s}` into NumericPrefixWithSuffix")
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
assert_eq!(sorted, ["1-abc", "2", "10", "11def", "21-abc"]);
|
|
|
|
|
|
|
|
|
|
for numeric_prefix_less in ["numeric_prefix_less", "aaa", "~™£"] {
|
|
|
|
|
assert_eq!(
|
2024-02-28 12:04:01 +03:00
|
|
|
|
NumericPrefixWithSuffix::from_numeric_prefixed_str(numeric_prefix_less),
|
2024-02-28 12:03:34 +03:00
|
|
|
|
None,
|
|
|
|
|
"String without numeric prefix `{numeric_prefix_less}` should not be converted into NumericPrefixWithSuffix"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-11 18:08:18 +03:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_word_consists_of_emojis() {
|
|
|
|
|
let words_to_test = vec![
|
|
|
|
|
("👨👩👧👧👋🥒", true),
|
|
|
|
|
("👋", true),
|
|
|
|
|
("!👋", false),
|
|
|
|
|
("👋!", false),
|
|
|
|
|
("👋 ", false),
|
|
|
|
|
(" 👋", false),
|
|
|
|
|
("Test", false),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (text, expected_result) in words_to_test {
|
|
|
|
|
assert_eq!(word_consists_of_emojis(text), expected_result);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-28 22:19:04 +03:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_truncate_lines_and_trailoff() {
|
|
|
|
|
let text = r#"Line 1
|
|
|
|
|
Line 2
|
|
|
|
|
Line 3"#;
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
truncate_lines_and_trailoff(text, 2),
|
|
|
|
|
r#"Line 1
|
|
|
|
|
…"#
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
truncate_lines_and_trailoff(text, 3),
|
|
|
|
|
r#"Line 1
|
|
|
|
|
Line 2
|
|
|
|
|
…"#
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
truncate_lines_and_trailoff(text, 4),
|
|
|
|
|
r#"Line 1
|
|
|
|
|
Line 2
|
|
|
|
|
Line 3"#
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-03-18 22:13:31 +03:00
|
|
|
|
}
|