]> Untitled Git - lemmy.git/blob - crates/utils/src/lib.rs
Merge pull request #1328 from LemmyNet/move_views_to_diesel
[lemmy.git] / crates / utils / src / lib.rs
1 #[macro_use]
2 extern crate lazy_static;
3 #[macro_use]
4 extern crate strum_macros;
5
6 pub mod apub;
7 pub mod email;
8 pub mod rate_limit;
9 pub mod request;
10 pub mod settings;
11 #[cfg(test)]
12 mod test;
13 pub mod utils;
14
15 use crate::settings::Settings;
16 use regex::Regex;
17 use thiserror::Error;
18
19 pub type ConnectionId = usize;
20 pub type PostId = i32;
21 pub type CommunityId = i32;
22 pub type UserId = i32;
23 pub type IPAddr = String;
24
25 #[macro_export]
26 macro_rules! location_info {
27   () => {
28     format!(
29       "None value at {}:{}, column {}",
30       file!(),
31       line!(),
32       column!()
33     )
34   };
35 }
36
37 #[derive(Debug, Error)]
38 #[error("{{\"error\":\"{message}\"}}")]
39 pub struct APIError {
40   pub message: String,
41 }
42
43 impl APIError {
44   pub fn err(msg: &str) -> Self {
45     APIError {
46       message: msg.to_string(),
47     }
48   }
49 }
50
51 #[derive(Debug)]
52 pub struct LemmyError {
53   pub inner: anyhow::Error,
54 }
55
56 impl<T> From<T> for LemmyError
57 where
58   T: Into<anyhow::Error>,
59 {
60   fn from(t: T) -> Self {
61     LemmyError { inner: t.into() }
62   }
63 }
64
65 impl std::fmt::Display for LemmyError {
66   fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
67     self.inner.fmt(f)
68   }
69 }
70
71 impl actix_web::error::ResponseError for LemmyError {}
72
73 lazy_static! {
74   pub static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!(
75     "^group:([a-z0-9_]{{3, 20}})@{}$",
76     Settings::get().hostname
77   ))
78   .unwrap();
79   pub static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!(
80     "^acct:([a-z0-9_]{{3, 20}})@{}$",
81     Settings::get().hostname
82   ))
83   .unwrap();
84 }