From 51d5ee9a2e64dcdb3d100dc25680776c3ac3f0f2 Mon Sep 17 00:00:00 2001 From: Ben Aaron Goldberg Date: Fri, 22 Oct 2021 00:35:06 -0400 Subject: [PATCH] user_preferences: Completed impl of Preference for UserPreferences from_prefstring_bytes and into_prefstring_bytes are now implemented. Signed-off-by: Ben Aaron Goldberg --- src/user_preferences/mod.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/user_preferences/mod.rs b/src/user_preferences/mod.rs index e176ac2..08ef286 100644 --- a/src/user_preferences/mod.rs +++ b/src/user_preferences/mod.rs @@ -8,6 +8,7 @@ pub mod v0; mod error; pub use error::ParseError; +use crate::user_preferences::v0::UserPreferencesV0; use crate::{InstanceSettings, Pronoun, WeightedTable}; use data_encoding::BASE32_NOPAD; @@ -85,10 +86,22 @@ impl Preference for UserPreferences { } fn from_prefstring_bytes(bytes: &[u8]) -> Result where Self: Sized { - todo!() + let version_byte = bytes.get(0).ok_or(ParseError::ZeroLengthPrefstring)?; + let version = version_byte >> 3; + let varient = version_byte & 0b111; + match (version, varient) { + (0, 0) => UserPreferencesV0::from_prefstring_bytes(bytes).map(|prefs| UserPreferences::V0(prefs)), + _ => Err(ParseError::VersionMismatch { + expected_version: 0..1, + expected_variant: 0..1, + actual_version_byte: *version_byte, + }) + } } fn into_prefstring_bytes(&self) -> Vec { - todo!() + match self { + UserPreferences::V0(pref) => pref, + }.into_prefstring_bytes() } }