From 71e979e023b7c4418cc579af7ed862937c64e5a5 Mon Sep 17 00:00:00 2001 From: Ben Aaron Goldberg Date: Sat, 23 Oct 2021 20:31:45 -0400 Subject: [PATCH] Add the select_pronoun method to UserPreferencesV0 & impl Default for UserPreferences Signed-off-by: Ben Aaron Goldberg --- src/user_preferences/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/user_preferences/mod.rs b/src/user_preferences/mod.rs index 08ef286..7ccb6ec 100644 --- a/src/user_preferences/mod.rs +++ b/src/user_preferences/mod.rs @@ -29,6 +29,12 @@ pub enum UserPreferences { V0(v0::UserPreferencesV0) } +impl Default for UserPreferences { + fn default() -> Self { + UserPreferences::V0(UserPreferencesV0::default()) + } +} + /// Functionality provided by any version of user preferences /// /// See also: [`UserPreferences`] @@ -76,6 +82,14 @@ pub trait Preference { fn into_prefstring(&self) -> String { BASE32_NOPAD.encode(&self.into_prefstring_bytes()) } + + fn select_pronoun<'a>(&self, settings: &'a InstanceSettings, name: Option<&str>) -> Result<&'a Pronoun, ParseError> { + let seed = match name { + Some(name) => name.as_bytes(), + None => &[], + }; + Ok(self.into_weighted_table(settings)?.select_today(seed)) + } } impl Preference for UserPreferences {