use crate::prelude::*; use sqlx::{mysql::MySqlQueryResult as QueryResult, query, query_as}; #[derive(Clone, Copy, Default)] #[allow(clippy::module_name_repetitions)] pub enum LocaleType { #[default] Eng = 1, Ru = 2, } impl TryFrom for LocaleType { type Error = (); fn try_from(value: u8) -> Result { match value { 1 => Ok(Self::Eng), 2 => Ok(Self::Ru), _ => Err(()), } } } impl From for u8 { fn from(value: LocaleType) -> Self { value as Self } } impl LocaleType { pub async fn get_from_db(user_id: u64, db: &Pool) -> super::Result> { let result: Option<(u8,)> = query_as("SELECT locale FROM master_pass WHERE user_id = ?") .bind(user_id) .fetch_optional(db) .await?; Ok(result.and_then(|val| val.0.try_into().ok())) } pub async fn update(self, user_id: u64, db: &Pool) -> super::Result { let result: QueryResult = query!( "UPDATE master_pass SET locale = ? WHERE user_id = ?", u8::from(self), user_id ) .execute(db) .await?; Ok(result.rows_affected() == 1) } }