add some more newtypes to the RPC defs

This commit is contained in:
Damien Elmes 2020-05-23 16:56:05 +10:00
parent 95735f106a
commit 6710e3d528
4 changed files with 79 additions and 53 deletions

View file

@ -33,14 +33,17 @@ message String {
string val = 1; string val = 1;
} }
message Bytes { message Json {
bytes val = 1; bytes json = 1;
} }
message Bool { message Bool {
bool val = 1; bool val = 1;
} }
// IDs used in RPC calls
///////////////////////////////////////////////////////////
message NoteTypeID { message NoteTypeID {
int64 ntid = 1; int64 ntid = 1;
} }
@ -53,6 +56,13 @@ message CardID {
int64 cid = 1; int64 cid = 1;
} }
message DeckID {
int64 did = 1;
}
message DeckConfigID {
int64 dcid = 1;
}
// New style RPC definitions // New style RPC definitions
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
@ -85,23 +95,23 @@ service BackendService {
// decks // decks
rpc AddOrUpdateDeckLegacy (AddOrUpdateDeckLegacyIn) returns (Int64); rpc AddOrUpdateDeckLegacy (AddOrUpdateDeckLegacyIn) returns (DeckID);
rpc DeckTree (DeckTreeIn) returns (DeckTreeNode); rpc DeckTree (DeckTreeIn) returns (DeckTreeNode);
rpc DeckTreeLegacy (Empty) returns (Bytes); rpc DeckTreeLegacy (Empty) returns (Json);
rpc GetAllDecksLegacy (Empty) returns (Bytes); rpc GetAllDecksLegacy (Empty) returns (Json);
rpc GetDeckIDByName (String) returns (Int64); rpc GetDeckIDByName (String) returns (DeckID);
rpc GetDeckLegacy (Int64) returns (Bytes); rpc GetDeckLegacy (DeckID) returns (Json);
rpc GetDeckNames (GetDeckNamesIn) returns (DeckNames); rpc GetDeckNames (GetDeckNamesIn) returns (DeckNames);
rpc NewDeckLegacy (Bool) returns (Bytes); rpc NewDeckLegacy (Bool) returns (Json);
rpc RemoveDeck (Int64) returns (Empty); rpc RemoveDeck (DeckID) returns (Empty);
// deck config // deck config
rpc AddOrUpdateDeckConfigLegacy (AddOrUpdateDeckConfigLegacyIn) returns (Int64); rpc AddOrUpdateDeckConfigLegacy (AddOrUpdateDeckConfigLegacyIn) returns (DeckConfigID);
rpc AllDeckConfigLegacy (Empty) returns (Bytes); rpc AllDeckConfigLegacy (Empty) returns (Json);
rpc GetDeckConfigLegacy (Int64) returns (Bytes); rpc GetDeckConfigLegacy (DeckConfigID) returns (Json);
rpc NewDeckConfigLegacy (Empty) returns (Bytes); rpc NewDeckConfigLegacy (Empty) returns (Json);
rpc RemoveDeckConfig (Int64) returns (Empty); rpc RemoveDeckConfig (DeckConfigID) returns (Empty);
// cards // cards

View file

@ -113,7 +113,10 @@ class DeckManager:
) )
def id_for_name(self, name: str) -> Optional[int]: 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]: def get_legacy(self, did: int) -> Optional[Dict]:
try: try:

View file

