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