lemmy_utils/
lib.rs

1use cfg_if::cfg_if;
2
3cfg_if! {
4  if #[cfg(feature = "full")] {
5    pub mod cache_header;
6    pub mod email;
7    pub mod rate_limit;
8    pub mod request;
9    pub mod response;
10    pub mod settings;
11    pub mod utils;
12  }
13}
14
15pub mod error;
16pub use error::LemmyErrorType;
17use std::time::Duration;
18
19pub type ConnectionId = usize;
20
21/// git_version marks this crate as dirty and causes a rebuild if any file in the repo is changed.
22/// This slows down development a lot, so we only use git_version for release builds.
23#[cfg(not(debug_assertions))]
24pub const VERSION: &str = git_version::git_version!(
25  args = ["--tags", "--dirty=-modified"],
26  fallback = env!("CARGO_PKG_VERSION")
27);
28#[cfg(debug_assertions)]
29pub const VERSION: &str = env!("CARGO_PKG_VERSION");
30
31pub const REQWEST_TIMEOUT: Duration = Duration::from_secs(10);
32
33#[cfg(debug_assertions)]
34pub const CACHE_DURATION_FEDERATION: Duration = Duration::from_millis(500);
35#[cfg(not(debug_assertions))]
36pub const CACHE_DURATION_FEDERATION: Duration = Duration::from_secs(60);
37
38pub const CACHE_DURATION_API: Duration = Duration::from_secs(1);
39
40pub const MAX_COMMENT_DEPTH_LIMIT: usize = 50;
41
42#[macro_export]
43macro_rules! location_info {
44  () => {
45    format!(
46      "None value at {}:{}, column {}",
47      file!(),
48      line!(),
49      column!()
50    )
51  };
52}
53
54#[cfg(feature = "full")]
55/// tokio::spawn, but accepts a future that may fail and also
56/// * logs errors
57/// * attaches the spawned task to the tracing span of the caller for better logging
58pub fn spawn_try_task(
59  task: impl futures::Future<Output = Result<(), error::LemmyError>> + Send + 'static,
60) {
61  use tracing::Instrument;
62  tokio::spawn(
63    async {
64      if let Err(e) = task.await {
65        tracing::warn!("error in spawn: {e}");
66      }
67    }
68    .in_current_span(), /* this makes sure the inner tracing gets the same context as where
69                         * spawn was called */
70  );
71}