AppFlowy/frontend/rust-lib/lib-infra/src/util.rs
Nathan.fooo 90516b6adc
feat: wasm build (#4412)
* chore: enable wasm build

* chore: bump collab

* chore: fix build

* chore: flowy-document wasm

* chore: fix compile

* chore: fix compile

* chore: fix compile

* chore: fix compile

* chore: fix ci

* chore: fix ci

* chore: fix ci
2024-01-22 13:34:15 +08:00

55 lines
1.0 KiB
Rust

#[macro_export]
macro_rules! if_native {
($($item:item)*) => {$(
#[cfg(not(target_arch = "wasm32"))]
$item
)*}
}
#[macro_export]
macro_rules! if_wasm {
($($item:item)*) => {$(
#[cfg(target_arch = "wasm32")]
$item
)*}
}
pub fn move_vec_element<T, F>(
vec: &mut Vec<T>,
filter: F,
_from_index: usize,
to_index: usize,
) -> Result<bool, String>
where
F: FnMut(&T) -> bool,
{
match vec.iter().position(filter) {
None => Ok(false),
Some(index) => {
if vec.len() > to_index {
let removed_element = vec.remove(index);
vec.insert(to_index, removed_element);
Ok(true)
} else {
let msg = format!(
"Move element to invalid index: {}, current len: {}",
to_index,
vec.len()
);
Err(msg)
}
},
}
}
#[allow(dead_code)]
pub fn timestamp() -> i64 {
chrono::Utc::now().timestamp()
}
#[inline]
pub fn md5<T: AsRef<[u8]>>(data: T) -> String {
let md5 = format!("{:x}", md5::compute(data));
md5
}