util.rs 773 B

123456789101112131415161718192021222324252627
  1. pub fn move_vec_element<T, F>(vec: &mut Vec<T>, filter: F, _from_index: usize, to_index: usize) -> Result<bool, String>
  2. where
  3. F: FnMut(&T) -> bool,
  4. {
  5. match vec.iter().position(filter) {
  6. None => Ok(false),
  7. Some(index) => {
  8. if vec.len() > to_index {
  9. let removed_element = vec.remove(index);
  10. vec.insert(to_index, removed_element);
  11. Ok(true)
  12. } else {
  13. let msg = format!(
  14. "Move element to invalid index: {}, current len: {}",
  15. to_index,
  16. vec.len()
  17. );
  18. Err(msg)
  19. }
  20. }
  21. }
  22. }
  23. #[allow(dead_code)]
  24. pub fn timestamp() -> i64 {
  25. chrono::Utc::now().timestamp()
  26. }