Associate the lifetime of the settings with the table, not the user prefs

This commit is contained in:
Emi Simpson 2021-10-21 15:05:52 -04:00
parent eaad02b4e7
commit a97b97182e
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
2 changed files with 15 additions and 19 deletions

View file

@ -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<Self, DecodeError> where Self: Sized {
fn from_prefstring(prefstring: &str) -> Result<Self, DecodeError> 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!()
}

View file

@ -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!()
}