Skip to content

Commit 432faa2

Browse files
committed
feat(dev): spike to see how delegate can be work
To avoid an additional type parameter, we will use dynamic dispatch for the delegate. Having function overrides at some point seems like an excercise better left for version 1.1 ;)
1 parent 678b692 commit 432faa2

3 files changed

Lines changed: 91 additions & 5 deletions

File tree

src/rust/cmn.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::marker::MarkerTrait;
22
use std::io::{Read, Seek};
3-
use std::borrow::BorrowMut;
43

54
use oauth2;
65
use hyper;
@@ -52,7 +51,7 @@ struct JsonServerError {
5251
///
5352
/// It contains methods to deal with all common issues, as well with the ones related to
5453
/// uploading media
55-
pub trait Delegate: Clone {
54+
pub trait Delegate {
5655

5756
/// Called whenever there is an HttpError, usually if there are network problems.
5857
///
@@ -61,3 +60,8 @@ pub trait Delegate: Clone {
6160
oauth2::Retry::Abort
6261
}
6362
}
63+
64+
#[derive(Default)]
65+
pub struct DefaultDelegate;
66+
67+
impl Delegate for DefaultDelegate {}

src/rust/dev/mod.rs

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
//! # }
1313
//! ```
1414
use std::marker::PhantomData;
15-
use std::borrow::BorrowMut;
1615
use std::cell::RefCell;
16+
use std::borrow::BorrowMut;
17+
use std::default::Default;
1718

1819
use hyper;
1920
use oauth2;
@@ -45,6 +46,10 @@ impl<'a, C, NC, A> YouTube<C, NC, A>
4546
pub fn videos(&'a self) -> videos::Service<'a, C, NC, A> {
4647
videos::Service::new(&self)
4748
}
49+
50+
pub fn channel_sections(&'a self) -> ChannelSectionMethodsBuilder<'a, C, NC, A> {
51+
ChannelSectionMethodsBuilder { hub: &self }
52+
}
4853
}
4954

5055

@@ -77,4 +82,81 @@ mod tests {
7782

7883
let v = yt.videos().insert("snippet", &Default::default());
7984
}
80-
}
85+
86+
#[test] fn helper_test() {
87+
use std::default::Default;
88+
use oauth2::{Authenticator, DefaultAuthenticatorDelegate, ApplicationSecret, MemoryStorage};
89+
90+
let secret: ApplicationSecret = Default::default();
91+
let auth = Authenticator::new(&secret, DefaultAuthenticatorDelegate,
92+
hyper::Client::new(),
93+
<MemoryStorage as Default>::default(), None);
94+
let mut hub = YouTube::new(hyper::Client::new(), auth);
95+
let result = hub.channel_sections().insert()
96+
.delegate(&mut <DefaultDelegate as Default>::default())
97+
.doit();
98+
}
99+
}
100+
101+
pub struct ChannelSectionMethodsBuilder<'a, C, NC, A>
102+
where NC: 'a,
103+
C: 'a,
104+
A: 'a, {
105+
106+
hub: &'a YouTube<C, NC, A>,
107+
}
108+
109+
impl<'a, C, NC, A> ChannelSectionMethodsBuilder<'a, C, NC, A> {
110+
111+
/// Create a builder to help you perform the following task:
112+
///
113+
/// Adds a channelSection for the authenticated user's channel.
114+
pub fn insert(&self) -> ChannelSectionInsertMethodBuilder<'a, C, NC, A> {
115+
ChannelSectionInsertMethodBuilder {
116+
hub: self.hub,
117+
_delegate: Default::default(),
118+
}
119+
}
120+
}
121+
122+
pub struct ChannelSectionInsertMethodBuilder<'a, C, NC, A>
123+
where NC: 'a,
124+
C: 'a,
125+
A: 'a, {
126+
127+
hub: &'a YouTube<C, NC, A>,
128+
_delegate: Option<&'a mut Delegate>,
129+
}
130+
131+
132+
impl<'a, C, NC, A> ChannelSectionInsertMethodBuilder<'a, C, NC, A> where NC: hyper::net::NetworkConnector, C: BorrowMut<hyper::Client<NC>> + 'a, A: oauth2::GetToken {
133+
134+
/// Perform the operation you have build so far.
135+
/// TODO: Build actual call
136+
pub fn doit(mut self) -> () {
137+
if self._delegate.is_some() {
138+
self._delegate.as_mut().unwrap().connection_error(hyper::HttpError::HttpStatusError);
139+
}
140+
}
141+
142+
pub fn delegate(mut self, new_value: &'a mut Delegate) -> ChannelSectionInsertMethodBuilder<'a, C, NC, A> {
143+
self._delegate = Some(new_value);
144+
self
145+
}
146+
147+
}
148+
149+
pub trait Delegate {
150+
151+
/// Called whenever there is an HttpError, usually if there are network problems.
152+
///
153+
/// Return retry information.
154+
fn connection_error(&mut self, hyper::HttpError) -> oauth2::Retry {
155+
oauth2::Retry::Abort
156+
}
157+
}
158+
159+
#[derive(Default)]
160+
pub struct DefaultDelegate;
161+
162+
impl Delegate for DefaultDelegate {}

src/rust/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(core)]
1+
#![feature(core,io)]
22
//! library with code shared by all generated implementations
33
extern crate hyper;
44
extern crate "rustc-serialize" as rustc_serialize;

0 commit comments

Comments
 (0)