@ -587,9 +587,9 @@ class RustBackend:
input = pb.AddOrUpdateDeckLegacyIn( input = pb.AddOrUpdateDeckLegacyIn(
deck=deck, preserve_usn_and_mtime=preserve_usn_and_mtime deck=deck, preserve_usn_and_mtime=preserve_usn_and_mtime
) )
output = pb.Int64() output = pb.DeckID()
output.ParseFromString(self._run_command2(14, input)) 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: 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) 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: def get_deck_id_by_name(self, val: str) -> int:
input = pb.String(val=val) input = pb.String(val=val)
output = pb.Int64() output = pb.DeckID()
output.ParseFromString(self._run_command2(18, input)) output.ParseFromString(self._run_command2(18, input))
return output.val return output.did
def get_deck_legacy(self, val: int) -> bytes: def get_deck_legacy(self, did: int) -> bytes:
input = pb.Int64(val=val) input = pb.DeckID(did=did)
output = pb.Bytes() output = pb.Bytes()
output.ParseFromString(self._run_command2(19, input)) output.ParseFromString(self._run_command2(19, input))
return output.val return output.val
@ -637,8 +637,8 @@ class RustBackend:
output.ParseFromString(self._run_command2(21, input)) output.ParseFromString(self._run_command2(21, input))
return output.val return output.val
def remove_deck(self, val: int) -> pb.Empty: def remove_deck(self, did: int) -> pb.Empty:
input = pb.Int64(val=val) input = pb.DeckID(did=did)
output = pb.Empty() output = pb.Empty()
output.ParseFromString(self._run_command2(22, input)) output.ParseFromString(self._run_command2(22, input))
return output return output
@ -649,9 +649,9 @@ class RustBackend:
input = pb.AddOrUpdateDeckConfigLegacyIn( input = pb.AddOrUpdateDeckConfigLegacyIn(
config=config, preserve_usn_and_mtime=preserve_usn_and_mtime config=config, preserve_usn_and_mtime=preserve_usn_and_mtime
) )
output = pb.Int64() output = pb.DeckConfigID()
output.ParseFromString(self._run_command2(23, input)) output.ParseFromString(self._run_command2(23, input))
return output.val return output.dcid
def all_deck_config_legacy(self) -> bytes: def all_deck_config_legacy(self) -> bytes:
input = pb.Empty() input = pb.Empty()
@ -659,8 +659,8 @@ class RustBackend:
output.ParseFromString(self._run_command2(24, input)) output.ParseFromString(self._run_command2(24, input))
return output.val return output.val
def get_deck_config_legacy(self, val: int) -> bytes: def get_deck_config_legacy(self, dcid: int) -> bytes:
input = pb.Int64(val=val) input = pb.DeckConfigID(dcid=dcid)
output = pb.Bytes() output = pb.Bytes()
output.ParseFromString(self._run_command2(25, input)) output.ParseFromString(self._run_command2(25, input))
return output.val return output.val
@ -671,8 +671,8 @@ class RustBackend:
output.ParseFromString(self._run_command2(26, input)) output.ParseFromString(self._run_command2(26, input))
return output.val return output.val
def remove_deck_config(self, val: int) -> pb.Empty: def remove_deck_config(self, dcid: int) -> pb.Empty:
input = pb.Int64(val=val) input = pb.DeckConfigID(dcid=dcid)
output = pb.Empty() output = pb.Empty()
output.ParseFromString(self._run_command2(27, input)) output.ParseFromString(self._run_command2(27, input))
return output return output

View file

