From 63dd63c2308e8119b7b173033d2a1c87b08fb154 Mon Sep 17 00:00:00 2001 From: Ben Aaron Goldberg Date: Sat, 23 Oct 2021 20:33:12 -0400 Subject: [PATCH] Impl select_pronouns and add parse_prefstring to InstanceSettings Signed-off-by: Ben Aaron Goldberg --- src/lib.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5413552..251f4d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,6 +46,7 @@ use std::fmt; use serde::{Serialize, Deserialize, self}; +use user_preferences::{ParseError, Preference}; pub use weighted_table::WeightedTable; pub use user_preferences::UserPreferences; @@ -73,15 +74,21 @@ impl InstanceSettings { /// ``` /// # use pronouns_today::InstanceSettings; /// # let settings = InstanceSettings::default(); - /// # let name = String::from("Sashanora"); + /// # let name = String::from("Sashanoraa"); /// # let prefstring = String::from("todo"); - /// let pronouns = settings.parse_prefstring(Some(&prefstring)).select_pronouns(Some(&name)); + /// let pronouns = InstanceSettings::parse_prefstring(Some(&prefstring))?.select_pronouns(&settings, Some(&name)); /// # assert_eq!(pronouns, settings.select_pronouns(Some(&name), Some(&prefstring))); /// ``` - pub fn select_pronouns(&self, name: Option>, pref_string: Option>) -> &str { - todo!() + pub fn select_pronouns(&self, name: Option<&str>, pref_string: Option<&str>) -> Result<&Pronoun, ParseError> { + Self::parse_prefstring(pref_string)?.select_pronoun(&self, name) } + pub fn parse_prefstring(pref_string: Option<&str>) -> Result { + match pref_string { + Some(pref_string) => UserPreferences::from_prefstring(pref_string), + None => Ok(UserPreferences::default()) + } + } } impl Default for InstanceSettings {