Impl into_prefstring_bytes for UserPreferencesV0

Signed-off-by: Ben Aaron Goldberg <ben@benaaron.dev>
This commit is contained in:
Ben Aaron Goldberg 2021-10-23 20:30:48 -04:00
parent 2233cf5a15
commit a927e9010b
1 changed files with 33 additions and 1 deletions

View File

@ -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 {