diff --git a/proto/backend.proto b/proto/backend.proto index 192450ce6..54958c848 100644 --- a/proto/backend.proto +++ b/proto/backend.proto @@ -33,14 +33,17 @@ message String { string val = 1; } -message Bytes { - bytes val = 1; +message Json { + bytes json = 1; } message Bool { bool val = 1; } +// IDs used in RPC calls +/////////////////////////////////////////////////////////// + message NoteTypeID { int64 ntid = 1; } @@ -53,6 +56,13 @@ message CardID { int64 cid = 1; } +message DeckID { + int64 did = 1; +} + +message DeckConfigID { + int64 dcid = 1; +} // New style RPC definitions /////////////////////////////////////////////////////////// @@ -85,23 +95,23 @@ service BackendService { // decks - rpc AddOrUpdateDeckLegacy (AddOrUpdateDeckLegacyIn) returns (Int64); + rpc AddOrUpdateDeckLegacy (AddOrUpdateDeckLegacyIn) returns (DeckID); rpc DeckTree (DeckTreeIn) returns (DeckTreeNode); - rpc DeckTreeLegacy (Empty) returns (Bytes); - rpc GetAllDecksLegacy (Empty) returns (Bytes); - rpc GetDeckIDByName (String) returns (Int64); - rpc GetDeckLegacy (Int64) returns (Bytes); + rpc DeckTreeLegacy (Empty) returns (Json); + rpc GetAllDecksLegacy (Empty) returns (Json); + rpc GetDeckIDByName (String) returns (DeckID); + rpc GetDeckLegacy (DeckID) returns (Json); rpc GetDeckNames (GetDeckNamesIn) returns (DeckNames); - rpc NewDeckLegacy (Bool) returns (Bytes); - rpc RemoveDeck (Int64) returns (Empty); + rpc NewDeckLegacy (Bool) returns (Json); + rpc RemoveDeck (DeckID) returns (Empty); // deck config - rpc AddOrUpdateDeckConfigLegacy (AddOrUpdateDeckConfigLegacyIn) returns (Int64); - rpc AllDeckConfigLegacy (Empty) returns (Bytes); - rpc GetDeckConfigLegacy (Int64) returns (Bytes); - rpc NewDeckConfigLegacy (Empty) returns (Bytes); - rpc RemoveDeckConfig (Int64) returns (Empty); + rpc AddOrUpdateDeckConfigLegacy (AddOrUpdateDeckConfigLegacyIn) returns (DeckConfigID); + rpc AllDeckConfigLegacy (Empty) returns (Json); + rpc GetDeckConfigLegacy (DeckConfigID) returns (Json); + rpc NewDeckConfigLegacy (Empty) returns (Json); + rpc RemoveDeckConfig (DeckConfigID) returns (Empty); // cards diff --git a/pylib/anki/decks.py b/pylib/anki/decks.py index c5e3c1bf1..b6d3320ca 100644 --- a/pylib/anki/decks.py +++ b/pylib/anki/decks.py @@ -113,7 +113,10 @@ class DeckManager: ) def id_for_name(self, name: str) -> Optional[int]: - return self.col.backend.get_deck_id_by_name(name) or None + try: + return self.col.backend.get_deck_id_by_name(name) + except NotFoundError: + return None def get_legacy(self, did: int) -> Optional[Dict]: try: diff --git a/pylib/anki/rsbackend.py b/pylib/anki/rsbackend.py index 332d16964..5db11cc5c 100644 --- a/pylib/anki/rsbackend.py +++ b/pylib/anki/rsbackend.py @@ -587,9 +587,9 @@ class RustBackend: input = pb.AddOrUpdateDeckLegacyIn( deck=deck, preserve_usn_and_mtime=preserve_usn_and_mtime ) - output = pb.Int64() + output = pb.DeckID() output.ParseFromString(self._run_command2(14, input)) - return output.val + return output.did def deck_tree(self, include_counts: bool, top_deck_id: int) -> pb.DeckTreeNode: input = pb.DeckTreeIn(include_counts=include_counts, top_deck_id=top_deck_id) @@ -611,12 +611,12 @@ class RustBackend: def get_deck_id_by_name(self, val: str) -> int: input = pb.String(val=val) - output = pb.Int64() + output = pb.DeckID() output.ParseFromString(self._run_command2(18, input)) - return output.val + return output.did - def get_deck_legacy(self, val: int) -> bytes: - input = pb.Int64(val=val) + def get_deck_legacy(self, did: int) -> bytes: + input = pb.DeckID(did=did) output = pb.Bytes() output.ParseFromString(self._run_command2(19, input)) return output.val @@ -637,8 +637,8 @@ class RustBackend: output.ParseFromString(self._run_command2(21, input)) return output.val - def remove_deck(self, val: int) -> pb.Empty: - input = pb.Int64(val=val) + def remove_deck(self, did: int) -> pb.Empty: + input = pb.DeckID(did=did) output = pb.Empty() output.ParseFromString(self._run_command2(22, input)) return output @@ -649,9 +649,9 @@ class RustBackend: input = pb.AddOrUpdateDeckConfigLegacyIn( config=config, preserve_usn_and_mtime=preserve_usn_and_mtime ) - output = pb.Int64() + output = pb.DeckConfigID() output.ParseFromString(self._run_command2(23, input)) - return output.val + return output.dcid def all_deck_config_legacy(self) -> bytes: input = pb.Empty() @@ -659,8 +659,8 @@ class RustBackend: output.ParseFromString(self._run_command2(24, input)) return output.val - def get_deck_config_legacy(self, val: int) -> bytes: - input = pb.Int64(val=val) + def get_deck_config_legacy(self, dcid: int) -> bytes: + input = pb.DeckConfigID(dcid=dcid) output = pb.Bytes() output.ParseFromString(self._run_command2(25, input)) return output.val @@ -671,8 +671,8 @@ class RustBackend: output.ParseFromString(self._run_command2(26, input)) return output.val - def remove_deck_config(self, val: int) -> pb.Empty: - input = pb.Int64(val=val) + def remove_deck_config(self, dcid: int) -> pb.Empty: + input = pb.DeckConfigID(dcid=dcid) output = pb.Empty() output.ParseFromString(self._run_command2(27, input)) return output diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index 9b272459d..bd500de2e 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -148,9 +148,9 @@ pub fn init_backend(init_msg: &[u8]) -> std::result::Result { Ok(Backend::new(i18n, input.server)) } -impl From> for pb::Bytes { - fn from(val: Vec) -> Self { - pb::Bytes { val } +impl From> for pb::Json { + fn from(json: Vec) -> Self { + pb::Json { json } } } @@ -196,6 +196,18 @@ impl From for NoteTypeID { } } +impl From for DeckID { + fn from(did: pb::DeckId) -> Self { + DeckID(did.did) + } +} + +impl From for DeckConfID { + fn from(dcid: pb::DeckConfigId) -> Self { + DeckConfID(dcid.dcid) + } +} + impl BackendService for Backend { // card rendering @@ -369,7 +381,7 @@ impl BackendService for Backend { self.with_col(|col| col.deck_tree(input.include_counts, lim)) } - fn deck_tree_legacy(&mut self, _input: pb::Empty) -> BackendResult { + fn deck_tree_legacy(&mut self, _input: pb::Empty) -> BackendResult { self.with_col(|col| { let tree = col.legacy_deck_tree()?; serde_json::to_vec(&tree) @@ -378,11 +390,11 @@ impl BackendService for Backend { }) } - fn get_deck_legacy(&mut self, input: pb::Int64) -> Result { + fn get_deck_legacy(&mut self, input: pb::DeckId) -> Result { self.with_col(|col| { let deck: DeckSchema11 = col .storage - .get_deck(DeckID(input.val))? + .get_deck(input.into())? .ok_or(AnkiError::NotFound)? .into(); serde_json::to_vec(&deck) @@ -391,15 +403,16 @@ impl BackendService for Backend { }) } - fn get_deck_id_by_name(&mut self, input: pb::String) -> Result { + fn get_deck_id_by_name(&mut self, input: pb::String) -> Result { self.with_col(|col| { - col.get_deck_id(&input.val) - .map(|d| d.map(|d| d.0).unwrap_or_default()) - .map(Into::into) + col.get_deck_id(&input.val).and_then(|d| { + d.ok_or(AnkiError::NotFound) + .map(|d| pb::DeckId { did: d.0 }) + }) }) } - fn get_all_decks_legacy(&mut self, _input: Empty) -> BackendResult { + fn get_all_decks_legacy(&mut self, _input: Empty) -> BackendResult { self.with_col(|col| { let decks = col.storage.get_all_decks_as_schema11()?; serde_json::to_vec(&decks).map_err(Into::into) @@ -426,7 +439,7 @@ impl BackendService for Backend { fn add_or_update_deck_legacy( &mut self, input: pb::AddOrUpdateDeckLegacyIn, - ) -> Result { + ) -> Result { self.with_col(|col| { let schema11: DeckSchema11 = serde_json::from_slice(&input.deck)?; let mut deck: Deck = schema11.into(); @@ -438,11 +451,11 @@ impl BackendService for Backend { } else { col.add_or_update_deck(&mut deck)?; } - Ok(deck.id.0.into()) + Ok(pb::DeckId { did: deck.id.0 }) }) } - fn new_deck_legacy(&mut self, input: pb::Bool) -> BackendResult { + fn new_deck_legacy(&mut self, input: pb::Bool) -> BackendResult { let deck = if input.val { Deck::new_filtered() } else { @@ -454,8 +467,8 @@ impl BackendService for Backend { .map(Into::into) } - fn remove_deck(&mut self, input: pb::Int64) -> BackendResult { - self.with_col(|col| col.remove_deck_and_child_decks(DeckID(input.val))) + fn remove_deck(&mut self, input: pb::DeckId) -> BackendResult { + self.with_col(|col| col.remove_deck_and_child_decks(input.into())) .map(Into::into) } @@ -465,19 +478,19 @@ impl BackendService for Backend { fn add_or_update_deck_config_legacy( &mut self, input: AddOrUpdateDeckConfigLegacyIn, - ) -> BackendResult { + ) -> BackendResult { let conf: DeckConfSchema11 = serde_json::from_slice(&input.config)?; let mut conf: DeckConf = conf.into(); self.with_col(|col| { col.transact(None, |col| { col.add_or_update_deck_config(&mut conf, input.preserve_usn_and_mtime)?; - Ok(conf.id.0) + Ok(pb::DeckConfigId { dcid: conf.id.0 }) }) }) .map(Into::into) } - fn all_deck_config_legacy(&mut self, _input: Empty) -> BackendResult { + fn all_deck_config_legacy(&mut self, _input: Empty) -> BackendResult { self.with_col(|col| { let conf: Vec = col .storage @@ -490,20 +503,20 @@ impl BackendService for Backend { .map(Into::into) } - fn new_deck_config_legacy(&mut self, _input: Empty) -> BackendResult { + fn new_deck_config_legacy(&mut self, _input: Empty) -> BackendResult { serde_json::to_vec(&DeckConfSchema11::default()) .map_err(Into::into) .map(Into::into) } - fn remove_deck_config(&mut self, input: pb::Int64) -> BackendResult { - self.with_col(|col| col.transact(None, |col| col.remove_deck_config(DeckConfID(input.val)))) + fn remove_deck_config(&mut self, input: pb::DeckConfigId) -> BackendResult { + self.with_col(|col| col.transact(None, |col| col.remove_deck_config(input.into()))) .map(Into::into) } - fn get_deck_config_legacy(&mut self, input: pb::Int64) -> BackendResult { + fn get_deck_config_legacy(&mut self, input: pb::DeckConfigId) -> BackendResult { self.with_col(|col| { - let conf = col.get_deck_config(DeckConfID(input.val), true)?.unwrap(); + let conf = col.get_deck_config(input.into(), true)?.unwrap(); let conf: DeckConfSchema11 = conf.into(); Ok(serde_json::to_vec(&conf)?) })