Compare commits
4 commits
51d5ee9a2e
...
63dd63c230
Author | SHA1 | Date | |
---|---|---|---|
63dd63c230 | |||
71e979e023 | |||
a927e9010b | |||
2233cf5a15 |
|
@ -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-1 := variant-0-0
|
||||
version-0 := variant-0-0
|
||||
|
||||
variant-0-0 := default-enabled default-weight *command
|
||||
default-enabled := 1 bit
|
||||
|
|
15
src/lib.rs
15
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<impl AsRef<str>>, pref_string: Option<impl AsRef<str>>) -> &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<UserPreferences, ParseError> {
|
||||
match pref_string {
|
||||
Some(pref_string) => UserPreferences::from_prefstring(pref_string),
|
||||
None => Ok(UserPreferences::default())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for InstanceSettings {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -119,7 +119,27 @@ impl Preference for UserPreferencesV0 {
|
|||
}
|
||||
|
||||
fn into_prefstring_bytes(&self) -> Vec<u8> {
|
||||
todo!()
|
||||
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![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -166,6 +186,18 @@ 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 {
|
||||
|
|
Loading…
Reference in a new issue