Skip to main content

nwnrs_localization/
types.rs

1use std::{error::Error, fmt, str::FromStr};
2
3/// A TLK string reference.
4pub type StrRef = u32;
5/// The sentinel string reference used for “no string”.
6pub const BAD_STRREF: StrRef = u32::MAX;
7
8/// A supported NWN language identifier.
9#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Language {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                Language::English => "English",
                Language::French => "French",
                Language::German => "German",
                Language::Italian => "Italian",
                Language::Spanish => "Spanish",
                Language::Polish => "Polish",
            })
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for Language {
    #[inline]
    fn clone(&self) -> Language { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Language { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for Language {
    #[inline]
    fn eq(&self, other: &Language) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Language {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for Language {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state)
    }
}Hash)]
10#[repr(u32)]
11pub enum Language {
12    /// English resources.
13    English = 0,
14    /// French resources.
15    French = 1,
16    /// German resources.
17    German = 2,
18    /// Italian resources.
19    Italian = 3,
20    /// Spanish resources.
21    Spanish = 4,
22    /// Polish resources.
23    Polish = 5,
24}
25
26/// A gender selector for TLK lookups.
27#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Gender {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                Gender::Male => "Male",
                Gender::Female => "Female",
            })
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for Gender {
    #[inline]
    fn clone(&self) -> Gender { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Gender { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for Gender {
    #[inline]
    fn eq(&self, other: &Gender) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Gender {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for Gender {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state)
    }
}Hash)]
28pub enum Gender {
29    /// Male dialogue.
30    Male,
31    /// Female dialogue.
32    Female,
33}
34
35/// An error returned when a language identifier cannot be parsed.
36#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ParseLanguageError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "ParseLanguageError", "input", &self.input, "reason",
            &&self.reason)
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for ParseLanguageError {
    #[inline]
    fn clone(&self) -> ParseLanguageError {
        ParseLanguageError {
            input: ::core::clone::Clone::clone(&self.input),
            reason: ::core::clone::Clone::clone(&self.reason),
        }
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for ParseLanguageError {
    #[inline]
    fn eq(&self, other: &ParseLanguageError) -> bool {
        self.input == other.input && self.reason == other.reason
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ParseLanguageError {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<String>;
    }
}Eq)]
37pub struct ParseLanguageError {
38    pub(crate) input:  String,
39    pub(crate) reason: String,
40}
41
42impl ParseLanguageError {
43    pub(crate) fn new(input: &str, reason: impl Into<String>) -> Self {
44        Self {
45            input:  input.to_string(),
46            reason: reason.into(),
47        }
48    }
49}
50
51impl fmt::Display for ParseLanguageError {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        f.write_fmt(format_args!("{0}: Not a valid language ({1})", self.input,
        self.reason))write!(f, "{}: Not a valid language ({})", self.input, self.reason)
54    }
55}
56
57impl Error for ParseLanguageError {}
58
59impl Language {
60    /// Returns the numeric NWN language id.
61    #[must_use]
62    pub fn id(self) -> u32 {
63        self as u32
64    }
65
66    /// Returns the two-letter NWN language code.
67    #[must_use]
68    pub fn short_code(self) -> &'static str {
69        match self {
70            Self::English => "en",
71            Self::French => "fr",
72            Self::German => "de",
73            Self::Italian => "it",
74            Self::Spanish => "es",
75            Self::Polish => "pl",
76        }
77    }
78
79    /// Resolves a language from its numeric NWN id.
80    #[must_use]
81    pub fn from_id(id: u32) -> Option<Self> {
82        match id {
83            0 => Some(Self::English),
84            1 => Some(Self::French),
85            2 => Some(Self::German),
86            3 => Some(Self::Italian),
87            4 => Some(Self::Spanish),
88            5 => Some(Self::Polish),
89            _ => None,
90        }
91    }
92}
93
94impl FromStr for Language {
95    type Err = ParseLanguageError;
96
97    fn from_str(input: &str) -> Result<Self, Self::Err> {
98        crate::resolve_language(input)
99    }
100}