From f1b99af5dcca4e169463a8932fcf217f9cace8c6 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 4 Mar 2015 15:58:53 +0100 Subject: [PATCH] refactor(lib): move resource builder into own lib As the code is likely to grow far more complex, it's required to move it as long as it is easy to do so. --- gen/youtube3/src/lib.rs | 90 ++++++++++++++++++++++++++++++++++++++++ src/mako/lib.rs.mako | 46 +++++++------------- src/mako/lib/rbuild.mako | 40 ++++++++++++++++++ src/mako/lib/util.py | 3 ++ 4 files changed, 148 insertions(+), 31 deletions(-) create mode 100644 src/mako/lib/rbuild.mako diff --git a/gen/youtube3/src/lib.rs b/gen/youtube3/src/lib.rs index 551d242c03..a55e9d0d85 100644 --- a/gen/youtube3/src/lib.rs +++ b/gen/youtube3/src/lib.rs @@ -3093,6 +3093,7 @@ impl Part for ChannelContentDetailsRelatedplaylists {} // MethodBuilders ### // ################# + /// A builder providing access to all methods supported on *i18nLanguage* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3137,6 +3138,10 @@ pub struct I18nLanguagesMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for I18nLanguagesMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *channelBanner* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3181,6 +3186,10 @@ pub struct ChannelBannersMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for ChannelBannersMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *channelSection* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3225,6 +3234,10 @@ pub struct ChannelSectionsMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for ChannelSectionsMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *guideCategorie* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3269,6 +3282,10 @@ pub struct GuideCategoriesMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for GuideCategoriesMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *playlist* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3313,6 +3330,10 @@ pub struct PlaylistsMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for PlaylistsMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *thumbnail* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3357,6 +3378,10 @@ pub struct ThumbnailsMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for ThumbnailsMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *video* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3401,6 +3426,10 @@ pub struct VideosMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for VideosMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *subscription* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3445,6 +3474,10 @@ pub struct SubscriptionsMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for SubscriptionsMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *search* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3489,6 +3522,10 @@ pub struct SearchMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for SearchMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *i18nRegion* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3533,6 +3570,10 @@ pub struct I18nRegionsMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for I18nRegionsMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *liveStream* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3577,6 +3618,10 @@ pub struct LiveStreamsMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for LiveStreamsMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *channel* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3621,6 +3666,10 @@ pub struct ChannelsMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for ChannelsMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *playlistItem* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3665,6 +3714,10 @@ pub struct PlaylistItemsMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for PlaylistItemsMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *watermark* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3709,6 +3762,10 @@ pub struct WatermarksMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for WatermarksMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *liveBroadcast* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3753,6 +3810,10 @@ pub struct LiveBroadcastsMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for LiveBroadcastsMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *videoCategorie* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3797,6 +3858,10 @@ pub struct VideoCategoriesMethodBuilder<'a, C, NC, A> } impl<'a, C, NC, A> MethodBuilder for VideoCategoriesMethodBuilder<'a, C, NC, A> {} + + + + /// A builder providing access to all methods supported on *activitie* resources. /// It is usually not used directly, but through the `YouTube` hub. /// @@ -3842,3 +3907,28 @@ pub struct ActivitiesMethodBuilder<'a, C, NC, A> impl<'a, C, NC, A> MethodBuilder for ActivitiesMethodBuilder<'a, C, NC, A> {} + + + + +// ################### +// CallBuilders ### +// ################# + + + + + + + + + + + + + + + + + + diff --git a/src/mako/lib.rs.mako b/src/mako/lib.rs.mako index d52015abd4..a14cf40f98 100644 --- a/src/mako/lib.rs.mako +++ b/src/mako/lib.rs.mako @@ -1,19 +1,19 @@ <% from util import (iter_nested_types, new_context, rust_comment, rust_doc_comment, - rust_module_doc_comment, rust_doc_test_norun, canonical_type_name, - mb_type, singular, rust_test_fn_invisible, put_and) + rust_module_doc_comment, mb_type, hub_type) nested_schemas = list(iter_nested_types(schemas)) c = new_context(resources) - hub_type = canonical_type_name(canonicalName) + hub_type = hub_type(canonicalName) %>\ <%namespace name="lib" file="lib/lib.mako"/>\ -<%namespace name="mutil" file="lib/util.mako"/>\ +<%namespace name="util" file="lib/util.mako"/>\ +<%namespace name="rbuild" file="lib/rbuild.mako"/>\ <%namespace name="schema" file="lib/schema.mako"/>\ <%block filter="rust_comment">\ -<%mutil:gen_info source="${self.uri}" />\ +<%util:gen_info source="${self.uri}" />\ <%block filter="rust_module_doc_comment">\ @@ -97,33 +97,17 @@ ${schema.new(s, c)} // MethodBuilders ### // ################# -% for resource, methods in c.rta_map.iteritems(): -/// A builder providing access to all methods supported on *${singular(resource)}* resources. -/// It is usually not used directly, but through the `${hub_type}` hub. -/// -/// # Example -/// -/// Instantiate a resource builder -/// -<%block filter="rust_doc_test_norun, rust_doc_comment">\ -${mutil.test_prelude()}\ +% for resource in c.rta_map: +${rbuild.new(resource, c)} -<%block filter="rust_test_fn_invisible">\ -${lib.test_hub(canonical_type_name(canonicalName))}\ -// Usually you wouldn't stick this into a variable, but keep calling `MethodBuilders` -// like ${put_and(sorted('`%s(...)`' % f for f in methods))} -let rb = hub.${resource}(); - - -pub struct ${mb_type(resource)}<'a, C, NC, A> - where NC: 'a, - C: 'a, - A: 'a, { - - hub: &'a ${hub_type} -} - -impl<'a, C, NC, A> MethodBuilder for ${mb_type(resource)}<'a, C, NC, A> {} % endfor + +// ################### +// CallBuilders ### +// ################# + +% for resource in c.rta_map: + +% endfor \ No newline at end of file diff --git a/src/mako/lib/rbuild.mako b/src/mako/lib/rbuild.mako new file mode 100644 index 0000000000..555b6fa390 --- /dev/null +++ b/src/mako/lib/rbuild.mako @@ -0,0 +1,40 @@ +<%! + from util import (put_and, rust_test_fn_invisible, rust_doc_test_norun, rust_doc_comment, + mb_type, singular, hub_type) +%>\ +<%namespace name="util" file="util.mako"/>\ +<%namespace name="lib" file="lib.mako"/>\ + +## Creates a Resource builder type +############################################################################################### +############################################################################################### +<%def name="new(resource, c)">\ +<% hub_type_name = hub_type(canonicalName) %> +/// A builder providing access to all methods supported on *${singular(resource)}* resources. +/// It is usually not used directly, but through the `${hub_type_name}` hub. +/// +/// # Example +/// +/// Instantiate a resource builder +/// +<%block filter="rust_doc_test_norun, rust_doc_comment">\ +${util.test_prelude()}\ + +<%block filter="rust_test_fn_invisible">\ +${lib.test_hub(hub_type_name)}\ + +// Usually you wouldn't stick this into a variable, but keep calling `MethodBuilders` +// like ${put_and(sorted('`%s(...)`' % f for f in c.rta_map[resource]))} +let rb = hub.${resource}(); + + +pub struct ${mb_type(resource)}<'a, C, NC, A> + where NC: 'a, + C: 'a, + A: 'a, { + + hub: &'a ${hub_type_name} +} + +impl<'a, C, NC, A> MethodBuilder for ${mb_type(resource)}<'a, C, NC, A> {} + \ No newline at end of file diff --git a/src/mako/lib/util.py b/src/mako/lib/util.py index beafb30f01..2d48ffbb3a 100644 --- a/src/mako/lib/util.py +++ b/src/mako/lib/util.py @@ -313,3 +313,6 @@ def library_name(name, version): # return type name of a resource builder, from a resource name def mb_type(r): return "%sMethodBuilder" % canonical_type_name(r) + +def hub_type(canonicalName): + return canonical_type_name(canonicalName)