From 5b2da0380bf8c56fdaf7ddb24e583d34c6422506 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Sat, 21 Oct 2023 23:50:09 +0200 Subject: [PATCH] restructure templates to use modules, so it's not just one big file --- etc/api/type-api.yaml | 12 ++- .../templates/api/api/call_builders.rs.mako | 24 ++++++ .../api/{api.rs.mako => api/hub.rs.mako} | 79 +++---------------- .../templates/api/api/method_builders.rs.mako | 23 ++++++ src/generator/templates/api/api/mod.rs.mako | 51 ++++++++++++ .../templates/api/api/schemas.rs.mako | 23 ++++++ .../templates/api/api/utilities.rs.mako | 19 +++++ src/generator/templates/api/lib/mbuild.mako | 8 +- src/generator/templates/api/lib/rbuild.mako | 2 +- 9 files changed, 165 insertions(+), 76 deletions(-) create mode 100644 src/generator/templates/api/api/call_builders.rs.mako rename src/generator/templates/api/{api.rs.mako => api/hub.rs.mako} (63%) create mode 100644 src/generator/templates/api/api/method_builders.rs.mako create mode 100644 src/generator/templates/api/api/mod.rs.mako create mode 100644 src/generator/templates/api/api/schemas.rs.mako create mode 100644 src/generator/templates/api/api/utilities.rs.mako diff --git a/etc/api/type-api.yaml b/etc/api/type-api.yaml index 6f77c502fe..8f68b9a6fb 100644 --- a/etc/api/type-api.yaml +++ b/etc/api/type-api.yaml @@ -20,7 +20,17 @@ make: - source: ../Cargo.toml - source: lib.rs output_dir: src - - source: api.rs + - source: api/mod.rs + output_dir: src + - source: api/utilities.rs + output_dir: src + - source: api/hub.rs + output_dir: src + - source: api/method_builders.rs + output_dir: src + - source: api/schemas.rs + output_dir: src + - source: api/call_builders.rs output_dir: src cargo: keywords: [protocol, web, api] diff --git a/src/generator/templates/api/api/call_builders.rs.mako b/src/generator/templates/api/api/call_builders.rs.mako new file mode 100644 index 0000000000..b54582401c --- /dev/null +++ b/src/generator/templates/api/api/call_builders.rs.mako @@ -0,0 +1,24 @@ +<%namespace name="lib" file="../lib/lib.mako"/>\ +<%namespace name="util" file="../../../lib/util.mako"/>\ +<%namespace name="rbuild" file="../lib/rbuild.mako"/>\ +<%namespace name="mbuild" file="../lib/mbuild.mako"/>\ +<%namespace name="schema" file="../lib/schema.mako"/>\ +<% + from generator.lib.util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment, + rb_type, hub_type, mangle_ident, hub_type_params_s, + rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE, + UNUSED_TYPE_MARKER, schema_markers) + + c = new_context(schemas, resources) + hub_type = hub_type(c.schemas, util.canonical_name()) + ht_params = hub_type_params_s() + + default_user_agent = "google-api-rust-client/" + cargo.build_version +%>\ +use super::*; +% for resource, methods in c.rta_map.items(): +% for method in methods: +${mbuild.new(resource, method, c)} + +% endfor ## method in methods +% endfor ## resource, methods diff --git a/src/generator/templates/api/api.rs.mako b/src/generator/templates/api/api/hub.rs.mako similarity index 63% rename from src/generator/templates/api/api.rs.mako rename to src/generator/templates/api/api/hub.rs.mako index 7029c36241..ccf63dccb9 100644 --- a/src/generator/templates/api/api.rs.mako +++ b/src/generator/templates/api/api/hub.rs.mako @@ -1,8 +1,8 @@ -<%namespace name="lib" file="lib/lib.mako"/>\ -<%namespace name="util" file="../../lib/util.mako"/>\ -<%namespace name="rbuild" file="lib/rbuild.mako"/>\ -<%namespace name="mbuild" file="lib/mbuild.mako"/>\ -<%namespace name="schema" file="lib/schema.mako"/>\ +<%namespace name="lib" file="../lib/lib.mako"/>\ +<%namespace name="util" file="../../../lib/util.mako"/>\ +<%namespace name="rbuild" file="../lib/rbuild.mako"/>\ +<%namespace name="mbuild" file="../lib/mbuild.mako"/>\ +<%namespace name="schema" file="../lib/schema.mako"/>\ <% from generator.lib.util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment, rb_type, hub_type, mangle_ident, hub_type_params_s, @@ -15,34 +15,7 @@ default_user_agent = "google-api-rust-client/" + cargo.build_version %>\ -use std::collections::HashMap; -use std::cell::RefCell; -use std::default::Default; -use std::collections::BTreeSet; -use std::error::Error as StdError; -use serde_json as json; -use std::io; -use std::fs; -use std::mem; - -use hyper::client::connect; -use tokio::io::{AsyncRead, AsyncWrite}; -use tokio::time::sleep; -use tower_service; -use serde::{Serialize, Deserialize}; - -use crate::{client, client::GetToken, client::serde_with}; - -// ############## -// UTILITIES ### -// ############ - -${lib.scope_enum()} - - -// ######## -// HUB ### -// ###### +use super::*; /// Central instance to access all ${hub_type} related resource activities /// @@ -57,9 +30,9 @@ ${lib.hub_usage_example(c)}\ pub struct ${hub_type}${ht_params} { pub client: hyper::Client, pub auth: Box, - _user_agent: String, - _base_url: String, - _root_url: String, + pub(super) _user_agent: String, + pub(super) _base_url: String, + pub(super) _root_url: String, } impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> client::Hub for ${hub_type}${ht_params} {} @@ -106,37 +79,3 @@ impl<'a, ${', '.join(HUB_TYPE_PARAMETERS)}> ${hub_type}${ht_params} { mem::replace(&mut self._root_url, new_root_url) } } - - -% if c.schemas: -// ############ -// SCHEMAS ### -// ########## -% for s in c.schemas.values(): -% if UNUSED_TYPE_MARKER not in schema_markers(s, c, transitive=True): -${schema.new(s, c)} -% endif -% endfor -% endif - -// ################### -// MethodBuilders ### -// ################# - -% for resource in c.rta_map: -${rbuild.new(resource, c)} - - -% endfor - - -// ################### -// CallBuilders ### -// ################# - -% for resource, methods in c.rta_map.items(): -% for method in methods: -${mbuild.new(resource, method, c)} - -% endfor ## method in methods -% endfor ## resource, methods diff --git a/src/generator/templates/api/api/method_builders.rs.mako b/src/generator/templates/api/api/method_builders.rs.mako new file mode 100644 index 0000000000..e029589f6e --- /dev/null +++ b/src/generator/templates/api/api/method_builders.rs.mako @@ -0,0 +1,23 @@ +<%namespace name="lib" file="../lib/lib.mako"/>\ +<%namespace name="util" file="../../../lib/util.mako"/>\ +<%namespace name="rbuild" file="../lib/rbuild.mako"/>\ +<%namespace name="mbuild" file="../lib/mbuild.mako"/>\ +<%namespace name="schema" file="../lib/schema.mako"/>\ +<% + from generator.lib.util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment, + rb_type, hub_type, mangle_ident, hub_type_params_s, + rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE, + UNUSED_TYPE_MARKER, schema_markers) + + c = new_context(schemas, resources) + hub_type = hub_type(c.schemas, util.canonical_name()) + ht_params = hub_type_params_s() + + default_user_agent = "google-api-rust-client/" + cargo.build_version +%>\ +use super::*; +% for resource in c.rta_map: +${rbuild.new(resource, c)} + + +% endfor diff --git a/src/generator/templates/api/api/mod.rs.mako b/src/generator/templates/api/api/mod.rs.mako new file mode 100644 index 0000000000..e657247b69 --- /dev/null +++ b/src/generator/templates/api/api/mod.rs.mako @@ -0,0 +1,51 @@ +<%namespace name="lib" file="../lib/lib.mako"/>\ +<%namespace name="util" file="../../../lib/util.mako"/>\ +<%namespace name="rbuild" file="../lib/rbuild.mako"/>\ +<%namespace name="mbuild" file="../lib/mbuild.mako"/>\ +<%namespace name="schema" file="../lib/schema.mako"/>\ +<% + from generator.lib.util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment, + rb_type, hub_type, mangle_ident, hub_type_params_s, + rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE, + UNUSED_TYPE_MARKER, schema_markers) + + c = new_context(schemas, resources) + hub_type = hub_type(c.schemas, util.canonical_name()) + ht_params = hub_type_params_s() + + default_user_agent = "google-api-rust-client/" + cargo.build_version +%>\ +use std::collections::HashMap; +use std::cell::RefCell; +use std::default::Default; +use std::collections::BTreeSet; +use std::error::Error as StdError; +use serde_json as json; +use std::io; +use std::fs; +use std::mem; + +use hyper::client::connect; +use tokio::io::{AsyncRead, AsyncWrite}; +use tokio::time::sleep; +use tower_service; +use serde::{Serialize, Deserialize}; + +use crate::{client, client::GetToken, client::serde_with}; + +mod utilities; +pub use utilities::*; + +mod hub; +pub use hub::*; + +% if c.schemas: +mod schemas; +pub use schemas::*; +% endif + +mod method_builders; +pub use method_builders::*; + +mod call_builders; +pub use call_builders::*; diff --git a/src/generator/templates/api/api/schemas.rs.mako b/src/generator/templates/api/api/schemas.rs.mako new file mode 100644 index 0000000000..60f6359d98 --- /dev/null +++ b/src/generator/templates/api/api/schemas.rs.mako @@ -0,0 +1,23 @@ +<%namespace name="lib" file="../lib/lib.mako"/>\ +<%namespace name="util" file="../../../lib/util.mako"/>\ +<%namespace name="rbuild" file="../lib/rbuild.mako"/>\ +<%namespace name="mbuild" file="../lib/mbuild.mako"/>\ +<%namespace name="schema" file="../lib/schema.mako"/>\ +<% + from generator.lib.util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment, + rb_type, hub_type, mangle_ident, hub_type_params_s, + rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE, + UNUSED_TYPE_MARKER, schema_markers) + + c = new_context(schemas, resources) + hub_type = hub_type(c.schemas, util.canonical_name()) + ht_params = hub_type_params_s() + + default_user_agent = "google-api-rust-client/" + cargo.build_version +%>\ +use super::*; +% for s in c.schemas.values(): +% if UNUSED_TYPE_MARKER not in schema_markers(s, c, transitive=True): +${schema.new(s, c)} +% endif +% endfor diff --git a/src/generator/templates/api/api/utilities.rs.mako b/src/generator/templates/api/api/utilities.rs.mako new file mode 100644 index 0000000000..e6376a10ee --- /dev/null +++ b/src/generator/templates/api/api/utilities.rs.mako @@ -0,0 +1,19 @@ +<%namespace name="lib" file="../lib/lib.mako"/>\ +<%namespace name="util" file="../../../lib/util.mako"/>\ +<%namespace name="rbuild" file="../lib/rbuild.mako"/>\ +<%namespace name="mbuild" file="../lib/mbuild.mako"/>\ +<%namespace name="schema" file="../lib/schema.mako"/>\ +<% + from generator.lib.util import (new_context, rust_comment, rust_doc_comment, rust_module_doc_comment, + rb_type, hub_type, mangle_ident, hub_type_params_s, + rb_type_params_s, find_fattest_resource, HUB_TYPE_PARAMETERS, METHODS_RESOURCE, + UNUSED_TYPE_MARKER, schema_markers) + + c = new_context(schemas, resources) + hub_type = hub_type(c.schemas, util.canonical_name()) + ht_params = hub_type_params_s() + + default_user_agent = "google-api-rust-client/" + cargo.build_version +%>\ +use super::*; +${lib.scope_enum()} diff --git a/src/generator/templates/api/lib/mbuild.mako b/src/generator/templates/api/lib/mbuild.mako index 23e3c8d4db..7b943b0c90 100644 --- a/src/generator/templates/api/lib/mbuild.mako +++ b/src/generator/templates/api/lib/mbuild.mako @@ -120,10 +120,10 @@ ${self.usage(resource, method, m, params, request_value, parts)}\ pub struct ${ThisType} where ${struct_type_bounds_s()} { - hub: &'a ${hub_type_name}${hub_type_params_s()}, + pub(super) hub: &'a ${hub_type_name}${hub_type_params_s()}, ## PROPERTIES ############### % for p in params: - ${property(p.name)}:\ + pub(super) ${property(p.name)}:\ % if is_required_property(p): ${activity_rust_type(schemas, p, allow_optionals=False)}, % else: @@ -131,10 +131,10 @@ pub struct ${ThisType} % endif % endfor ## A generic map for additinal parameters. Sometimes you can set some that are documented online only - ${api.properties.params}: HashMap, + pub(super) ${api.properties.params}: HashMap, % if method_default_scope(m): ## We need the scopes sorted, to not unnecessarily query new tokens - ${api.properties.scopes}: BTreeSet + pub(super) ${api.properties.scopes}: BTreeSet % endif } diff --git a/src/generator/templates/api/lib/rbuild.mako b/src/generator/templates/api/lib/rbuild.mako index bd13253981..6024858b03 100644 --- a/src/generator/templates/api/lib/rbuild.mako +++ b/src/generator/templates/api/lib/rbuild.mako @@ -46,7 +46,7 @@ let rb = hub.${mangle_ident(resource)}(); pub struct ${ThisType} where ${struct_type_bounds_s()} { - hub: &'a ${hub_type_name}${hub_type_params_s()}, + pub(super) hub: &'a ${hub_type_name}${hub_type_params_s()}, } impl${rb_params} ${METHODS_BUILDER_MARKER_TRAIT} for ${ThisType} {}