Anki/proto/anki/card_rendering.proto
Damien Elmes 553303fc12
Refactor service generation (#2552)
* Automatically elide empty inputs and outputs to backend methods

* Refactor service generation

Despite the fact that the majority of our Protobuf service methods require
an open collection, they were not accessible with just a Collection
object. To access the methods (e.g. because we haven't gotten around to
exposing the correct API in Collection yet), you had to wrap the collection
in a Backend object, and pay a mutex-acquisition cost for each call, even
if you have exclusive access to the object.

This commit migrates the majority of service methods to the Collection, so
they can now be used directly, and improves the ergonomics a bit at the
same time.

The approach taken:

- The service generation now happens in rslib instead of anki_proto, which
avoids the need for trait constraints and associated types.
- Service methods are assumed to be collection-based by default. Instead of
implementing the service on Backend, we now implement it on Collection, which
means our methods no longer need to use self.with_col(...).
- We automatically generate methods in Backend which use self.with_col() to
delegate to the Collection method.
- For methods that are only appropriate for the backend, we add a flag in
the .proto file. The codegen uses this flag to write the method into a
BackendFooService instead of FooService, which the backend implements.
- The flag can also allows us to define separate implementations for collection
and backend, so we can e.g. skip the collection mutex in the i18n service
while also providing the service on a collection.
2023-06-19 15:33:40 +10:00

174 lines
3.9 KiB
Protocol Buffer

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
syntax = "proto3";
option java_multiple_files = true;
package anki.card_rendering;
import "anki/generic.proto";
import "anki/notes.proto";
import "anki/notetypes.proto";
import "anki/codegen.proto";
service CardRenderingService {
rpc ExtractAvTags(ExtractAvTagsRequest) returns (ExtractAvTagsResponse);
rpc ExtractLatex(ExtractLatexRequest) returns (ExtractLatexResponse);
rpc GetEmptyCards(generic.Empty) returns (EmptyCardsReport);
rpc RenderExistingCard(RenderExistingCardRequest)
returns (RenderCardResponse);
rpc RenderUncommittedCard(RenderUncommittedCardRequest)
returns (RenderCardResponse);
rpc RenderUncommittedCardLegacy(RenderUncommittedCardLegacyRequest)
returns (RenderCardResponse);
rpc StripAvTags(generic.String) returns (generic.String);
rpc RenderMarkdown(RenderMarkdownRequest) returns (generic.String);
rpc EncodeIriPaths(generic.String) returns (generic.String);
rpc DecodeIriPaths(generic.String) returns (generic.String);
rpc StripHtml(StripHtmlRequest) returns (generic.String) {
// a bunch of our unit tests access this without a collection
option (codegen.rust_methods) = RUST_METHODS_COLLECTION_AND_MANUAL_BACKEND;
}
rpc CompareAnswer(CompareAnswerRequest) returns (generic.String);
rpc ExtractClozeForTyping(ExtractClozeForTypingRequest)
returns (generic.String);
rpc AllTtsVoices(AllTtsVoicesRequest) returns (AllTtsVoicesResponse);
rpc WriteTtsStream(WriteTtsStreamRequest) returns (generic.Empty);
}
message ExtractAvTagsRequest {
string text = 1;
bool question_side = 2;
}
message ExtractAvTagsResponse {
string text = 1;
repeated AVTag av_tags = 2;
}
message AVTag {
oneof value {
string sound_or_video = 1;
TTSTag tts = 2;
}
}
message TTSTag {
string field_text = 1;
string lang = 2;
repeated string voices = 3;
float speed = 4;
repeated string other_args = 5;
}
message ExtractLatexRequest {
string text = 1;
bool svg = 2;
bool expand_clozes = 3;
}
message ExtractLatexResponse {
string text = 1;
repeated ExtractedLatex latex = 2;
}
message ExtractedLatex {
string filename = 1;
string latex_body = 2;
}
message EmptyCardsReport {
message NoteWithEmptyCards {
int64 note_id = 1;
repeated int64 card_ids = 2;
bool will_delete_note = 3;
}
string report = 1;
repeated NoteWithEmptyCards notes = 2;
}
message RenderExistingCardRequest {
int64 card_id = 1;
bool browser = 2;
}
message RenderUncommittedCardRequest {
notes.Note note = 1;
uint32 card_ord = 2;
notetypes.Notetype.Template template = 3;
bool fill_empty = 4;
}
message RenderUncommittedCardLegacyRequest {
notes.Note note = 1;
uint32 card_ord = 2;
bytes template = 3;
bool fill_empty = 4;
}
message RenderCardResponse {
repeated RenderedTemplateNode question_nodes = 1;
repeated RenderedTemplateNode answer_nodes = 2;
string css = 3;
bool latex_svg = 4;
}
message RenderedTemplateNode {
oneof value {
string text = 1;
RenderedTemplateReplacement replacement = 2;
}
}
message RenderedTemplateReplacement {
string field_name = 1;
string current_text = 2;
repeated string filters = 3;
}
message RenderMarkdownRequest {
string markdown = 1;
bool sanitize = 2;
}
message StripHtmlRequest {
enum Mode {
NORMAL = 0;
PRESERVE_MEDIA_FILENAMES = 1;
}
string text = 1;
Mode mode = 2;
}
message CompareAnswerRequest {
string expected = 1;
string provided = 2;
}
message ExtractClozeForTypingRequest {
string text = 1;
uint32 ordinal = 2;
}
message AllTtsVoicesRequest {
bool validate = 1;
}
message AllTtsVoicesResponse {
message TtsVoice {
string id = 1;
string name = 2;
string language = 3;
optional bool available = 4;
}
repeated TtsVoice voices = 1;
}
message WriteTtsStreamRequest {
string path = 1;
string voice_id = 2;
float speed = 3;
string text = 4;
}