1
0
Fork 0
forked from 2sys/phonograph
phonograph/phono-models/src/language.rs

60 lines
1.5 KiB
Rust
Raw Normal View History

2025-12-16 09:59:30 -08:00
use std::str::FromStr as _;
use serde::{Deserialize, Serialize};
use sqlx::{Decode, Postgres};
/// Languages represented as
/// [ISO 639-3 codes](https://en.wikipedia.org/wiki/List_of_ISO_639-3_codes).
2025-10-01 22:36:19 -07:00
#[derive(
2025-12-16 09:59:30 -08:00
Clone,
Debug,
Deserialize,
Eq,
Hash,
PartialEq,
Serialize,
strum::Display,
strum::EnumIs,
strum::EnumIter,
strum::EnumString,
2025-10-01 22:36:19 -07:00
)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
// NOTE: The [`sqlx::Encode`] and [`sqlx::Decode`] derive macros do not seem to
// use the [`strum`] serializations. The corresponding traits should be
// implemented explicitly (if used).
pub enum Language {
Deu,
Eng,
Spa,
}
impl Language {
/// Returns language name to be presented in UI.
pub fn as_locale_str(&self) -> &'static str {
match self {
Self::Deu => "Deutsch",
Self::Eng => "English",
Self::Spa => "Español",
}
}
}
impl Default for Language {
/// Language defaults to English when necessary, as the product is being
/// developed with a primarily English speaking/reading/writing market in
/// mind.
fn default() -> Self {
Self::Eng
}
}
impl Decode<'_, Postgres> for Language {
fn decode(
value: <Postgres as sqlx::Database>::ValueRef<'_>,
) -> Result<Self, sqlx::error::BoxDynError> {
let value = <&str as Decode<Postgres>>::decode(value)?;
Ok(Self::from_str(value)?)
}
}