From a97b97182e7e26a3c19cc247fd8a70e27d339c1a Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Thu, 21 Oct 2021 15:05:52 -0400 Subject: [PATCH] Associate the lifetime of the settings with the table, not the user prefs --- src/user_preferences/mod.rs | 22 +++++++++++----------- src/user_preferences/v0.rs | 12 ++++-------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/user_preferences/mod.rs b/src/user_preferences/mod.rs index 6571159..ec8bfd4 100644 --- a/src/user_preferences/mod.rs +++ b/src/user_preferences/mod.rs @@ -22,14 +22,14 @@ use data_encoding::{BASE32_NOPAD, DecodeError}; /// Because parsing a prefstring must be done in relation to an [`InstanceSettings`], the /// `UserPreferences` struct shares a lifetime with the settings it was created with. #[non_exhaustive] -pub enum UserPreferences<'a> { - V0(v0::UserPreferencesV0<'a>) +pub enum UserPreferences { + V0(v0::UserPreferencesV0) } /// Functionality provided by any version of user preferences /// /// See also: [`UserPreferences`] -pub trait Preference<'a> { +pub trait Preference { /// Produce a weighted list of pronouns based on these preferences /// @@ -37,7 +37,7 @@ pub trait Preference<'a> { /// crucial step to randomly selecting a pronoun set based on a user's preferences, as /// any selection is done by using a [`WeightedTable`]. All preference versions must /// implement this method. - fn into_weighted_table(&self) -> WeightedTable; + fn into_weighted_table<'a>(&self, settings: &'a InstanceSettings) -> WeightedTable<'a>; /// Parse a given prefstring, after it's extraction from base64 /// @@ -45,7 +45,7 @@ pub trait Preference<'a> { /// format. Prefstrings will already have been parsed from base64 before being passed to the /// implementation. Users looking to turn a b64 prefstring into a `Preference` should use /// [`Preference::from_prefstring()`] - fn from_prefstring_bytes(bytes: &[u8], settings: &'a InstanceSettings) -> Self where Self: Sized; + fn from_prefstring_bytes(bytes: &[u8]) -> Self; /// Serialize these preferences into as few bytes as possible /// @@ -60,9 +60,9 @@ pub trait Preference<'a> { /// /// This is the primary method of creating a `Preference` object from a prefstring. The /// default implementation calls the underlying [`Preference::from_prefstring_bytes()`] method. - fn from_prefstring(prefstring: &str, settings: &'a InstanceSettings) -> Result where Self: Sized { + fn from_prefstring(prefstring: &str) -> Result where Self: Sized { BASE32_NOPAD.decode(prefstring.as_ref()) - .map(|ps| Self::from_prefstring_bytes(&ps, settings)) + .map(|ps| Self::from_prefstring_bytes(&ps)) } /// Serialize into a base64 prefstring @@ -74,14 +74,14 @@ pub trait Preference<'a> { } } -impl<'a> Preference<'a> for UserPreferences<'a> { - fn into_weighted_table(&self) -> WeightedTable { +impl Preference for UserPreferences { + fn into_weighted_table<'a>(&self, settings: &'a InstanceSettings) -> WeightedTable<'a> { match self { UserPreferences::V0(pref) => pref, - }.into_weighted_table() + }.into_weighted_table(settings) } - fn from_prefstring_bytes(bytes: &[u8], settings: &'a InstanceSettings) -> Self { + fn from_prefstring_bytes(bytes: &[u8]) -> Self { todo!() } diff --git a/src/user_preferences/v0.rs b/src/user_preferences/v0.rs index 1025164..669f0d8 100644 --- a/src/user_preferences/v0.rs +++ b/src/user_preferences/v0.rs @@ -2,30 +2,26 @@ use crate::{ InstanceSettings, - Pronoun, user_preferences::Preference, WeightedTable, }; -use std::collections::HashMap; - /// A parsed version of the V0 prefstring /// /// See the [prefstring specification][1] for more information about how this is interpretted. /// /// [1]: https://fem.mint.lgbt/Emi/PronounsToday/raw/branch/main/doc/User-Preference-String-Spec.txt -pub struct UserPreferencesV0<'a> { +pub struct UserPreferencesV0 { default_weight: u8, default_enabled: bool, - overrides: HashMap<&'a Pronoun, u8>, } -impl<'a> Preference<'a> for UserPreferencesV0<'a> { - fn into_weighted_table(&self) -> WeightedTable { +impl Preference for UserPreferencesV0 { + fn into_weighted_table<'a>(&self, settings: &'a InstanceSettings) -> WeightedTable<'a> { todo!() } - fn from_prefstring_bytes(bytes: &[u8], settings: &'a InstanceSettings) -> Self { + fn from_prefstring_bytes(bytes: &[u8]) -> Self { todo!() }