2021-01-31 21:50:21 +01:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2021-04-18 10:29:20 +02:00
|
|
|
use std::{env, fmt::Write, path::PathBuf};
|
2020-11-01 05:26:58 +01:00
|
|
|
|
|
|
|
struct CustomGenerator {}
|
|
|
|
|
|
|
|
fn write_method_trait(buf: &mut String, service: &prost_build::Service) {
|
|
|
|
buf.push_str(
|
|
|
|
r#"
|
2021-03-11 05:33:57 +01:00
|
|
|
pub trait Service {
|
|
|
|
fn run_method(&self, method: u32, input: &[u8]) -> Result<Vec<u8>> {
|
2020-11-01 05:26:58 +01:00
|
|
|
match method {
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
for (idx, method) in service.methods.iter().enumerate() {
|
|
|
|
write!(
|
|
|
|
buf,
|
|
|
|
concat!(" ",
|
refactor protobuf handling for split/import
In order to split backend.proto into a more manageable size, the protobuf
handling needed to be updated. This took more time than I would have
liked, as each language handles protobuf differently:
- The Python Protobuf code ignores "package" directives, and relies
solely on how the files are laid out on disk. While it would have been
nice to keep the generated files in a private subpackage, Protobuf gets
confused if the files are located in a location that does not match
their original .proto layout, so the old approach of storing them in
_backend/ will not work. They now clutter up pylib/anki instead. I'm
rather annoyed by that, but alternatives seem to be having to add an extra
level to the Protobuf path, making the other languages suffer, or trying
to hack around the issue by munging sys.modules.
- Protobufjs fails to expose packages if they don't start with a capital
letter, despite the fact that lowercase packages are the norm in most
languages :-( This required a patch to fix.
- Rust was the easiest, as Prost is relatively straightforward compared
to Google's tools.
The Protobuf files are now stored in /proto/anki, with a separate package
for each file. I've split backend.proto into a few files as a test, but
the majority of that work is still to come.
The Python Protobuf building is a bit of a hack at the moment, hard-coding
"proto" as the top level folder, but it seems to get the job done for now.
Also changed the workspace name, as there seems to be a number of Bazel
repos moving away from the more awkward reverse DNS naming style.
2021-07-10 09:50:18 +02:00
|
|
|
"{idx} => {{ let input = super::{input_type}::decode(input)?;\n",
|
2020-11-01 05:26:58 +01:00
|
|
|
"let output = self.{rust_method}(input)?;\n",
|
|
|
|
"let mut out_bytes = Vec::new(); output.encode(&mut out_bytes)?; Ok(out_bytes) }}, "),
|
2021-03-11 07:53:36 +01:00
|
|
|
idx = idx,
|
2020-11-01 05:26:58 +01:00
|
|
|
input_type = method.input_type,
|
|
|
|
rust_method = method.name
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
buf.push_str(
|
|
|
|
r#"
|
2021-04-01 08:06:24 +02:00
|
|
|
_ => Err(crate::error::AnkiError::invalid_input("invalid command")),
|
2020-11-01 05:26:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
for method in &service.methods {
|
|
|
|
write!(
|
|
|
|
buf,
|
|
|
|
concat!(
|
refactor protobuf handling for split/import
In order to split backend.proto into a more manageable size, the protobuf
handling needed to be updated. This took more time than I would have
liked, as each language handles protobuf differently:
- The Python Protobuf code ignores "package" directives, and relies
solely on how the files are laid out on disk. While it would have been
nice to keep the generated files in a private subpackage, Protobuf gets
confused if the files are located in a location that does not match
their original .proto layout, so the old approach of storing them in
_backend/ will not work. They now clutter up pylib/anki instead. I'm
rather annoyed by that, but alternatives seem to be having to add an extra
level to the Protobuf path, making the other languages suffer, or trying
to hack around the issue by munging sys.modules.
- Protobufjs fails to expose packages if they don't start with a capital
letter, despite the fact that lowercase packages are the norm in most
languages :-( This required a patch to fix.
- Rust was the easiest, as Prost is relatively straightforward compared
to Google's tools.
The Protobuf files are now stored in /proto/anki, with a separate package
for each file. I've split backend.proto into a few files as a test, but
the majority of that work is still to come.
The Python Protobuf building is a bit of a hack at the moment, hard-coding
"proto" as the top level folder, but it seems to get the job done for now.
Also changed the workspace name, as there seems to be a number of Bazel
repos moving away from the more awkward reverse DNS naming style.
2021-07-10 09:50:18 +02:00
|
|
|
" fn {method_name}(&self, input: super::{input_type}) -> ",
|
|
|
|
"Result<super::{output_type}>;\n"
|
2020-11-01 05:26:58 +01:00
|
|
|
),
|
|
|
|
method_name = method.name,
|
|
|
|
input_type = method.input_type,
|
|
|
|
output_type = method.output_type
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
buf.push_str("}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
impl prost_build::ServiceGenerator for CustomGenerator {
|
|
|
|
fn generate(&mut self, service: prost_build::Service, buf: &mut String) {
|
2021-03-11 05:33:57 +01:00
|
|
|
write!(
|
|
|
|
buf,
|
2021-03-11 06:47:31 +01:00
|
|
|
"pub mod {name}_service {{
|
2021-03-11 05:33:57 +01:00
|
|
|
use prost::Message;
|
2021-04-01 08:06:24 +02:00
|
|
|
use crate::error::Result;
|
2021-03-11 05:33:57 +01:00
|
|
|
",
|
|
|
|
name = service.name.replace("Service", "").to_ascii_lowercase()
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-11-01 05:26:58 +01:00
|
|
|
write_method_trait(buf, &service);
|
2021-03-11 05:33:57 +01:00
|
|
|
buf.push('}');
|
2020-11-01 05:26:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn service_generator() -> Box<dyn prost_build::ServiceGenerator> {
|
|
|
|
Box::new(CustomGenerator {})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_backend_proto_rs() {
|
refactor protobuf handling for split/import
In order to split backend.proto into a more manageable size, the protobuf
handling needed to be updated. This took more time than I would have
liked, as each language handles protobuf differently:
- The Python Protobuf code ignores "package" directives, and relies
solely on how the files are laid out on disk. While it would have been
nice to keep the generated files in a private subpackage, Protobuf gets
confused if the files are located in a location that does not match
their original .proto layout, so the old approach of storing them in
_backend/ will not work. They now clutter up pylib/anki instead. I'm
rather annoyed by that, but alternatives seem to be having to add an extra
level to the Protobuf path, making the other languages suffer, or trying
to hack around the issue by munging sys.modules.
- Protobufjs fails to expose packages if they don't start with a capital
letter, despite the fact that lowercase packages are the norm in most
languages :-( This required a patch to fix.
- Rust was the easiest, as Prost is relatively straightforward compared
to Google's tools.
The Protobuf files are now stored in /proto/anki, with a separate package
for each file. I've split backend.proto into a few files as a test, but
the majority of that work is still to come.
The Python Protobuf building is a bit of a hack at the moment, hard-coding
"proto" as the top level folder, but it seems to get the job done for now.
Also changed the workspace name, as there seems to be a number of Bazel
repos moving away from the more awkward reverse DNS naming style.
2021-07-10 09:50:18 +02:00
|
|
|
let proto_dir = if let Ok(proto) = env::var("PROTO_TOP") {
|
2021-07-12 08:15:38 +02:00
|
|
|
PathBuf::from(proto).parent().unwrap().to_owned()
|
2020-11-24 09:51:19 +01:00
|
|
|
} else {
|
refactor protobuf handling for split/import
In order to split backend.proto into a more manageable size, the protobuf
handling needed to be updated. This took more time than I would have
liked, as each language handles protobuf differently:
- The Python Protobuf code ignores "package" directives, and relies
solely on how the files are laid out on disk. While it would have been
nice to keep the generated files in a private subpackage, Protobuf gets
confused if the files are located in a location that does not match
their original .proto layout, so the old approach of storing them in
_backend/ will not work. They now clutter up pylib/anki instead. I'm
rather annoyed by that, but alternatives seem to be having to add an extra
level to the Protobuf path, making the other languages suffer, or trying
to hack around the issue by munging sys.modules.
- Protobufjs fails to expose packages if they don't start with a capital
letter, despite the fact that lowercase packages are the norm in most
languages :-( This required a patch to fix.
- Rust was the easiest, as Prost is relatively straightforward compared
to Google's tools.
The Protobuf files are now stored in /proto/anki, with a separate package
for each file. I've split backend.proto into a few files as a test, but
the majority of that work is still to come.
The Python Protobuf building is a bit of a hack at the moment, hard-coding
"proto" as the top level folder, but it seems to get the job done for now.
Also changed the workspace name, as there seems to be a number of Bazel
repos moving away from the more awkward reverse DNS naming style.
2021-07-10 09:50:18 +02:00
|
|
|
PathBuf::from("../proto")
|
|
|
|
};
|
|
|
|
|
|
|
|
let subfolders = &["anki"];
|
|
|
|
let mut paths = vec![];
|
|
|
|
for subfolder in subfolders {
|
|
|
|
for entry in proto_dir.join(subfolder).read_dir().unwrap() {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
let path = entry.path();
|
|
|
|
if path
|
|
|
|
.file_name()
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.ends_with(".proto")
|
|
|
|
{
|
|
|
|
println!("cargo:rerun-if-changed={}", path.to_str().unwrap());
|
|
|
|
paths.push(path);
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 09:51:19 +01:00
|
|
|
}
|
2020-11-01 05:26:58 +01:00
|
|
|
|
2021-07-12 08:15:38 +02:00
|
|
|
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
2020-11-01 05:26:58 +01:00
|
|
|
let mut config = prost_build::Config::new();
|
|
|
|
config
|
|
|
|
.out_dir(&out_dir)
|
|
|
|
.service_generator(service_generator())
|
2021-04-01 15:50:16 +02:00
|
|
|
.type_attribute(
|
2021-04-04 13:15:30 +02:00
|
|
|
"Deck.Filtered.SearchTerm.Order",
|
2021-04-01 15:50:16 +02:00
|
|
|
"#[derive(strum::EnumIter)]",
|
|
|
|
)
|
2021-07-22 16:32:49 +02:00
|
|
|
.type_attribute(
|
|
|
|
"HelpPageLinkRequest.HelpPage",
|
|
|
|
"#[derive(strum::EnumIter)]",
|
|
|
|
)
|
2021-07-12 08:15:38 +02:00
|
|
|
.compile_protos(paths.as_slice(), &[proto_dir])
|
2020-11-01 05:26:58 +01:00
|
|
|
.unwrap();
|
|
|
|
}
|