lemmy/crates/utils/src/lib.rs

96 lines
1.8 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate strum_macros;
pub mod apub;
pub mod claims;
pub mod email;
pub mod rate_limit;
pub mod request;
pub mod settings;
#[cfg(test)]
mod test;
pub mod utils;
pub mod version;
use crate::settings::structs::Settings;
use http::StatusCode;
use regex::Regex;
use thiserror::Error;
pub type ConnectionId = usize;
pub type PostId = i32;
pub type CommunityId = i32;
pub type UserId = i32;
2021-02-22 18:04:32 +00:00
pub type IpAddr = String;
#[macro_export]
macro_rules! location_info {
() => {
format!(
"None value at {}:{}, column {}",
file!(),
line!(),
column!()
)
};
}
#[derive(Debug, Error)]
#[error("{{\"error\":\"{message}\"}}")]
2021-02-22 18:04:32 +00:00
pub struct ApiError {
pub message: String,
}
2021-02-22 18:04:32 +00:00
impl ApiError {
pub fn err(msg: &str) -> Self {
2021-02-22 18:04:32 +00:00
ApiError {
message: msg.to_string(),
}
}
}
#[derive(Debug)]
pub struct LemmyError {
pub inner: anyhow::Error,
}
impl<T> From<T> for LemmyError
2020-09-02 23:17:35 +00:00
where
T: Into<anyhow::Error>,
{
fn from(t: T) -> Self {
LemmyError { inner: t.into() }
}
}
impl std::fmt::Display for LemmyError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl actix_web::error::ResponseError for LemmyError {
fn status_code(&self) -> StatusCode {
match self.inner.downcast_ref::<diesel::result::Error>() {
Some(diesel::result::Error::NotFound) => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
lazy_static! {
pub static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!(
"^group:([a-z0-9_]{{3, 20}})@{}$",
Settings::get().hostname()
))
2021-03-01 12:56:07 +00:00
.expect("compile webfinger regex");
pub static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!(
"^acct:([a-z0-9_]{{3, 20}})@{}$",
Settings::get().hostname()
))
2021-03-01 12:56:07 +00:00
.expect("compile webfinger regex");
}