From cf0248f0f34a7c884ce9f2045cd9878e92daec17 Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Sun, 22 Oct 2023 00:50:17 +0200 Subject: [PATCH] move scope_enum to own mako file --- .../templates/api/api/utilities.rs.mako | 3 +- src/generator/templates/api/lib/enum.mako | 67 +++++++++++++++++++ src/generator/templates/api/lib/lib.mako | 50 -------------- 3 files changed, 69 insertions(+), 51 deletions(-) create mode 100644 src/generator/templates/api/lib/enum.mako diff --git a/src/generator/templates/api/api/utilities.rs.mako b/src/generator/templates/api/api/utilities.rs.mako index 375021a026..a0aad935d1 100644 --- a/src/generator/templates/api/api/utilities.rs.mako +++ b/src/generator/templates/api/api/utilities.rs.mako @@ -1,4 +1,5 @@ <%namespace name="lib" file="../lib/lib.mako"/>\ +<%namespace name="enum" file="../lib/enum.mako"/>\ <%namespace name="util" file="../../../lib/util.mako"/>\ <%namespace name="rbuild" file="../lib/rbuild.mako"/>\ <%namespace name="mbuild" file="../lib/mbuild.mako"/>\ @@ -13,4 +14,4 @@ default_user_agent = "google-api-rust-client/" + cargo.build_version %>\ use super::*; -${lib.scope_enum()} +${enum.scope_enum()} diff --git a/src/generator/templates/api/lib/enum.mako b/src/generator/templates/api/lib/enum.mako new file mode 100644 index 0000000000..7d9f8cd843 --- /dev/null +++ b/src/generator/templates/api/lib/enum.mako @@ -0,0 +1,67 @@ +<%! + from generator.lib.util import (activity_split, put_and, md_italic, split_camelcase_s, canonical_type_name, hub_type, + rust_test_fn_invisible, rust_doc_test_norun, rust_doc_comment, markdown_rust_block, + unindent_first_by, mangle_ident, mb_type, singular, scope_url_to_variant, + PART_MARKER_TRAIT, RESOURCE_MARKER_TRAIT, CALL_BUILDER_MARKERT_TRAIT, + find_fattest_resource, build_all_params, pass_through, parts_from_params, + REQUEST_MARKER_TRAIT, RESPONSE_MARKER_TRAIT, supports_scopes, to_api_version, + to_fqan, METHODS_RESOURCE, ADD_PARAM_MEDIA_EXAMPLE, PROTOCOL_TYPE_INFO, enclose_in, + upload_action_fn, METHODS_BUILDER_MARKER_TRAIT, DELEGATE_TYPE, + to_extern_crate_name, rust_doc_sanitize) + + def pretty_name(name): + return ' '.join(split_camelcase_s(name).split('.')) +%>\ +<%namespace name="util" file="../../../lib/util.mako"/>\ +<%namespace name="mbuild" file="mbuild.mako"/>\ + + +## Builds the scope-enum for the API +## It's possible there is no scope enum if there is no auth information +############################################################################################### +############################################################################################### +<%def name="scope_enum()">\ +% if not supports_scopes(auth): +<% return '' %>\ +% endif +/// Identifies the an OAuth2 authorization scope. +/// A scope is needed when requesting an +/// [authorization token](https://developers.google.com/youtube/v3/guides/authentication). +#[derive(PartialEq, Eq, Hash, Debug, Clone)] +pub enum Scope { +% for url, scope in auth.oauth2.scopes.items(): + ${scope.description | rust_doc_sanitize(documentationLink), rust_doc_comment} + ${scope_url_to_variant(name, url, fully_qualified=False)}, + % if not loop.last: + + % endif +% endfor +} + +impl AsRef for Scope { + fn as_ref(&self) -> &str { + match *self { + % for url in auth.oauth2.scopes.keys(): + ${scope_url_to_variant(name, url)} => "${url}", + % endfor + } + } +} + +impl Default for Scope { + fn default() -> Scope { +<% + default_url = None + shortest_url = None + for url in auth.oauth2.scopes.keys(): + if not default_url and 'readonly' in url: + default_url = url + if not shortest_url or len(shortest_url) > len(url): + shortest_url = url + # end for each url + default_url = default_url or shortest_url +%>\ + ${scope_url_to_variant(name, default_url)} + } +} + \ No newline at end of file diff --git a/src/generator/templates/api/lib/lib.mako b/src/generator/templates/api/lib/lib.mako index bc0374f3f9..1dd536f598 100644 --- a/src/generator/templates/api/lib/lib.mako +++ b/src/generator/templates/api/lib/lib.mako @@ -319,53 +319,3 @@ You can read the full text at the repository's [license file][repo-license]. [repo-license]: ${cargo.repo_base_url + 'blob/main/LICENSE.md'} - -## Builds the scope-enum for the API -## It's possible there is no scope enum if there is no auth information -############################################################################################### -############################################################################################### -<%def name="scope_enum()">\ -% if not supports_scopes(auth): -<% return '' %>\ -% endif -/// Identifies the an OAuth2 authorization scope. -/// A scope is needed when requesting an -/// [authorization token](https://developers.google.com/youtube/v3/guides/authentication). -#[derive(PartialEq, Eq, Ord, PartialOrd, Hash, Debug, Clone, Copy)] -pub enum Scope { -% for url, scope in auth.oauth2.scopes.items(): - ${scope.description | rust_doc_sanitize(documentationLink), rust_doc_comment} - ${scope_url_to_variant(name, url, fully_qualified=False)}, - % if not loop.last: - - % endif -% endfor -} - -impl AsRef for Scope { - fn as_ref(&self) -> &str { - match *self { - % for url in auth.oauth2.scopes.keys(): - ${scope_url_to_variant(name, url)} => "${url}", - % endfor - } - } -} - -impl Default for Scope { - fn default() -> Scope { -<% - default_url = None - shortest_url = None - for url in auth.oauth2.scopes.keys(): - if not default_url and 'readonly' in url: - default_url = url - if not shortest_url or len(shortest_url) > len(url): - shortest_url = url - # end for each url - default_url = default_url or shortest_url -%>\ - ${scope_url_to_variant(name, default_url)} - } -} -