Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 678b692

Browse files
committed
feat(delegate): first attempt to get it to work
With a big but ! The most simple thing to do it was to just add additional type parameters to the respective method. Now the type cannot be inferred, which means type-hints must be added. This should be easy enough, but ... has to be done somehow.
1 parent ac35432 commit 678b692

8 files changed

Lines changed: 1180 additions & 479 deletions

File tree

gen/youtube3/src/cmn.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use std::marker::MarkerTrait;
44
use std::io::{Read, Seek};
55
use std::borrow::BorrowMut;
66

7+
use oauth2;
8+
use hyper;
9+
710
/// Identifies the Hub. There is only one per library, this trait is supposed
811
/// to make intended use more explicit.
912
/// The hub allows to access all resource methods more easily.
@@ -43,4 +46,20 @@ impl<T: Seek + Read> ReadSeek for T {}
4346
struct JsonServerError {
4447
error: String,
4548
error_description: Option<String>
46-
}
49+
}
50+
51+
52+
/// A trait specifying functionality to help controlling any request performed by the API.
53+
/// The trait has a conservative default implementation.
54+
///
55+
/// It contains methods to deal with all common issues, as well with the ones related to
56+
/// uploading media
57+
pub trait Delegate: Clone {
58+
59+
/// Called whenever there is an HttpError, usually if there are network problems.
60+
///
61+
/// Return retry information.
62+
fn connection_error(&mut self, hyper::HttpError) -> oauth2::Retry {
63+
oauth2::Retry::Abort
64+
}
65+
}

gen/youtube3/src/lib.rs

Lines changed: 1094 additions & 461 deletions
Large diffs are not rendered by default.

src/mako/lib.rs.mako

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
<%namespace name="schema" file="lib/schema.mako"/>\
66
<%
77
from util import (iter_nested_types, new_context, rust_comment, rust_doc_comment,
8-
rust_module_doc_comment, rb_type, hub_type, mangle_ident, hub_type_params_s)
8+
rust_module_doc_comment, rb_type, hub_type, mangle_ident, hub_type_params_s,
9+
hub_type_bounds, rb_type_params_s)
910
1011
nested_schemas = list(iter_nested_types(schemas))
1112
c = new_context(resources)
@@ -36,7 +37,7 @@ use std::default::Default;
3637
use std::io::{Read, Seek};
3738
use std::fs;
3839

39-
pub use cmn::{Hub, ReadSeek, ResourceMethodsBuilder, MethodBuilder, Resource, Part, ResponseResult, RequestValue, NestedType};
40+
pub use cmn::{Hub, ReadSeek, ResourceMethodsBuilder, MethodBuilder, Resource, Part, ResponseResult, RequestValue, NestedType, Delegate};
4041

4142

