use std::fmt;
use iced_core::theme;
use icedwright_types::Size as LogicalSize;
use icedwright_types::units::Logical;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Theme {
#[default]
Light,
Dark,
}
impl From<Theme> for theme::Mode {
fn from(theme: Theme) -> Self {
match theme {
Theme::Light => theme::Mode::Light,
Theme::Dark => theme::Mode::Dark,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Size {
Phone,
Tablet,
Desktop,
Custom(u16, u16),
}
impl Size {
#[must_use]
pub const fn dimensions(self) -> (u16, u16) {
match self {
Size::Phone => (360, 780),
Size::Tablet => (768, 1024),
Size::Desktop => (1280, 800),
Size::Custom(width, height) => (width, height),
}
}
}
impl From<Size> for Logical<LogicalSize> {
fn from(size: Size) -> Self {
let (width, height) = size.dimensions();
Logical(LogicalSize::new(f32::from(width), f32::from(height)))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Locale(&'static str);
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("{tag:?} is not a BCP 47 language tag, such as \"en-GB\"")]
pub struct InvalidLocale {
pub tag: &'static str,
}
impl Locale {
pub const EN_GB: Self = Self("en-GB");
pub const EN_US: Self = Self("en-US");
pub const DE: Self = Self("de");
pub const FR: Self = Self("fr");
pub const ES: Self = Self("es");
pub const AR: Self = Self("ar");
pub const HE: Self = Self("he");
pub const JA: Self = Self("ja");
pub const fn parse(tag: &'static str) -> Result<Self, InvalidLocale> {
let bytes = tag.as_bytes();
let mut index = 0;
let mut start = 0;
let mut first = true;
while index <= bytes.len() {
if index == bytes.len() || bytes[index] == b'-' {
let len = index - start;
let ok = if first {
matches!(len, 2 | 3 | 5..=8)
} else {
len >= 1 && len <= 8
};
if !ok {
return Err(InvalidLocale { tag });
}
first = false;
start = index + 1;
} else {
let byte = bytes[index];
let ok = if first {
byte.is_ascii_alphabetic()
} else {
byte.is_ascii_alphanumeric()
};
if !ok {
return Err(InvalidLocale { tag });
}
}
index += 1;
}
Ok(Self(tag))
}
#[must_use]
pub const fn tag(self) -> &'static str {
self.0
}
}
impl Default for Locale {
fn default() -> Self {
Self::EN_GB
}
}
impl fmt::Display for Locale {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
const PT_BR: Locale = match Locale::parse("pt-BR") {
Ok(locale) => locale,
Err(_) => panic!("pt-BR is a tag"),
};
#[test]
fn locales_parse_in_const_context() {
assert_eq!(PT_BR.tag(), "pt-BR");
assert_eq!(Locale::EN_GB.to_string(), "en-GB");
}
#[test]
fn a_malformed_locale_is_an_error_value() {
for bad in [
"",
"e",
"en_GB",
"en-",
"en--GB",
"1en",
"languages",
"en-toolongtag",
] {
assert_eq!(Locale::parse(bad), Err(InvalidLocale { tag: bad }), "{bad}");
}
for good in ["en", "zh-Hant-TW", "de-1996", "haw", Locale::AR.tag()] {
assert!(Locale::parse(good).is_ok(), "{good}");
}
}
#[test]
fn sizes_and_themes_map_to_the_harness_types() {
assert_eq!(
Logical::<LogicalSize>::from(Size::Phone),
Logical(LogicalSize::new(360.0, 780.0))
);
assert_eq!(Size::Custom(1024, 700).dimensions(), (1024, 700));
assert_eq!(theme::Mode::from(Theme::Dark), theme::Mode::Dark);
assert_eq!(theme::Mode::from(Theme::default()), theme::Mode::Light);
}
}