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