Compare commits

..

No commits in common. "63dd63c2308e8119b7b173033d2a1c87b08fb154" and "51d5ee9a2e64dcdb3d100dc25680776c3ac3f0f2" have entirely different histories.

4 changed files with 6 additions and 59 deletions

View file

@ -10,7 +10,7 @@ contents := version-0 | ...
If user-preferences is not present (i.e. is zero-length), then the default preferences
should be assumed. Default preferences will be set by the instance operator
version-0 := variant-0-0
version-1 := variant-0-0
variant-0-0 := default-enabled default-weight *command
default-enabled := 1 bit

View file

@ -46,7 +46,6 @@ use std::fmt;
use serde::{Serialize, Deserialize, self};
use user_preferences::{ParseError, Preference};
pub use weighted_table::WeightedTable;
pub use user_preferences::UserPreferences;
@ -74,21 +73,15 @@ impl InstanceSettings {
/// ```
/// # use pronouns_today::InstanceSettings;
/// # let settings = InstanceSettings::default();
/// # let name = String::from("Sashanoraa");
/// # let name = String::from("Sashanora");
/// # let prefstring = String::from("todo");
/// let pronouns = InstanceSettings::parse_prefstring(Some(&prefstring))?.select_pronouns(&settings, Some(&name));
/// let pronouns = settings.parse_prefstring(Some(&prefstring)).select_pronouns(Some(&name));
/// # assert_eq!(pronouns, settings.select_pronouns(Some(&name), Some(&prefstring)));
/// ```
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 select_pronouns(&self, name: Option<impl AsRef<str>>, pref_string: Option<impl AsRef<str>>) -> &str {
todo!()
}
pub fn parse_prefstring(pref_string: Option<&str>) -> Result<UserPreferences, ParseError> {
match pref_string {
Some(pref_string) => UserPreferences::from_prefstring(pref_string),
None => Ok(UserPreferences::default())
}
}
}
impl Default for InstanceSettings {

View file

@ -29,12 +29,6 @@ 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`]
@ -82,14 +76,6 @@ 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 {

View file

@ -119,27 +119,7 @@ impl Preference for UserPreferencesV0 {
}
fn into_prefstring_bytes(&self) -> Vec<u8> {
let mut defaults_byte = 0u8;
if self.default_enabled {
defaults_byte |= 0b10000000;
}
defaults_byte |= self.default_weight & 0b01111111;
vec![defaults_byte]
.into_iter()
.chain(self.commands.iter().map(|cmd| cmd.into()))
.collect()
}
}
/// Default to all pronouns on with equal weight
/// TODO make this configurable
impl Default for UserPreferencesV0 {
fn default() -> Self {
Self {
default_enabled: true,
default_weight: 1,
commands: vec![],
}
todo!()
}
}
@ -186,18 +166,6 @@ impl From<u8> for Command {
}
}
impl From<&Command> for u8 {
fn from(command: &Command) -> Self {
match command {
// Some these ands shouldn't be needed if the command is valid
Command::SetWeight(weight) => weight & 0b01111111,
Command::Move { toggle_enabled, distance } => {
0b10000000 | ((*toggle_enabled as u8) << 6) | (distance & 0b00111111)
}
}
}
}
#[cfg(test)]
mod tests {