From db9372d5d01278c64bdca64f0468d7ea61214435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gianguido=20Sor=C3=A0?= Date: Mon, 10 Oct 2022 15:36:36 +0200 Subject: [PATCH] make the thing compile --- Cargo.toml | 2 +- libs/userprefs/prefsgenerator/src/lib.rs | 22 ++---- libs/userprefs/src/lib.rs | 85 ++---------------------- 3 files changed, 11 insertions(+), 98 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e487a5c1e..3aaa44242 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ default-members = [ "services/dns", "services/modals", "services/usb-device-xous", - "lib/userprefs" + "libs/userprefs" ] members = [ "xous-ipc", diff --git a/libs/userprefs/prefsgenerator/src/lib.rs b/libs/userprefs/prefsgenerator/src/lib.rs index fadee8519..0ebf96326 100644 --- a/libs/userprefs/prefsgenerator/src/lib.rs +++ b/libs/userprefs/prefsgenerator/src/lib.rs @@ -15,6 +15,7 @@ pub fn getter_setter(input: TokenStream) -> TokenStream { syn::Fields::Named(FieldsNamed { named, .. }) => { for field in &named { let ident = field.ident.as_ref().unwrap().clone(); + let ident_str = ident.to_string(); let typ = field.ty.clone(); let set_fn_name = format_ident!("set_{}", ident); @@ -22,27 +23,17 @@ pub fn getter_setter(input: TokenStream) -> TokenStream { let read = quote! { impl Manager { - // pub fn #set_fn_name(&mut self, value: #typ) { - // self.prefs.#ident = value; - // self.pddb_store(&self.prefs).unwrap() - // } - - // pub fn #fn_name(&mut self) -> #typ { - // self.prefs = self.pddb_get().unwrap(); - // self.prefs.#ident - // } - pub fn #set_fn_name(&mut self, value: #typ) -> Result<(), Error> { let bytes: Vec = match bincode::encode_to_vec(value, bincode::config::standard()) { - Ok(ret) => Ok(ret), + Ok(ret) => ret, Err(err) => return Err(Error::EncodeError(err)), }; - self.pddb_store_key(#fn_name, &bytes) + self.pddb_store_key(#ident_str, &bytes) } pub fn #fn_name(&mut self) -> Result<#typ, Error> { - let bytes = self.pddb_get_key("#fn_name")?; + let bytes = self.pddb_get_key(#ident_str)?; let ret: #typ = match bincode::decode_from_slice(&bytes, bincode::config::standard()) { Ok((data, _)) => data, Err(err) => return Err(Error::DecodeError(err)), @@ -52,9 +43,8 @@ pub fn getter_setter(input: TokenStream) -> TokenStream { } } }; - println!("code: {}", read); - panic!(); - //rw_methods.extend(read); + + rw_methods.extend(read); } } _ => (), diff --git a/libs/userprefs/src/lib.rs b/libs/userprefs/src/lib.rs index 216a8a9db..f386636a1 100644 --- a/libs/userprefs/src/lib.rs +++ b/libs/userprefs/src/lib.rs @@ -4,7 +4,6 @@ use std::io::Read; use std::io::Write; static PREFS_DICT: &str = "UserPrefsDict"; -static PREFS_KEY: &str = "UserPrefsKey"; #[derive(Debug)] pub enum Error { @@ -35,7 +34,6 @@ impl From for Error { bincode::Encode, bincode::Decode, PartialEq, Debug, Default, prefsgenerator::GetterSetter, )] struct UserPrefs { - // TODO: optimize maybe by adding a dirty bit? radio_on_on_boot: bool, connect_known_networks_on_boot: bool, autobacklight_on_boot: bool, @@ -43,86 +41,14 @@ struct UserPrefs { close_bases_on_lock: bool, } -impl TryFrom> for UserPrefs { - fn try_from(data: Vec) -> Result { - if data.is_empty() { - return Ok(Self::default()); - } - let decoded: UserPrefs = - match bincode::decode_from_slice(&data, bincode::config::standard()) { - Ok((data, _)) => data, - Err(err) => return Err(Error::DecodeError(err)), - }; - - Ok(decoded) - } - - type Error = Error; -} - -impl TryFrom<&UserPrefs> for Vec { - fn try_from(up: &UserPrefs) -> Result { - match bincode::encode_to_vec(up, bincode::config::standard()) { - Ok(ret) => Ok(ret), - Err(err) => Err(Error::EncodeError(err)), - } - } - - type Error = Error; -} - pub struct Manager { pddb_handle: Pddb, - prefs: UserPrefs, } impl Manager { fn new() -> Self { - let mut us = Self { + Self { pddb_handle: Pddb::new(), - prefs: UserPrefs::default(), - }; - - us.prefs = us.pddb_get().unwrap(); - - us - } - - fn pddb_store(&self, payload: &UserPrefs) -> Result<(), Error> { - let payload: Vec = payload.try_into()?; - match self.pddb_handle.get( - PREFS_DICT, - PREFS_KEY, - Some(".System"), - true, - true, - None, - None::, - ) { - Ok(mut data) => match data.write(&payload) { - Ok(_) => Ok(self.pddb_handle.sync().unwrap_or(())), - Err(e) => Err(e.into()), - }, - Err(e) => Err(e.into()), - } - } - - fn pddb_get(&self) -> Result { - match self.pddb_handle.get( - PREFS_DICT, - PREFS_KEY, - Some(".System"), - true, - true, - None, - None::, - ) { - Ok(mut record) => { - let mut data = Vec::::new(); - record.read_to_end(&mut data)?; - Ok(data.try_into()?) - } - Err(e) => return Err(e.into()), } } @@ -147,7 +73,7 @@ impl Manager { fn pddb_get_key(&self, key: &str) -> Result, Error> { match self.pddb_handle.get( PREFS_DICT, - PREFS_KEY, + key, Some(".System"), true, true, @@ -171,11 +97,8 @@ impl Default for Manager { } mod test { - use crate::{Manager, UserPrefs}; - #[test] fn describe() { - //UserPrefs::describe(); - let m = Manager::new(); - } + use crate::Manager; + let _m = Manager::new(); }