GeneIT/geneit_backend/src/constants.rs

137 lines
4.1 KiB
Rust
Raw Normal View History

use std::time::Duration;
2023-08-04 17:03:46 +00:00
#[derive(Debug, Copy, Clone, serde::Serialize, serde::Deserialize)]
2023-05-24 12:38:18 +00:00
pub struct SizeConstraint {
min: usize,
max: usize,
}
impl SizeConstraint {
pub fn new(min: usize, max: usize) -> Self {
Self { min, max }
}
2023-05-24 14:19:46 +00:00
pub fn validate(&self, val: &str) -> bool {
let len = val.trim().len();
len >= self.min && len <= self.max
}
2023-05-24 12:38:18 +00:00
}
2023-08-04 17:03:46 +00:00
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct NumberValueConstraint {
min: i64,
max: i64,
}
impl NumberValueConstraint {
pub fn new(min: i64, max: i64) -> Self {
Self { min, max }
}
pub fn validate(&self, val: impl Into<i64>) -> bool {
let val = val.into();
val >= self.min && val <= self.max
}
}
2023-05-24 12:38:18 +00:00
#[derive(Debug, Clone, serde::Serialize)]
pub struct StaticConstraints {
2023-08-05 17:15:52 +00:00
pub date_year: NumberValueConstraint,
pub date_month: NumberValueConstraint,
pub date_day: NumberValueConstraint,
pub photo_allowed_types: &'static [&'static str],
pub photo_max_size: usize,
2023-05-24 12:38:18 +00:00
pub mail_len: SizeConstraint,
pub user_name_len: SizeConstraint,
pub password_len: SizeConstraint,
2023-06-16 15:51:51 +00:00
pub family_name_len: SizeConstraint,
2023-06-27 16:52:49 +00:00
pub invitation_code_len: SizeConstraint,
2023-08-04 17:03:46 +00:00
pub member_first_name: SizeConstraint,
pub member_last_name: SizeConstraint,
pub member_birth_last_name: SizeConstraint,
pub member_email: SizeConstraint,
pub member_phone: SizeConstraint,
pub member_address: SizeConstraint,
pub member_city: SizeConstraint,
pub member_postal_code: SizeConstraint,
pub member_country: SizeConstraint,
pub member_sex: SizeConstraint,
pub member_note: SizeConstraint,
2023-05-24 12:38:18 +00:00
}
impl Default for StaticConstraints {
fn default() -> Self {
Self {
2023-08-05 17:15:52 +00:00
date_year: NumberValueConstraint::new(1, 2050),
date_month: NumberValueConstraint::new(1, 12),
date_day: NumberValueConstraint::new(1, 31),
photo_allowed_types: &ALLOWED_PHOTOS_MIMETYPES,
photo_max_size: PHOTOS_MAX_SIZE,
2023-05-24 12:38:18 +00:00
mail_len: SizeConstraint::new(5, 255),
user_name_len: SizeConstraint::new(3, 30),
password_len: SizeConstraint::new(8, 255),
2023-06-16 15:51:51 +00:00
family_name_len: SizeConstraint::new(3, 30),
2023-06-27 16:52:49 +00:00
invitation_code_len: SizeConstraint::new(
FAMILY_INVITATION_CODE_LEN,
FAMILY_INVITATION_CODE_LEN,
),
2023-08-04 17:03:46 +00:00
member_first_name: SizeConstraint::new(0, 30),
member_last_name: SizeConstraint::new(0, 30),
member_birth_last_name: SizeConstraint::new(0, 30),
member_email: SizeConstraint::new(0, 255),
member_phone: SizeConstraint::new(0, 30),
member_address: SizeConstraint::new(0, 155),
member_city: SizeConstraint::new(0, 150),
member_postal_code: SizeConstraint::new(0, 12),
member_country: SizeConstraint::new(0, 2),
member_sex: SizeConstraint::new(0, 1),
member_note: SizeConstraint::new(0, 35000),
2023-05-24 12:38:18 +00:00
}
}
}
/// Password reset token duration
pub const PASSWORD_RESET_TOKEN_DURATION: Duration = Duration::from_secs(3600 * 24);
2023-06-02 09:49:18 +00:00
2023-06-06 08:02:41 +00:00
/// Account deletion token duration
pub const ACCOUNT_DELETE_TOKEN_DURATION: Duration = Duration::from_secs(3600 * 12);
2023-06-02 09:49:18 +00:00
/// OpenID state duration
pub const OPEN_ID_STATE_DURATION: Duration = Duration::from_secs(3600);
2023-06-16 15:51:51 +00:00
2023-06-16 16:40:21 +00:00
/// Auth token duration
pub const AUTH_TOKEN_DURATION: Duration = Duration::from_secs(3600 * 24);
/// Minimum interval before heartbeat update
pub const AUTH_TOKEN_HB_MIN_INTERVAL: Duration = Duration::from_secs(60);
/// Auth token max inactivity period
pub const AUTH_TOKEN_MAX_INACTIVITY: Duration = Duration::from_secs(3600);
2023-06-16 15:51:51 +00:00
/// Length of family invitation code
pub const FAMILY_INVITATION_CODE_LEN: usize = 7;
2023-08-05 17:15:52 +00:00
/// Allowed photos mimetypes
pub const ALLOWED_PHOTOS_MIMETYPES: [&str; 6] = [
"image/jpeg",
"image/png",
"image/gif",
"image/bmp",
"image/webp",
"image/vnd.microsoft.icon",
];
/// Uploaded photos max size
pub const PHOTOS_MAX_SIZE: usize = 10 * 1000 * 1000;
2023-08-07 09:07:24 +00:00
/// Thumbnail width
pub const THUMB_WIDTH: u32 = 150;
/// Thumbnail height
pub const THUMB_HEIGHT: u32 = 150;