4243
// ##############
@@ -80,9 +81,7 @@ pub struct ${hub_type}${ht_params} {
8081
impl<'a, C, NC, A> Hub for ${hub_type}${ht_params} {}
8182

8283
impl<'a, C, NC, A> ${hub_type}${ht_params}
83-
where NC: hyper::net::NetworkConnector,
84-
C: BorrowMut<hyper::Client<NC>> + 'a,
85-
A: oauth2::GetToken {
84+
where ${', '.join(hub_type_bounds())} {
8685

8786
pub fn new(client: C, authenticator: A) -> ${hub_type}${ht_params} {
8887
${hub_type} {
@@ -93,7 +92,7 @@ impl<'a, C, NC, A> ${hub_type}${ht_params}
9392
}
9493

9594
% for resource in sorted(c.rta_map.keys()):
96-
pub fn ${mangle_ident(resource)}(&'a self) -> ${rb_type(resource)}<'a, C, NC, A> {
95+
pub fn ${mangle_ident(resource)}(&'a self) -> ${rb_type(resource)}${rb_type_params_s(resource, c)} {
9796
${rb_type(resource)} { hub: &self }
9897
}
9998
% endfor

src/mako/lib/lib.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Handle the following *Resources* with ease ...
3030
md_resource = split_camelcase_s(r)
3131
sn = singular(canonical_type_name(r))
3232
33-
if rust_ddoc and sn in schemas:
33+
if rust_doc and sn in schemas:
3434
md_resource = '[%s](struct.%s.html)' % (md_resource, singular(canonical_type_name(r)))
3535
%>\
3636
* ${md_resource} (${put_and(md_methods)})

src/mako/lib/mbuild.mako

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
schema_to_required_property, rust_copy_value_s, is_required_property,
77
hide_rust_doc_test, build_all_params, REQUEST_VALUE_PROPERTY_NAME, organize_params,
88
indent_by, to_rust_type, rnd_arg_val_for_type, extract_parts, mb_type_params_s,
9-
hub_type_params_s, method_media_params, enclose_in)
9+
hub_type_params_s, method_media_params, enclose_in, mb_type_bounds)
1010
1111
def get_parts(part_prop):
1212
if not part_prop:
@@ -101,7 +101,7 @@ pub struct ${ThisType}
101101
102102
impl${mb_tparams} MethodBuilder for ${ThisType} {}
103103
104-
impl${mb_tparams} ${ThisType} {
104+
impl${mb_tparams} ${ThisType} where ${', '.join(mb_type_bounds())} {
105105
106106
${self._action_fn(resource, method, m, params, request_value, parts)}\
107107

src/mako/lib/rbuild.mako

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
to_fqan, indent_all_but_first_by, schema_markers,
55
activity_input_type, TREF, method_io, IO_REQUEST, schema_to_required_property,
66
rust_copy_value_s, is_required_property, organize_params, REQUEST_VALUE_PROPERTY_NAME,
7-
build_all_params, rb_type_params_s, hub_type_params_s, mb_type_params_s)
7+
build_all_params, rb_type_params_s, hub_type_params_s, mb_type_params_s, mb_additional_type_params)
88
%>\
99
<%namespace name="util" file="util.mako"/>\
1010
<%namespace name="lib" file="lib.mako"/>\
@@ -15,7 +15,7 @@
1515
<%def name="new(resource, c)">\
1616
<%
1717
hub_type_name = hub_type(util.canonical_name())
18-
rb_params = rb_type_params_s()
18+
rb_params = rb_type_params_s(resource, c)
1919
ThisType = rb_type(resource) + rb_params
2020
%>\
2121
/// A builder providing access to all methods supported on *${singular(resource)}* resources.
@@ -42,7 +42,7 @@ pub struct ${ThisType}
4242
C: 'a,
4343
A: 'a, {
4444
45-
hub: &'a ${hub_type_name}${hub_type_params_s()}
45+
hub: &'a ${hub_type_name}${hub_type_params_s()},
4646
}
4747
4848
impl${rb_params} ResourceMethodsBuilder for ${ThisType} {}
@@ -71,7 +71,7 @@ impl${rb_params} ${ThisType} {
7171
///
7272
${m.description | rust_doc_comment, indent_all_but_first_by(1)}
7373
% endif
74-
pub fn ${mangle_ident(a)}(&self${method_args}) -> ${RType}${mb_tparams} {
74+
pub fn ${mangle_ident(a)}<${', '.join(mb_additional_type_params(m))}>(&self${method_args}) -> ${RType}${mb_tparams} {
7575
${RType} {
7676
hub: self.hub,
7777
% for p in required_props:

src/mako/lib/util.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
PART_MARKER_TRAIT = 'Part'
4848
NESTED_MARKER_TRAIT = 'NestedType'
4949
REQUEST_VALUE_PROPERTY_NAME = 'request'
50+
DELEGATE_TYPE_PARAM = 'D'
5051

5152
PROTOCOL_TYPE_INFO = {
5253
'simple' : {
@@ -487,12 +488,23 @@ def method_media_params(m):
487488

488489
return res
489490

491+
# Build all parameters used in a given method !
490492
# schemas, context, method(dict), 'request'|'response', request_prop_name -> (params, request_value|None)
491493
def build_all_params(schemas, c, m, n, npn):
492494
request_value = method_io(schemas, c, m, n)
493495
params = method_params(m)
494496
if request_value:
495497
params.insert(0, schema_to_required_property(request_value, npn))
498+
# add the delegate. It's a type parameter, which has to remain in sync with the type-parameters we actually build.
499+
dp = type(m)({ 'name': 'delegate',
500+
TREF: DELEGATE_TYPE_PARAM,
501+
'priority': 0,
502+
'description':
503+
"""The delegate implementation is consulted whenever there is an intermediate result, or if something goes wrong
504+
while executing the actual API request.
505+
506+
It should be used to handle progress information, and to implement a certain level of resilience."""})
507+
params.append(dp)
496508
return params, request_value
497509

498510

@@ -568,13 +580,32 @@ def _to_type_params_s(p):
568580
def hub_type_params_s():
569581
return _to_type_params_s(HUB_TYPE_PARAMETERS)
570582

583+
# return a list of where statements to server as bounds for the hub.
584+
def hub_type_bounds():
585+
return ['NC: hyper::net::NetworkConnector',
586+
"C: BorrowMut<hyper::Client<NC>> + 'a",
587+
'A: oauth2::GetToken']
588+
589+
# return list of type bounds required by method builder
590+
def mb_type_bounds():
591+
return hub_type_bounds() + [DELEGATE_TYPE_PARAM + ': Delegate']
592+
593+
DEFAULT_MB_TYPE_PARAMS = (DELEGATE_TYPE_PARAM, )
594+
595+
_rb_type_params = ("'a", ) + HUB_TYPE_PARAMETERS
596+
597+
571598
# type parameters for a resource builder - keeps hub as borrow
572-
def rb_type_params_s():
573-
return _to_type_params_s(("'a", ) + HUB_TYPE_PARAMETERS)
599+
def rb_type_params_s(resource, c):
600+
return _to_type_params_s(_rb_type_params)
574601

575602
# type params for the given method builder, as string suitable for Rust code
576603
def mb_type_params_s(m):
577-
return _to_type_params_s(("'a", ) + HUB_TYPE_PARAMETERS)
604+
return _to_type_params_s(_rb_type_params + DEFAULT_MB_TYPE_PARAMS)
605+
606+
# as rb_additional_type_params, but for an individual method, as seen from a resource builder !
607+
def mb_additional_type_params(m):
608+
return DEFAULT_MB_TYPE_PARAMS
578609

579610
# return type name for a method on the given resource
580611
def mb_type(r, m):

src/rust/cmn.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ use std::marker::MarkerTrait;
22
use std::io::{Read, Seek};
33
use std::borrow::BorrowMut;
44

5+
use oauth2;
6+
use hyper;
7+
58
/// Identifies the Hub. There is only one per library, this trait is supposed
69
/// to make intended use more explicit.
710
/// The hub allows to access all resource methods more easily.
@@ -41,4 +44,20 @@ impl<T: Seek + Read> ReadSeek for T {}
4144
struct JsonServerError {
4245
error: String,
4346
error_description: Option<String>
44-
}
47+
}
48+
49+
50+
/// A trait specifying functionality to help controlling any request performed by the API.
51+
/// The trait has a conservative default implementation.
52+
///
53+
/// It contains methods to deal with all common issues, as well with the ones related to
54+
/// uploading media
55+
pub trait Delegate: Clone {
56+
57+
/// Called whenever there is an HttpError, usually if there are network problems.
58+
///
59+
/// Return retry information.
60+
fn connection_error(&mut self, hyper::HttpError) -> oauth2::Retry {
61+
oauth2::Retry::Abort
62+
}
63+
}

0 commit comments

Comments
 (0)