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;
}
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

View file

@ -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:

View file

@ -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

View file

@ -148,9 +148,9 @@ pub fn init_backend(init_msg: &[u8]) -> std::result::Result<Backend, String> {
Ok(Backend::new(i18n, input.server))
}
impl From<Vec<u8>> for pb::Bytes {
fn from(val: Vec<u8>) -> Self {
pb::Bytes { val }
impl From<Vec<u8>> for pb::Json {
fn from(json: Vec<u8>) -> Self {
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 {
// 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<pb::Bytes> {
fn deck_tree_legacy(&mut self, _input: pb::Empty) -> BackendResult<pb::Json> {
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<pb::Bytes> {
fn get_deck_legacy(&mut self, input: pb::DeckId) -> Result<pb::Json> {
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<pb::Int64> {
fn get_deck_id_by_name(&mut self, input: pb::String) -> Result<pb::DeckId> {
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<pb::Bytes> {
fn get_all_decks_legacy(&mut self, _input: Empty) -> BackendResult<pb::Json> {
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<pb::Int64> {
) -> Result<pb::DeckId> {
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<pb::Bytes> {
fn new_deck_legacy(&mut self, input: pb::Bool) -> BackendResult<pb::Json> {
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<Empty> {
self.with_col(|col| col.remove_deck_and_child_decks(DeckID(input.val)))
fn remove_deck(&mut self, input: pb::DeckId) -> BackendResult<Empty> {
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<pb::Int64> {
) -> BackendResult<pb::DeckConfigId> {
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<pb::Bytes> {
fn all_deck_config_legacy(&mut self, _input: Empty) -> BackendResult<pb::Json> {
self.with_col(|col| {
let conf: Vec<DeckConfSchema11> = col
.storage
@ -490,20 +503,20 @@ impl BackendService for Backend {
.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())
.map_err(Into::into)
.map(Into::into)
}
fn remove_deck_config(&mut self, input: pb::Int64) -> BackendResult<Empty> {
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<Empty> {
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<pb::Bytes> {
fn get_deck_config_legacy(&mut self, input: pb::DeckConfigId) -> BackendResult<pb::Json> {
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)?)
})