@ -148,9 +148,9 @@ pub fn init_backend(init_msg: &[u8]) -> std::result::Result<Backend, String> {
Ok(Backend::new(i18n, input.server)) Ok(Backend::new(i18n, input.server))
} }
impl From<Vec<u8>> for pb::Bytes { impl From<Vec<u8>> for pb::Json {
fn from(val: Vec<u8>) -> Self { fn from(json: Vec<u8>) -> Self {
pb::Bytes { val } pb::Json { json }
} }
} }
@ -196,6 +196,18 @@ impl From<pb::NoteTypeId> for NoteTypeID {
} }
} }
impl From<pb::DeckId> for DeckID {
fn from(did: pb::DeckId) -> Self {
DeckID(did.did)
}
}
impl From<pb::DeckConfigId> for DeckConfID {
fn from(dcid: pb::DeckConfigId) -> Self {
DeckConfID(dcid.dcid)
}
}
impl BackendService for Backend { impl BackendService for Backend {
// card rendering // card rendering
@ -369,7 +381,7 @@ impl BackendService for Backend {
self.with_col(|col| col.deck_tree(input.include_counts, lim)) self.with_col(|col| col.deck_tree(input.include_counts, lim))
} }
fn deck_tree_legacy(&mut self, _input: pb::Empty) -> BackendResult<pb::Bytes> { fn deck_tree_legacy(&mut self, _input: pb::Empty) -> BackendResult<pb::Json> {
self.with_col(|col| { self.with_col(|col| {
let tree = col.legacy_deck_tree()?; let tree = col.legacy_deck_tree()?;
serde_json::to_vec(&tree) serde_json::to_vec(&tree)
@ -378,11 +390,11 @@ impl BackendService for Backend {
}) })
} }
fn get_deck_legacy(&mut self, input: pb::Int64) -> Result<pb::Bytes> { fn get_deck_legacy(&mut self, input: pb::DeckId) -> Result<pb::Json> {
self.with_col(|col| { self.with_col(|col| {
let deck: DeckSchema11 = col let deck: DeckSchema11 = col
.storage .storage
.get_deck(DeckID(input.val))? .get_deck(input.into())?
.ok_or(AnkiError::NotFound)? .ok_or(AnkiError::NotFound)?
.into(); .into();
serde_json::to_vec(&deck) 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<pb::Int64> { fn get_deck_id_by_name(&mut self, input: pb::String) -> Result<pb::DeckId> {
self.with_col(|col| { self.with_col(|col| {
col.get_deck_id(&input.val) col.get_deck_id(&input.val).and_then(|d| {
.map(|d| d.map(|d| d.0).unwrap_or_default()) d.ok_or(AnkiError::NotFound)
.map(Into::into) .map(|d| pb::DeckId { did: d.0 })
})
}) })
} }
fn get_all_decks_legacy(&mut self, _input: Empty) -> BackendResult<pb::Bytes> { fn get_all_decks_legacy(&mut self, _input: Empty) -> BackendResult<pb::Json> {
self.with_col(|col| { self.with_col(|col| {
let decks = col.storage.get_all_decks_as_schema11()?; let decks = col.storage.get_all_decks_as_schema11()?;
serde_json::to_vec(&decks).map_err(Into::into) serde_json::to_vec(&decks).map_err(Into::into)
@ -426,7 +439,7 @@ impl BackendService for Backend {
fn add_or_update_deck_legacy( fn add_or_update_deck_legacy(
&mut self, &mut self,
input: pb::AddOrUpdateDeckLegacyIn, input: pb::AddOrUpdateDeckLegacyIn,
) -> Result<pb::Int64> { ) -> Result<pb::DeckId> {
self.with_col(|col| { self.with_col(|col| {
let schema11: DeckSchema11 = serde_json::from_slice(&input.deck)?; let schema11: DeckSchema11 = serde_json::from_slice(&input.deck)?;
let mut deck: Deck = schema11.into(); let mut deck: Deck = schema11.into();
@ -438,11 +451,11 @@ impl BackendService for Backend {
} else { } else {
col.add_or_update_deck(&mut deck)?; 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<pb::Bytes> { fn new_deck_legacy(&mut self, input: pb::Bool) -> BackendResult<pb::Json> {
let deck = if input.val { let deck = if input.val {
Deck::new_filtered() Deck::new_filtered()
} else { } else {
@ -454,8 +467,8 @@ impl BackendService for Backend {
.map(Into::into) .map(Into::into)
} }
fn remove_deck(&mut self, input: pb::Int64) -> BackendResult<Empty> { fn remove_deck(&mut self, input: pb::DeckId) -> BackendResult<Empty> {
self.with_col(|col| col.remove_deck_and_child_decks(DeckID(input.val))) self.with_col(|col| col.remove_deck_and_child_decks(input.into()))
.map(Into::into) .map(Into::into)
} }
@ -465,19 +478,19 @@ impl BackendService for Backend {
fn add_or_update_deck_config_legacy( fn add_or_update_deck_config_legacy(
&mut self, &mut self,
input: AddOrUpdateDeckConfigLegacyIn, input: AddOrUpdateDeckConfigLegacyIn,
) -> BackendResult<pb::Int64> { ) -> BackendResult<pb::DeckConfigId> {
let conf: DeckConfSchema11 = serde_json::from_slice(&input.config)?; let conf: DeckConfSchema11 = serde_json::from_slice(&input.config)?;
let mut conf: DeckConf = conf.into(); let mut conf: DeckConf = conf.into();
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)?;
Ok(conf.id.0) Ok(pb::DeckConfigId { dcid: conf.id.0 })
}) })
}) })
.map(Into::into) .map(Into::into)
} }
fn all_deck_config_legacy(&mut self, _input: Empty) -> BackendResult<pb::Bytes> { fn all_deck_config_legacy(&mut self, _input: Empty) -> BackendResult<pb::Json> {
self.with_col(|col| { self.with_col(|col| {
let conf: Vec<DeckConfSchema11> = col let conf: Vec<DeckConfSchema11> = col
.storage .storage
@ -490,20 +503,20 @@ impl BackendService for Backend {
.map(Into::into) .map(Into::into)
} }
fn new_deck_config_legacy(&mut self, _input: Empty) -> BackendResult<pb::Bytes> { fn new_deck_config_legacy(&mut self, _input: Empty) -> BackendResult<pb::Json> {
serde_json::to_vec(&DeckConfSchema11::default()) serde_json::to_vec(&DeckConfSchema11::default())
.map_err(Into::into) .map_err(Into::into)
.map(Into::into) .map(Into::into)
} }
fn remove_deck_config(&mut self, input: pb::Int64) -> BackendResult<Empty> { fn remove_deck_config(&mut self, input: pb::DeckConfigId) -> BackendResult<Empty> {
self.with_col(|col| col.transact(None, |col| col.remove_deck_config(DeckConfID(input.val)))) self.with_col(|col| col.transact(None, |col| col.remove_deck_config(input.into())))
.map(Into::into) .map(Into::into)
} }
fn get_deck_config_legacy(&mut self, input: pb::Int64) -> BackendResult<pb::Bytes> { fn get_deck_config_legacy(&mut self, input: pb::DeckConfigId) -> BackendResult<pb::Json> {
self.with_col(|col| { 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(); let conf: DeckConfSchema11 = conf.into();
Ok(serde_json::to_vec(&conf)?) Ok(serde_json::to_vec(&conf)?)
}) })