2022-11-15 00:18:44 +03:00
|
|
|
|
pub mod channel;
|
2022-11-14 22:25:11 +03:00
|
|
|
|
pub mod paths;
|
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;
|
|
|
|
|
|
2022-11-30 21:03:15 +03:00
|
|
|
|
pub use backtrace::Backtrace;
|
2021-10-02 00:55:21 +03:00
|
|
|
|
use futures::Future;
|
2022-10-12 01:25:54 +03:00
|
|
|
|
use rand::{seq::SliceRandom, Rng};
|
2021-08-24 17:15:24 +03:00
|
|
|
|
use std::{
|
|
|
|
|
cmp::Ordering,
|
2022-02-15 03:19:29 +03:00
|
|
|
|
ops::AddAssign,
|
2021-08-24 17:15:24 +03:00
|
|
|
|
pin::Pin,
|
|
|
|
|
task::{Context, Poll},
|
|
|
|
|
};
|
2021-05-15 03:45:16 +03:00
|
|
|
|
|
2023-01-28 02:37:36 +03:00
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
pub struct StaffMode(pub bool);
|
|
|
|
|
|
|
|
|
|
impl std::ops::Deref for StaffMode {
|
|
|
|
|
type Target = bool;
|
|
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
|
|
let backtrace = $crate::Backtrace::new();
|
|
|
|
|
log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
/// enforcing a maximum length. Sort the items according to the given callback. Before calling this,
|
|
|
|
|
/// 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
|
|
|
|
|
2021-08-24 17:15:24 +03:00
|
|
|
|
pub trait ResultExt {
|
|
|
|
|
type Ok;
|
|
|
|
|
|
|
|
|
|
fn log_err(self) -> Option<Self::Ok>;
|
2021-10-05 01:47:18 +03:00
|
|
|
|
fn warn_on_err(self) -> Option<Self::Ok>;
|
2021-08-24 17:15:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 18:12:59 +03:00
|
|
|
|
impl<T, E> ResultExt for Result<T, E>
|
|
|
|
|
where
|
|
|
|
|
E: std::fmt::Debug,
|
|
|
|
|
{
|
2021-08-24 17:15:24 +03:00
|
|
|
|
type Ok = T;
|
|
|
|
|
|
|
|
|
|
fn log_err(self) -> Option<T> {
|
|
|
|
|
match self {
|
|
|
|
|
Ok(value) => Some(value),
|
|
|
|
|
Err(error) => {
|
|
|
|
|
log::error!("{:?}", error);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-05 01:47:18 +03:00
|
|
|
|
|
|
|
|
|
fn warn_on_err(self) -> Option<T> {
|
|
|
|
|
match self {
|
|
|
|
|
Ok(value) => Some(value),
|
|
|
|
|
Err(error) => {
|
|
|
|
|
log::warn!("{:?}", error);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-24 17:15:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait TryFutureExt {
|
|
|
|
|
fn log_err(self) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
2021-10-05 01:47:18 +03:00
|
|
|
|
fn warn_on_err(self) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized;
|
2021-08-24 17:15:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<F, T> TryFutureExt for F
|
2021-08-19 04:06:46 +03:00
|
|
|
|
where
|
2021-08-24 17:15:24 +03:00
|
|
|
|
F: Future<Output = anyhow::Result<T>>,
|
2021-08-19 04:06:46 +03:00
|
|
|
|
{
|
2021-08-24 17:15:24 +03:00
|
|
|
|
fn log_err(self) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
2021-10-05 01:47:18 +03:00
|
|
|
|
LogErrorFuture(self, log::Level::Error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn warn_on_err(self) -> LogErrorFuture<Self>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
|
|
|
|
LogErrorFuture(self, log::Level::Warn)
|
2021-08-24 17:15:24 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 01:47:18 +03:00
|
|
|
|
pub struct LogErrorFuture<F>(F, log::Level);
|
2021-08-24 17:15:24 +03:00
|
|
|
|
|
|
|
|
|
impl<F, T> Future for LogErrorFuture<F>
|
|
|
|
|
where
|
|
|
|
|
F: Future<Output = anyhow::Result<T>>,
|
|
|
|
|
{
|
|
|
|
|
type Output = Option<T>;
|
|
|
|
|
|
|
|
|
|
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2021-10-05 01:47:18 +03:00
|
|
|
|
let level = self.1;
|
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) => {
|
|
|
|
|
log::log!(level, "{:?}", error);
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}),
|
2021-08-24 17:15:24 +03:00
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
|
}
|
2021-08-19 04:06:46 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 00:47:06 +03:00
|
|
|
|
struct Defer<F: FnOnce()>(Option<F>);
|
|
|
|
|
|
|
|
|
|
impl<F: FnOnce()> Drop for Defer<F> {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn defer<F: FnOnce()>(f: F) -> impl Drop {
|
|
|
|
|
Defer(Some(f))
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 01:25:54 +03:00
|
|
|
|
pub struct RandomCharIter<T: Rng>(T);
|
|
|
|
|
|
|
|
|
|
impl<T: Rng> RandomCharIter<T> {
|
|
|
|
|
pub fn new(rng: T) -> Self {
|
|
|
|
|
Self(rng)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Rng> Iterator for RandomCharIter<T> {
|
|
|
|
|
type Item = char;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
if std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()) {
|
|
|
|
|
return if self.0.gen_range(0..100) < 5 {
|
|
|
|
|
Some('\n')
|
|
|
|
|
} else {
|
|
|
|
|
Some(self.0.gen_range(b'a'..b'z' + 1).into())
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match self.0.gen_range(0..100) {
|
|
|
|
|
// whitespace
|
|
|
|
|
0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.0).copied(),
|
|
|
|
|
// two-byte greek letters
|
|
|
|
|
20..=32 => char::from_u32(self.0.gen_range(('α' as u32)..('ω' as u32 + 1))),
|
|
|
|
|
// // three-byte characters
|
|
|
|
|
33..=45 => ['✋', '✅', '❌', '❎', '⭐'].choose(&mut self.0).copied(),
|
|
|
|
|
// // four-byte characters
|
|
|
|
|
46..=58 => ['🍐', '🏀', '🍗', '🎉'].choose(&mut self.0).copied(),
|
|
|
|
|
// ascii letters
|
|
|
|
|
_ => Some(self.0.gen_range(b'a'..b'z' + 1).into()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-04 23:22:35 +03:00
|
|
|
|
// copy unstable standard feature option unzip
|
|
|
|
|
// https://github.com/rust-lang/rust/issues/87800
|
|
|
|
|
// Remove when this ship in Rust 1.66 or 1.67
|
|
|
|
|
pub fn unzip_option<T, U>(option: Option<(T, U)>) -> (Option<T>, Option<U>) {
|
|
|
|
|
match option {
|
|
|
|
|
Some((a, b)) => (Some(a), Some(b)),
|
|
|
|
|
None => (None, None),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 03:39:32 +03:00
|
|
|
|
/// Immediately invoked function expression. Good for using the ? operator
|
|
|
|
|
/// in functions which do not return an Option or Result
|
2022-11-02 23:26:23 +03:00
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! iife {
|
|
|
|
|
($block:block) => {
|
|
|
|
|
(|| $block)()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-08 03:39:32 +03:00
|
|
|
|
/// Async lImmediately invoked function expression. Good for using the ? operator
|
|
|
|
|
/// in functions which do not return an Option or Result. Async version of above
|
2022-12-02 01:16:38 +03:00
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! async_iife {
|
|
|
|
|
($block:block) => {
|
|
|
|
|
(|| async move { $block })()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let foo = iife!({
|
|
|
|
|
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
|
|
|
|
}
|
2021-03-18 22:13:31 +03:00
|
|
|
|
}
|