selene-core 0.9.0-rc.1

Backend for selene-server
Documentation
pub mod account;

pub mod password;

use selene_common::api::{ApiError, ApiErrorKind};
use serde::{Deserialize, Serialize};

pub static NAME_ID_MAP: &str = "name_id_map";
pub static TOKEN_MAP: &str = "token_map";

impl From<AccountCreationError> for ApiError {
    fn from(value: AccountCreationError) -> ApiError {
        let kind = match value {
            AccountCreationError::InvalidLength(_) => ApiErrorKind::InvalidLength {
                field: "username".to_owned(),
                min: 1,
                max: 32,
            },
            AccountCreationError::DuplicateName(value) => ApiErrorKind::Duplicate {
                field: "username".to_owned(),
                value,
            },
            AccountCreationError::Password(err) => return ApiError::from(err),
        };
        ApiError::new(kind)
    }
}

impl From<PasswordCreationError> for ApiError {
    fn from(value: PasswordCreationError) -> ApiError {
        let kind = match value {
            PasswordCreationError::InvalidLength(_) => ApiErrorKind::InvalidLength {
                field: "password".to_owned(),
                min: 1,
                max: 32,
            },
        };
        ApiError::new(kind)
    }
}

#[derive(Debug, thiserror::Error, Serialize, Deserialize)]
pub enum AccountCreationError {
    #[error("Name must be between =1 and =32 characters long. Input len: {0}")]
    InvalidLength(u32),

    #[error("The name '{0}' is already in use")]
    DuplicateName(String),

    #[error("Invalid password: {0}")]
    Password(#[from] PasswordCreationError),
}

#[derive(Debug, thiserror::Error, Serialize, Deserialize)]
pub enum PasswordCreationError {
    #[error("Passwords must be between =15 and =128 characters long. Input len: {0}")]
    InvalidLength(u32),
}