use bytes for deck config proto

This commit is contained in:
Damien Elmes 2020-04-06 09:59:32 +10:00
parent 164bd29438
commit dc62d5072c
3 changed files with 15 additions and 17 deletions

View file

@ -99,10 +99,10 @@ message BackendOutput {
GetCardOut get_card = 38; GetCardOut get_card = 38;
Empty update_card = 39; Empty update_card = 39;
int64 add_card = 40; int64 add_card = 40;
string get_deck_config = 41; bytes get_deck_config = 41;
int64 add_or_update_deck_config = 42; int64 add_or_update_deck_config = 42;
string all_deck_config = 43; bytes all_deck_config = 43;
string new_deck_config = 44; bytes new_deck_config = 44;
Empty remove_deck_config = 45; Empty remove_deck_config = 45;
Empty before_upload = 47; Empty before_upload = 47;
bool register_tags = 48; bool register_tags = 48;
@ -434,7 +434,7 @@ message CloseCollectionIn {
} }
message AddOrUpdateDeckConfigIn { message AddOrUpdateDeckConfigIn {
string config = 1; bytes config = 1;
bool preserve_usn_and_mtime = 2; bool preserve_usn_and_mtime = 2;
} }

View file

@ -510,10 +510,10 @@ class RustBackend:
def get_deck_config(self, dcid: int) -> Dict[str, Any]: def get_deck_config(self, dcid: int) -> Dict[str, Any]:
jstr = self._run_command(pb.BackendInput(get_deck_config=dcid)).get_deck_config jstr = self._run_command(pb.BackendInput(get_deck_config=dcid)).get_deck_config
return json.loads(jstr) return orjson.loads(jstr)
def add_or_update_deck_config(self, conf: Dict[str, Any], preserve_usn) -> None: def add_or_update_deck_config(self, conf: Dict[str, Any], preserve_usn) -> None:
conf_json = json.dumps(conf) conf_json = orjson.dumps(conf)
id = self._run_command( id = self._run_command(
pb.BackendInput( pb.BackendInput(
add_or_update_deck_config=pb.AddOrUpdateDeckConfigIn( add_or_update_deck_config=pb.AddOrUpdateDeckConfigIn(
@ -527,13 +527,13 @@ class RustBackend:
jstr = self._run_command( jstr = self._run_command(
pb.BackendInput(all_deck_config=pb.Empty()) pb.BackendInput(all_deck_config=pb.Empty())
).all_deck_config ).all_deck_config
return json.loads(jstr) return orjson.loads(jstr)
def new_deck_config(self) -> Dict[str, Any]: def new_deck_config(self) -> Dict[str, Any]:
jstr = self._run_command( jstr = self._run_command(
pb.BackendInput(new_deck_config=pb.Empty()) pb.BackendInput(new_deck_config=pb.Empty())
).new_deck_config ).new_deck_config
return json.loads(jstr) return orjson.loads(jstr)
def remove_deck_config(self, dcid: int) -> None: def remove_deck_config(self, dcid: int) -> None:
self._run_command(pb.BackendInput(remove_deck_config=dcid)) self._run_command(pb.BackendInput(remove_deck_config=dcid))

View file

@ -717,15 +717,15 @@ impl Backend {
Ok(card.id.0) Ok(card.id.0)
} }
fn get_deck_config(&self, dcid: i64) -> Result<String> { fn get_deck_config(&self, dcid: i64) -> Result<Vec<u8>> {
self.with_col(|col| { self.with_col(|col| {
let conf = col.get_deck_config(DeckConfID(dcid), true)?.unwrap(); let conf = col.get_deck_config(DeckConfID(dcid), true)?.unwrap();
Ok(serde_json::to_string(&conf)?) Ok(serde_json::to_vec(&conf)?)
}) })
} }
fn add_or_update_deck_config(&self, input: AddOrUpdateDeckConfigIn) -> Result<i64> { fn add_or_update_deck_config(&self, input: AddOrUpdateDeckConfigIn) -> Result<i64> {
let mut conf: DeckConf = serde_json::from_str(&input.config)?; let mut conf: DeckConf = serde_json::from_slice(&input.config)?;
self.with_col(|col| { self.with_col(|col| {
col.transact(None, |col| { col.transact(None, |col| {
col.add_or_update_deck_config(&mut conf, input.preserve_usn_and_mtime)?; col.add_or_update_deck_config(&mut conf, input.preserve_usn_and_mtime)?;
@ -734,14 +734,12 @@ impl Backend {
}) })
} }
fn all_deck_config(&self) -> Result<String> { fn all_deck_config(&self) -> Result<Vec<u8>> {
self.with_col(|col| { self.with_col(|col| serde_json::to_vec(&col.storage.all_deck_config()?).map_err(Into::into))
serde_json::to_string(&col.storage.all_deck_config()?).map_err(Into::into)
})
} }
fn new_deck_config(&self) -> Result<String> { fn new_deck_config(&self) -> Result<Vec<u8>> {
serde_json::to_string(&DeckConf::default()).map_err(Into::into) serde_json::to_vec(&DeckConf::default()).map_err(Into::into)
} }
fn remove_deck_config(&self, dcid: i64) -> Result<()> { fn remove_deck_config(&self, dcid: i64) -> Result<()> {