Skip to content

Commit

Permalink
attempts to make servicegenerator work
Browse files Browse the repository at this point in the history
Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed Aug 22, 2021
1 parent 9a31740 commit 80289eb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ Results of this step is committed already. But to run, invoke `just swagger`.


## Building
With all dependent `swagger` and `protos` files built, run:
To build the [out](./out) directory from [build.rs](./build.rs) using swagger and protobuf results run `just build`.

```bash
cargo build
```
Results of this step is committed already.

### Hack

Expand Down
24 changes: 12 additions & 12 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::rc::Rc;
#[derive(Default)]
struct GeneratorState {
service_names: Vec<String>,
package_names: Vec<String>,
finalized: usize,
generated: usize
}

struct KubeGenerator {
Expand All @@ -20,22 +20,21 @@ impl KubeGenerator {
}

impl prost_build::ServiceGenerator for KubeGenerator {
fn generate(&mut self, service: prost_build::Service, _buf: &mut String) {
fn generate(&mut self, service: prost_build::Service, buf: &mut String) {
let mut state = self.state.borrow_mut();
state.service_names.push(service.name);
state.generated += 1;
// TODO: THIS doesn't work? never called by prost_build, bug?
let generics = format!("// TODO: generate\n");
buf.push_str(&generics);
}

fn finalize(&mut self, _buf: &mut String) {
fn finalize(&mut self, buf: &mut String) {
let mut state = self.state.borrow_mut();
state.finalized += 1;
}

fn finalize_package(&mut self, package: &str, buf: &mut String) {
let mut state = self.state.borrow_mut();
state.package_names.push(package.to_string());
// TODO: generate generics for pkg here using self.data
let pkg_generics = format!("// blahtest");
buf.push_str(&pkg_generics);
// NB: THIS works, but we need a name here before it's useful
//let generics = format!("// TODO: finalize\n");
//buf.push_str(&generics);
}
}

Expand All @@ -47,12 +46,13 @@ fn main() -> std::io::Result<()> {
let state = Rc::new(RefCell::new(GeneratorState::default()));
prost_build::Config::new()
.service_generator(Box::new(KubeGenerator::new(Rc::clone(&state))))
.out_dir("./out")
.compile_protos(protos.as_slice(), &["protos/"])?;

// sanity
let state = state.borrow();
//assert_eq!(state.service_names.len(), protos.len()); zero atm..
assert_eq!(state.finalized, protos.len());
assert_eq!(state.generated, protos.len()); // TODO: why does generate not trigger

// Generate code in `src/` by reading files in `OUT_DIR`?
// Need to create `mod.rs` file for each level based on the filename, and write generated code in correct file.
Expand Down
7 changes: 7 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@ swagger-transform:
# Download and generate all swagger dependent files
swagger: swagger-dl swagger-patch swagger-transform

# Generate the library code from completed swagger and protos
build:
#!/usr/bin/env bash
set -exuo pipefail
rm -rf out/ && mkdir out
cargo build

0 comments on commit 80289eb

Please sign in to comment.