diff --git a/sqlc-gen/Cargo.toml b/sqlc-gen/Cargo.toml index 0ffd820..f380d1e 100644 --- a/sqlc-gen/Cargo.toml +++ b/sqlc-gen/Cargo.toml @@ -10,7 +10,7 @@ lto = true sqlparser = { version = "0.9.0", features = ["serde"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -prost = "0.9.0" +prost = "0.13" bytes = "1.1.0" syn = "2.0.77" quote = "1.0.37" @@ -23,6 +23,8 @@ strum_macros = "0.26.4" itertools = "0.13.0" pluralizer = "0.4.0" check_keyword = "0.3.1" +base64 = "0.22.1" +sqlc_sqlc_community_neoeinstein-prost = { version = "0.4.0-20231002190240-3f2d312ab6fd.1", registry = "buf" } [build-dependencies] prost-build = "0.9.0" diff --git a/sqlc-gen/Makefile b/sqlc-gen/Makefile index 6400801..2f53ddd 100644 --- a/sqlc-gen/Makefile +++ b/sqlc-gen/Makefile @@ -13,7 +13,7 @@ get-outer-examples: echo $(EXAMPLES_OUTER) | make -s prepare-list generate-for-example: - @echo "generating for $$example" && \ + @echo "generating for $$example_dir" && \ cd $$example_dir && \ file_with_ext=$$(ls sqlc.*) && \ file_ext=$${file_with_ext##*.} && \ diff --git a/sqlc-gen/README.md b/sqlc-gen/README.md index 3bba560..56cd239 100644 --- a/sqlc-gen/README.md +++ b/sqlc-gen/README.md @@ -23,6 +23,12 @@ sql: lang: en-us ``` +### Setup + +- Go to [https://buf.build/sqlc/sqlc/sdks/v1.22.0](https://buf.build/sqlc/sqlc/sdks/v1.22.0) +- Follow the instructions for Rust - community/neoeinstein-prost + + ### Run The following command generates rust code for all examples diff --git a/sqlc-gen/buf.gen.yaml b/sqlc-gen/buf.gen.yaml new file mode 100644 index 0000000..eb66434 --- /dev/null +++ b/sqlc-gen/buf.gen.yaml @@ -0,0 +1,7 @@ +# buf.gen.yaml +version: v2 +managed: + enabled: false +plugins: + - remote: buf.build/community/neoeinstein-prost:v0.4.0 + out: src/codegen diff --git a/sqlc-gen/build.rs b/sqlc-gen/build.rs index 4636c93..a7d6dda 100644 --- a/sqlc-gen/build.rs +++ b/sqlc-gen/build.rs @@ -1,5 +1,14 @@ -extern crate prost_build; +// extern crate prost_build; fn main() { - prost_build::compile_protos(&["src/codegen.proto"], &["src/"]).unwrap(); + // // prost_build::compile_protos(&["src/codegen.proto"], &["src/"]).unwrap(); + // let status = std::process::Command::new("buf") + // .arg("generate") + // .current_dir(env!("CARGO_MANIFEST_DIR")) + // .status() + // .unwrap(); + // + // if !status.success() { + // std::process::exit(status.code().unwrap_or(-1)) + // } } diff --git a/sqlc-gen/examples/authors/sqlc.yaml b/sqlc-gen/examples/authors/sqlc.yaml index 6bde1c9..7b92b7d 100644 --- a/sqlc-gen/examples/authors/sqlc.yaml +++ b/sqlc-gen/examples/authors/sqlc.yaml @@ -6,7 +6,7 @@ plugins: - RUST_LOG wasm: url: file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm - sha256: 8920bd7d234e7705f537979be05780fd08a8285fbae26afec268caad3d064867 + sha256: d5c79985e5590cd3f29b0236203e9a054744d4edbbdfef2b91dc418dce8c0066 # # - name: js # process: diff --git a/sqlc-gen/examples/batch/sqlc.json b/sqlc-gen/examples/batch/sqlc.json index 8c95452..4d30157 100644 --- a/sqlc-gen/examples/batch/sqlc.json +++ b/sqlc-gen/examples/batch/sqlc.json @@ -9,7 +9,7 @@ ], "wasm": { "url": "file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm", - "sha256": "8920bd7d234e7705f537979be05780fd08a8285fbae26afec268caad3d064867" + "sha256": "d5c79985e5590cd3f29b0236203e9a054744d4edbbdfef2b91dc418dce8c0066" } } ], diff --git a/sqlc-gen/examples/booktest/sqlc.yaml b/sqlc-gen/examples/booktest/sqlc.yaml index aa3e2bc..ac6cbef 100644 --- a/sqlc-gen/examples/booktest/sqlc.yaml +++ b/sqlc-gen/examples/booktest/sqlc.yaml @@ -6,7 +6,7 @@ plugins: - RUST_LOG wasm: url: file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm - sha256: 8920bd7d234e7705f537979be05780fd08a8285fbae26afec268caad3d064867 + sha256: d5c79985e5590cd3f29b0236203e9a054744d4edbbdfef2b91dc418dce8c0066 # # - name: js # process: diff --git a/sqlc-gen/examples/jets/sqlc.json b/sqlc-gen/examples/jets/sqlc.json index 408f496..58d65cf 100644 --- a/sqlc-gen/examples/jets/sqlc.json +++ b/sqlc-gen/examples/jets/sqlc.json @@ -9,7 +9,7 @@ ], "wasm": { "url": "file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm", - "sha256": "8920bd7d234e7705f537979be05780fd08a8285fbae26afec268caad3d064867" + "sha256": "d5c79985e5590cd3f29b0236203e9a054744d4edbbdfef2b91dc418dce8c0066" } } ], diff --git a/sqlc-gen/examples/ondeck/sqlc.json b/sqlc-gen/examples/ondeck/sqlc.json index d567263..c0fe40e 100644 --- a/sqlc-gen/examples/ondeck/sqlc.json +++ b/sqlc-gen/examples/ondeck/sqlc.json @@ -9,7 +9,7 @@ ], "wasm": { "url": "file://./../../../target/wasm32-wasip1/release/sqlc-gen.wasm", - "sha256": "8920bd7d234e7705f537979be05780fd08a8285fbae26afec268caad3d064867" + "sha256": "d5c79985e5590cd3f29b0236203e9a054744d4edbbdfef2b91dc418dce8c0066" } } ], diff --git a/sqlc-gen/src/codegen.proto b/sqlc-gen/src/codegen.proto index 85e927b..e6faf19 100644 --- a/sqlc-gen/src/codegen.proto +++ b/sqlc-gen/src/codegen.proto @@ -1,135 +1,56 @@ -// https://github.com/kyleconroy/sqlc/blob/main/protos/plugin/codegen.proto syntax = "proto3"; package plugin; -option go_package = "github.com/kyleconroy/sqlc/internal/plugin"; - -message File -{ - string name = 1 [json_name="name"]; - bytes contents = 2 [json_name="contents"]; +service CodegenService { + rpc Generate (GenerateRequest) returns (GenerateResponse); } -message Override { - // name of the type to use, e.g. `github.com/segmentio/ksuid.KSUID` or `mymodule.Type` - string code_type = 1 [json_name="code_type"]; - - // name of the type to use, e.g. `text` - string db_type = 3 [json_name="db_type"]; - - // True if the override should apply to a nullable database type - bool nullable = 5 [json_name="nullable"]; - - // fully qualified name of the column, e.g. `accounts.id` - string column = 6 [json_name="column"]; - - Identifier table = 7 [json_name="table"]; - - string column_name = 8 [json_name="column_name"]; - - PythonType python_type = 9; - - ParsedGoType go_type = 10; +message File { + string name = 1 [json_name = "name"]; + bytes contents = 2 [json_name = "contents"]; } -message PythonType -{ - string module = 1; - string name = 2; -} +message Settings { + // Rename message was field 5 + // Overides message was field 6 + // PythonCode message was field 8 + // KotlinCode message was field 9 + // GoCode message was field 10; + // JSONCode message was field 11; + reserved 5, 8, 9, 10, 11; -message ParsedGoType -{ - string import_path = 1; - string package = 2; - string type_name = 3; - bool basic_type = 4; - map struct_tags = 5; + string version = 1 [json_name = "version"]; + string engine = 2 [json_name = "engine"]; + repeated string schema = 3 [json_name = "schema"]; + repeated string queries = 4 [json_name = "queries"]; + Codegen codegen = 12 [json_name = "codegen"]; } -message Settings -{ - string version = 1 [json_name="version"]; - string engine = 2 [json_name="engine"]; - repeated string schema = 3 [json_name="schema"]; - repeated string queries = 4 [json_name="queries"]; - map rename = 5 [json_name="rename"]; - repeated Override overrides = 6 [json_name="overrides"]; - Codegen codegen = 12 [json_name="codegen"]; - - // TODO: Refactor codegen settings - PythonCode python = 8; - KotlinCode kotlin = 9; - GoCode go = 10; - JSONCode json = 11; +message Codegen { + message Process { + string cmd = 1; + } + message WASM { + string url = 1; + string sha256 = 2; + } + string out = 1 [json_name = "out"]; + string plugin = 2 [json_name = "plugin"]; + bytes options = 3 [json_name = "options"]; + repeated string env = 4 [json_name = "env"]; + Process process = 5 [json_name = "process"]; + WASM wasm = 6 [json_name = "wasm"]; } -message Codegen -{ - string out = 1 [json_name="out"]; - string plugin = 2 [json_name="plugin"]; - bytes options = 3 [json_name="options"]; -} - -message PythonCode -{ - bool emit_exact_table_names = 1; - bool emit_sync_querier = 2; - bool emit_async_querier = 3; - string package = 4; - string out = 5; - bool emit_pydantic_models = 6; -} - -message KotlinCode -{ - bool emit_exact_table_names = 1; - string package = 2; - string out = 3; -} - -message GoCode -{ - bool emit_interface = 1; - bool emit_json_tags = 2; - bool emit_db_tags = 3; - bool emit_prepared_queries = 4; - bool emit_exact_table_names = 5; - bool emit_empty_slices = 6; - bool emit_exported_queries = 7; - bool emit_result_struct_pointers = 8; - bool emit_params_struct_pointers = 9; - bool emit_methods_with_db_argument = 10; - string json_tags_case_style = 11; - string package = 12; - string out = 13; - string sql_package = 14; - string output_db_file_name = 15; - string output_models_file_name = 16; - string output_querier_file_name = 17; - string output_files_suffix = 18; - bool emit_enum_valid_method = 19; - bool emit_all_enum_values = 20; -} - -message JSONCode -{ - string out = 1; - string indent = 2; - string filename = 3; -} - -message Catalog -{ +message Catalog { string comment = 1; string default_schema = 2; string name = 3; repeated Schema schemas = 4; } -message Schema -{ +message Schema { string comment = 1; string name = 2; repeated Table tables = 3; @@ -137,35 +58,30 @@ message Schema repeated CompositeType composite_types = 5; } -message CompositeType -{ +message CompositeType { string name = 1; string comment = 2; } -message Enum -{ +message Enum { string name = 1; repeated string vals = 2; string comment = 3; } -message Table -{ +message Table { Identifier rel = 1; repeated Column columns = 2; - string comment = 3; + string comment = 3; } -message Identifier -{ +message Identifier { string catalog = 1; string schema = 2; string name = 3; } -message Column -{ +message Column { string name = 1; bool not_null = 3; bool is_array = 4; @@ -179,35 +95,38 @@ message Column Identifier table = 10; string table_alias = 11; Identifier type = 12; + bool is_sqlc_slice = 13; + Identifier embed_table = 14; + string original_name = 15; + bool unsigned = 16; + int32 array_dims = 17; } -message Query -{ - string text = 1 [json_name="text"]; - string name = 2 [json_name="name"]; - string cmd = 3 [json_name="cmd"]; - repeated Column columns = 4 [json_name="columns"]; - repeated Parameter params = 5 [json_name="parameters"]; - repeated string comments = 6 [json_name="comments"]; - string filename = 7 [json_name="filename"]; - Identifier insert_into_table = 8 [json_name="insert_into_table"]; +message Query { + string text = 1 [json_name = "text"]; + string name = 2 [json_name = "name"]; + string cmd = 3 [json_name = "cmd"]; + repeated Column columns = 4 [json_name = "columns"]; + repeated Parameter params = 5 [json_name = "parameters"]; + repeated string comments = 6 [json_name = "comments"]; + string filename = 7 [json_name = "filename"]; + Identifier insert_into_table = 8 [json_name = "insert_into_table"]; } -message Parameter -{ - int32 number = 1 [json_name="number"]; - Column column = 2 [json_name="column"]; +message Parameter { + int32 number = 1 [json_name = "number"]; + Column column = 2 [json_name = "column"]; } -message CodeGenRequest -{ - Settings settings = 1 [json_name="settings"]; - Catalog catalog = 2 [json_name="catalog"]; - repeated Query queries = 3 [json_name="queries"]; - string sqlc_version = 4 [json_name="sqlc_version"]; +message GenerateRequest { + Settings settings = 1 [json_name = "settings"]; + Catalog catalog = 2 [json_name = "catalog"]; + repeated Query queries = 3 [json_name = "queries"]; + string sqlc_version = 4 [json_name = "sqlc_version"]; + bytes plugin_options = 5 [json_name = "plugin_options"]; + bytes global_options = 6 [json_name = "global_options"]; } -message CodeGenResponse -{ - repeated File files = 1 [json_name="files"]; +message GenerateResponse { + repeated File files = 1 [json_name = "files"]; } diff --git a/sqlc-gen/src/codegen/mod.rs b/sqlc-gen/src/codegen/mod.rs index 6d65c2a..46d6471 100644 --- a/sqlc-gen/src/codegen/mod.rs +++ b/sqlc-gen/src/codegen/mod.rs @@ -5,6 +5,7 @@ use itertools::Itertools; use proc_macro2::{Punct, Spacing, TokenStream}; use quote::{format_ident, quote, ToTokens}; use serde::{Deserialize, Serialize}; +use sqlc_sqlc_community_neoeinstein_prost::plugin; use std::fmt; use std::hash::Hash; use std::str::FromStr; @@ -19,9 +20,8 @@ mod type_enum; mod type_query; mod type_struct; -pub mod plugin { - include!(concat!(env!("OUT_DIR"), "/plugin.rs")); -} +// #[path = "./plugin.rs"] +// pub mod plugin; pub fn get_ident(value: &str) -> Ident { format_ident!("{}", value) @@ -264,12 +264,12 @@ impl Options { #[derive(Default)] pub struct CodeBuilder { - req: plugin::CodeGenRequest, + req: plugin::GenerateRequest, options: Options, } impl CodeBuilder { - pub fn new(req: plugin::CodeGenRequest) -> Self { + pub fn new(req: plugin::GenerateRequest) -> Self { let settings = req.settings.clone().expect("could not find sqlc config"); let codegen = settings .codegen diff --git a/sqlc-gen/src/codegen/plugin.rs b/sqlc-gen/src/codegen/plugin.rs new file mode 100644 index 0000000..3d0a972 --- /dev/null +++ b/sqlc-gen/src/codegen/plugin.rs @@ -0,0 +1,209 @@ +// @generated +// This file is @generated by prost-build. +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct File { + #[prost(string, tag="1")] + pub name: ::prost::alloc::string::String, + #[prost(bytes="vec", tag="2")] + pub contents: ::prost::alloc::vec::Vec, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Settings { + #[prost(string, tag="1")] + pub version: ::prost::alloc::string::String, + #[prost(string, tag="2")] + pub engine: ::prost::alloc::string::String, + #[prost(string, repeated, tag="3")] + pub schema: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, + #[prost(string, repeated, tag="4")] + pub queries: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, + #[prost(message, optional, tag="12")] + pub codegen: ::core::option::Option, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Codegen { + #[prost(string, tag="1")] + pub out: ::prost::alloc::string::String, + #[prost(string, tag="2")] + pub plugin: ::prost::alloc::string::String, + #[prost(bytes="vec", tag="3")] + pub options: ::prost::alloc::vec::Vec, + #[prost(string, repeated, tag="4")] + pub env: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, + #[prost(message, optional, tag="5")] + pub process: ::core::option::Option, + #[prost(message, optional, tag="6")] + pub wasm: ::core::option::Option, +} +/// Nested message and enum types in `Codegen`. +pub mod codegen { + #[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] + pub struct Process { + #[prost(string, tag="1")] + pub cmd: ::prost::alloc::string::String, + } + #[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] + pub struct Wasm { + #[prost(string, tag="1")] + pub url: ::prost::alloc::string::String, + #[prost(string, tag="2")] + pub sha256: ::prost::alloc::string::String, + } +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Catalog { + #[prost(string, tag="1")] + pub comment: ::prost::alloc::string::String, + #[prost(string, tag="2")] + pub default_schema: ::prost::alloc::string::String, + #[prost(string, tag="3")] + pub name: ::prost::alloc::string::String, + #[prost(message, repeated, tag="4")] + pub schemas: ::prost::alloc::vec::Vec, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Schema { + #[prost(string, tag="1")] + pub comment: ::prost::alloc::string::String, + #[prost(string, tag="2")] + pub name: ::prost::alloc::string::String, + #[prost(message, repeated, tag="3")] + pub tables: ::prost::alloc::vec::Vec, + #[prost(message, repeated, tag="4")] + pub enums: ::prost::alloc::vec::Vec, + #[prost(message, repeated, tag="5")] + pub composite_types: ::prost::alloc::vec::Vec, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct CompositeType { + #[prost(string, tag="1")] + pub name: ::prost::alloc::string::String, + #[prost(string, tag="2")] + pub comment: ::prost::alloc::string::String, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Enum { + #[prost(string, tag="1")] + pub name: ::prost::alloc::string::String, + #[prost(string, repeated, tag="2")] + pub vals: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, + #[prost(string, tag="3")] + pub comment: ::prost::alloc::string::String, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Table { + #[prost(message, optional, tag="1")] + pub rel: ::core::option::Option, + #[prost(message, repeated, tag="2")] + pub columns: ::prost::alloc::vec::Vec, + #[prost(string, tag="3")] + pub comment: ::prost::alloc::string::String, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Identifier { + #[prost(string, tag="1")] + pub catalog: ::prost::alloc::string::String, + #[prost(string, tag="2")] + pub schema: ::prost::alloc::string::String, + #[prost(string, tag="3")] + pub name: ::prost::alloc::string::String, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Column { + #[prost(string, tag="1")] + pub name: ::prost::alloc::string::String, + #[prost(bool, tag="3")] + pub not_null: bool, + #[prost(bool, tag="4")] + pub is_array: bool, + #[prost(string, tag="5")] + pub comment: ::prost::alloc::string::String, + #[prost(int32, tag="6")] + pub length: i32, + #[prost(bool, tag="7")] + pub is_named_param: bool, + #[prost(bool, tag="8")] + pub is_func_call: bool, + /// XXX: Figure out what PostgreSQL calls `foo.id` + #[prost(string, tag="9")] + pub scope: ::prost::alloc::string::String, + #[prost(message, optional, tag="10")] + pub table: ::core::option::Option, + #[prost(string, tag="11")] + pub table_alias: ::prost::alloc::string::String, + #[prost(message, optional, tag="12")] + pub r#type: ::core::option::Option, + #[prost(bool, tag="13")] + pub is_sqlc_slice: bool, + #[prost(message, optional, tag="14")] + pub embed_table: ::core::option::Option, + #[prost(string, tag="15")] + pub original_name: ::prost::alloc::string::String, + #[prost(bool, tag="16")] + pub unsigned: bool, + #[prost(int32, tag="17")] + pub array_dims: i32, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Query { + #[prost(string, tag="1")] + pub text: ::prost::alloc::string::String, + #[prost(string, tag="2")] + pub name: ::prost::alloc::string::String, + #[prost(string, tag="3")] + pub cmd: ::prost::alloc::string::String, + #[prost(message, repeated, tag="4")] + pub columns: ::prost::alloc::vec::Vec, + #[prost(message, repeated, tag="5")] + pub params: ::prost::alloc::vec::Vec, + #[prost(string, repeated, tag="6")] + pub comments: ::prost::alloc::vec::Vec<::prost::alloc::string::String>, + #[prost(string, tag="7")] + pub filename: ::prost::alloc::string::String, + #[prost(message, optional, tag="8")] + pub insert_into_table: ::core::option::Option, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct Parameter { + #[prost(int32, tag="1")] + pub number: i32, + #[prost(message, optional, tag="2")] + pub column: ::core::option::Option, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct GenerateRequest { + #[prost(message, optional, tag="1")] + pub settings: ::core::option::Option, + #[prost(message, optional, tag="2")] + pub catalog: ::core::option::Option, + #[prost(message, repeated, tag="3")] + pub queries: ::prost::alloc::vec::Vec, + #[prost(string, tag="4")] + pub sqlc_version: ::prost::alloc::string::String, + #[prost(bytes="vec", tag="5")] + pub plugin_options: ::prost::alloc::vec::Vec, + #[prost(bytes="vec", tag="6")] + pub global_options: ::prost::alloc::vec::Vec, +} +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct GenerateResponse { + #[prost(message, repeated, tag="1")] + pub files: ::prost::alloc::vec::Vec, +} +// @@protoc_insertion_point(module) diff --git a/sqlc-gen/src/main.rs b/sqlc-gen/src/main.rs index 4b6c636..5bc7e07 100644 --- a/sqlc-gen/src/main.rs +++ b/sqlc-gen/src/main.rs @@ -1,36 +1,31 @@ -use codegen::plugin; use prost::Message; +use sqlc_sqlc_community_neoeinstein_prost::plugin; use std::io; use std::io::prelude::*; -use std::io::Cursor; pub mod codegen; pub fn deserialize_codegen_request( buf: &[u8], -) -> Result { - plugin::CodeGenRequest::decode(&mut Cursor::new(buf)) +) -> Result { + plugin::GenerateRequest::decode(buf) } -pub fn serialize_codegen_response(resp: &plugin::CodeGenResponse) -> Vec { - let mut buf = Vec::new(); - buf.reserve(resp.encoded_len()); - - resp.encode(&mut buf).unwrap(); - buf +pub fn serialize_codegen_response(resp: &plugin::GenerateResponse) -> Vec { + resp.encode_to_vec() } -pub fn create_codegen_response(content: &str) -> plugin::CodeGenResponse { +pub fn create_codegen_response(content: &str) -> plugin::GenerateResponse { let mut file = plugin::File::default(); file.name = "gen.rs".to_string(); - file.contents = content.as_bytes().to_vec(); + file.contents = content.as_bytes().to_vec().into(); - let mut resp = plugin::CodeGenResponse::default(); + let mut resp = plugin::GenerateResponse::default(); resp.files.push(file); resp } -pub fn generate_rust_code(req: plugin::CodeGenRequest) -> String { +pub fn generate_rust_code(req: plugin::GenerateRequest) -> String { // println!("{:?}", req); let tokens = codegen::CodeBuilder::new(req).generate_code(); let syntax_tree = syn::parse_file(tokens.to_string().as_str()).unwrap(); @@ -41,22 +36,22 @@ pub fn generate_rust_code(req: plugin::CodeGenRequest) -> String { fn main() -> Result<(), prost::DecodeError> { let stdin = io::stdin(); let mut stdin = stdin.lock(); + let mut buffer: Vec = Vec::new(); + stdin.read_to_end(&mut buffer).unwrap(); + let buffer = stdin.fill_buf().unwrap(); + // panic!("{:?}", buffer.len()); - let req = match deserialize_codegen_request(&buffer) { - Ok(request_deserialized_result) => request_deserialized_result, - Err(_e) => std::process::exit(1), - }; + let req = deserialize_codegen_request(&buffer)?; let out = generate_rust_code(req); let resp = create_codegen_response(&out); let out = serialize_codegen_response(&resp); - let _ = match io::stdout().write_all(&out) { - Ok(result) => result, - Err(_e) => std::process::exit(1), - }; + let _result = io::stdout() + .write_all(&out) + .expect("failed to write generated code"); Ok(()) }